Skip to content

Commit

Permalink
Merge pull request #279 from ckb-cell/fix/molecule-bytes-error
Browse files Browse the repository at this point in the history
fix(rgbpp-sdk): Update molecule codegen, packing and unpacking
  • Loading branch information
duanyytop authored Aug 15, 2024
2 parents 27b9fbe + 5230bf1 commit 26003cd
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 36 deletions.
8 changes: 8 additions & 0 deletions .changeset/two-badgers-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@rgbpp-sdk/ckb': patch
---

fix: Update molecule codegen, packing and unpacking

- Generate molecule code with latest lumos/molecule
- Update RGB++ witness and BTC time lock args packing and unpacking
2 changes: 1 addition & 1 deletion packages/ckb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"js-sha256": "^0.11.0"
},
"devDependencies": {
"@ckb-lumos/molecule": "^0.22.2"
"@ckb-lumos/molecule": "0.0.0-canary-66bbbfd-20240805132534"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 1 addition & 2 deletions packages/ckb/src/rgbpp/btc-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ import {
transformSpvProof,
buildSpvClientCellDep,
} from '../utils';
import { blockchain } from '@ckb-lumos/base';
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses';

export const buildBtcTimeUnlockWitness = (btcTxProof: Hex): Hex => {
const btcTimeUnlock = BTCTimeUnlock.pack({ btcTxProof: blockchain.Bytes.pack(btcTxProof) });
const btcTimeUnlock = BTCTimeUnlock.pack({ btcTxProof });
return append0x(bytesToHex(btcTimeUnlock));
};

Expand Down
7 changes: 2 additions & 5 deletions packages/ckb/src/rgbpp/ckb-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
import { InputsCapacityNotEnoughError } from '../error';
import { buildSpvClientCellDep } from '../utils';
import { RGBPPUnlock, Uint16 } from '../schemas/generated/rgbpp';
import { blockchain } from '@ckb-lumos/base';
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses';
import {
bytesToHex,
Expand All @@ -36,14 +35,12 @@ export const buildRgbppUnlockWitness = (
const inputLen = append0x(u8ToHex(inputsLen));
const outputLen = append0x(u8ToHex(outputsLen));

const btcTx = blockchain.Bytes.pack(append0x(btcTxBytes));

const version = Uint16.pack([0, 0]);
const rgbppUnlock = RGBPPUnlock.pack({
version,
extraData: { inputLen, outputLen },
btcTx,
btcTxProof: bytesToHex(blockchain.Bytes.pack(append0x(btcTxProof))),
btcTx: append0x(btcTxBytes),
btcTxProof: append0x(btcTxProof),
});
return append0x(bytesToHex(rgbppUnlock));
};
Expand Down
11 changes: 4 additions & 7 deletions packages/ckb/src/schemas/generated/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import { bytes, createBytesCodec, createFixedBytesCodec, molecule } from '@ckb-lumos/codec';
import { Uint32, Uint64, Uint128, DepType, HashType } from '../customized';

const { array, vector, union, option, struct, table } = molecule;
const { array, vector, union, option, struct, table, byteVecOf } = molecule;

const fallbackBytesCodec = createBytesCodec({
pack: bytes.bytify,
unpack: bytes.hexify,
});
const fallbackBytesCodec = byteVecOf({ pack: bytes.bytify, unpack: bytes.hexify });

function createFallbackFixedBytesCodec(byteLength: number) {
return createFixedBytesCodec({
Expand Down Expand Up @@ -41,7 +38,7 @@ export const ProposalShortIdVec = vector(ProposalShortId);
export const Script = table(
{
codeHash: Byte32,
hashType: HashType,
hashType: byte,
args: Bytes,
},
['codeHash', 'hashType', 'args'],
Expand All @@ -66,7 +63,7 @@ export const CellInput = struct(
export const CellDep = struct(
{
outPoint: OutPoint,
depType: DepType,
depType: byte,
},
['outPoint', 'depType'],
);
Expand Down
7 changes: 2 additions & 5 deletions packages/ckb/src/schemas/generated/rgbpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ import {
BlockV1,
} from './blockchain';

const { array, vector, union, option, struct, table } = molecule;
const { array, vector, union, option, struct, table, byteVecOf } = molecule;

const fallbackBytesCodec = createBytesCodec({
pack: bytes.bytify,
unpack: bytes.hexify,
});
const fallbackBytesCodec = byteVecOf({ pack: bytes.bytify, unpack: bytes.hexify });

function createFallbackFixedBytesCodec(byteLength: number) {
return createFixedBytesCodec({
Expand Down
28 changes: 24 additions & 4 deletions packages/ckb/src/utils/rgbpp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,39 @@ describe('rgbpp tests', () => {
});

it('lockScriptFromBtcTimeLockArgs', () => {
const lockArgs =
let lockArgs =
'0x7d00000010000000590000005d000000490000001000000030000000310000000101010101010101010101010101010101010101010101010101010101010101011400000002020202020202020202020202020202020202022a0000000303030303030303030303030303030303030303030303030303030303030303';
const lock = lockScriptFromBtcTimeLockArgs(lockArgs);
expect(lock.codeHash).toBe('0x0101010101010101010101010101010101010101010101010101010101010101');
expect(lock.args).toBe('0x0202020202020202020202020202020202020202');

lockArgs =
'0x890000001000000065000000690000005500000010000000300000003100000028e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a50020000000c0a45d9d7c024adcc8076c18b3f07c08de7c42120cdb7e6cbc05a28266b15b5f060000000000000000000000000000000000000000000000000000000000000000000000';
const { codeHash, args, hashType } = lockScriptFromBtcTimeLockArgs(lockArgs);
expect(codeHash).toBe('0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5');
expect(args).toBe('0xc0a45d9d7c024adcc8076c18b3f07c08de7c42120cdb7e6cbc05a28266b15b5f');
expect(hashType).toBe('data');

lockArgs =
'0x7f000000100000005b0000005f0000004b0000001000000030000000310000009b819793a64463aed77c615d6cb226eea5487ccfc0783043a587254cda2b6f2601160000000430455c7db8901bee35dc70eeff078c2adfc729920006000000964663ab3b35fd5f02ac29f268ea29401504255707c0b3a943fe143196e80770';
const result = lockScriptFromBtcTimeLockArgs(lockArgs);
expect(result.codeHash).toBe('0x9b819793a64463aed77c615d6cb226eea5487ccfc0783043a587254cda2b6f26');
expect(result.args).toBe('0x0430455c7db8901bee35dc70eeff078c2adfc7299200');
expect(hashType).toBe('data');
});

it('btcTxIdAndAfterFromBtcTimeLockArgs', () => {
const lockArgs =
'0x850000001000000061000000650000005100000010000000300000003100000028e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a500c0a45d9d7c024adcc8076c18b3f07c08de7c42120cdb7e6cbc05a28266b15b5f0600000006ec22c2def100bba3e295a1ff279c490d227151bf3166a4f3f008906c849399';
let lockArgs =
'0x7f000000100000005b0000005f0000004b000000100000003000000031000000d23761b364210735c19c60561d213fb3beae2fd6172743719eff6920e020baac011600000000016c61f984f12d3c8a4f649e60acda5deda0b8837c060000007213bf894727c1b3cb99556549c01c73ba439ecf84d5f6974b3928bc025f4ed4';
const { btcTxId, after } = btcTxIdAndAfterFromBtcTimeLockArgs(lockArgs);
expect(btcTxId).toBe('0x9993846c9008f0f3a46631bf5171220d499c27ffa195e2a3bb00f1dec222ec06');
expect(btcTxId).toBe('0xd44e5f02bc28394b97f6d584cf9e43ba731cc049655599cbb3c1274789bf1372');
expect(after).toBe(6);

lockArgs =
'0x7f000000100000005b0000005f0000004b0000001000000030000000310000009b819793a64463aed77c615d6cb226eea5487ccfc0783043a587254cda2b6f2601160000000430455c7db8901bee35dc70eeff078c2adfc729920006000000964663ab3b35fd5f02ac29f268ea29401504255707c0b3a943fe143196e80770';
const result = btcTxIdAndAfterFromBtcTimeLockArgs(lockArgs);
expect(result.btcTxId).toBe('0x7007e8963114fe43a9b3c007572504154029ea68f229ac025ffd353bab634696');
expect(result.after).toBe(6);
});

it('calculateUdtCellCapacity', () => {
Expand Down
13 changes: 8 additions & 5 deletions packages/ckb/src/utils/rgbpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { RGBPPLock } from '../schemas/generated/rgbpp';
import { BTCTimeLock } from '../schemas/generated/rgbpp';
import { Script } from '../schemas/generated/blockchain';
import { bytes, BytesLike } from '@ckb-lumos/codec';
import { BytesLike } from '@ckb-lumos/codec';
import { toCamelcase } from './case-parser';
import {
InputsOrOutputsLenError,
Expand All @@ -30,6 +30,7 @@ import {
serializeOutput,
serializeScript,
} from '@nervosnetwork/ckb-sdk-utils';
import { HashType } from '../schemas/customized';

export const genRgbppLockScript = (rgbppLockArgs: Hex, isMainnet: boolean, btcTestnetType?: BTCTestnetType) => {
return {
Expand Down Expand Up @@ -114,7 +115,7 @@ export const lockScriptFromBtcTimeLockArgs = (args: Hex): CKBComponents.Script =
const { lockScript } = BTCTimeLock.unpack(append0x(args));
return {
...lockScript,
args: bytes.hexify(blockchain.Bytes.unpack(lockScript.args)),
hashType: HashType.unpack(lockScript.hashType),
};
};

Expand All @@ -123,10 +124,12 @@ export interface BTCTimeLockArgs {
after: number;
}
export const btcTxIdAndAfterFromBtcTimeLockArgs = (args: Hex): BTCTimeLockArgs => {
const btcTimeLockArgs = BTCTimeLock.unpack(append0x(args));
const { btcTxid, after } = BTCTimeLock.unpack(append0x(args));
console.log(btcTxid);
console.log(after);
return {
btcTxId: reverseHex(append0x(btcTimeLockArgs.btcTxid)),
after: btcTimeLockArgs.after,
btcTxId: reverseHex(append0x(btcTxid)),
after,
};
};

Expand Down
30 changes: 23 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 26003cd

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New snapshot version of the rgbpp-sdk packages have been released:

Name Version
@rgbpp-sdk/btc 0.0.0-snap-20240815002507
@rgbpp-sdk/ckb 0.0.0-snap-20240815002507
rgbpp 0.0.0-snap-20240815002507
@rgbpp-sdk/service 0.0.0-snap-20240815002507

Please sign in to comment.