diff --git a/.changeset/two-badgers-repeat.md b/.changeset/two-badgers-repeat.md new file mode 100644 index 00000000..e42b3c18 --- /dev/null +++ b/.changeset/two-badgers-repeat.md @@ -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 diff --git a/packages/ckb/package.json b/packages/ckb/package.json index 6c5a6fa8..e29d4f16 100644 --- a/packages/ckb/package.json +++ b/packages/ckb/package.json @@ -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" diff --git a/packages/ckb/src/rgbpp/btc-time.ts b/packages/ckb/src/rgbpp/btc-time.ts index 8eceebae..cfed7b6e 100644 --- a/packages/ckb/src/rgbpp/btc-time.ts +++ b/packages/ckb/src/rgbpp/btc-time.ts @@ -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)); }; diff --git a/packages/ckb/src/rgbpp/ckb-builder.ts b/packages/ckb/src/rgbpp/ckb-builder.ts index 08e470c1..e9df3276 100644 --- a/packages/ckb/src/rgbpp/ckb-builder.ts +++ b/packages/ckb/src/rgbpp/ckb-builder.ts @@ -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, @@ -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)); }; diff --git a/packages/ckb/src/schemas/generated/blockchain.ts b/packages/ckb/src/schemas/generated/blockchain.ts index 98221df5..71e1bbb0 100644 --- a/packages/ckb/src/schemas/generated/blockchain.ts +++ b/packages/ckb/src/schemas/generated/blockchain.ts @@ -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({ @@ -41,7 +38,7 @@ export const ProposalShortIdVec = vector(ProposalShortId); export const Script = table( { codeHash: Byte32, - hashType: HashType, + hashType: byte, args: Bytes, }, ['codeHash', 'hashType', 'args'], @@ -66,7 +63,7 @@ export const CellInput = struct( export const CellDep = struct( { outPoint: OutPoint, - depType: DepType, + depType: byte, }, ['outPoint', 'depType'], ); diff --git a/packages/ckb/src/schemas/generated/rgbpp.ts b/packages/ckb/src/schemas/generated/rgbpp.ts index 95208477..f3bd61f7 100644 --- a/packages/ckb/src/schemas/generated/rgbpp.ts +++ b/packages/ckb/src/schemas/generated/rgbpp.ts @@ -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({ diff --git a/packages/ckb/src/utils/rgbpp.spec.ts b/packages/ckb/src/utils/rgbpp.spec.ts index a4c51183..7252efb7 100644 --- a/packages/ckb/src/utils/rgbpp.spec.ts +++ b/packages/ckb/src/utils/rgbpp.spec.ts @@ -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', () => { diff --git a/packages/ckb/src/utils/rgbpp.ts b/packages/ckb/src/utils/rgbpp.ts index ef08dff7..c24f4560 100644 --- a/packages/ckb/src/utils/rgbpp.ts +++ b/packages/ckb/src/utils/rgbpp.ts @@ -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, @@ -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 { @@ -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), }; }; @@ -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, }; }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b6d69799..ab119410 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -226,8 +226,8 @@ importers: version: 0.11.0 devDependencies: '@ckb-lumos/molecule': - specifier: ^0.22.2 - version: 0.22.2 + specifier: 0.0.0-canary-66bbbfd-20240805132534 + version: 0.0.0-canary-66bbbfd-20240805132534 packages/rgbpp: dependencies: @@ -558,6 +558,10 @@ packages: resolution: {integrity: sha512-nosUCSa5rTV2IzxbEpqzrvUeQNXB66mgA0h40+QEdnE/gV/s4ke83AScrTAxWkErJy1G/sToIHCc2kWwO95DfQ==} engines: {node: '>=12.0.0'} + '@ckb-lumos/bi@0.0.0-canary-66bbbfd-20240805132534': + resolution: {integrity: sha512-0eEPkaZ7WOXIkHbRx/vD2kynn4b5TRT8Xo3DGFPKDd/9ZQJX07fPvaCRdCV9/fyEZXC4aVv0XskWySKbrWDDLw==} + engines: {node: '>=12.0.0'} + '@ckb-lumos/bi@0.22.0-next.5': resolution: {integrity: sha512-hfh9PQDLJeZedlJ6qBVxx2MR9ndU0XyIl53vh1I+S4SGmvl/z2PGEY06vN34nkjmZ/1c87/KW+sL2Cpii0IxGg==} engines: {node: '>=12.0.0'} @@ -570,6 +574,10 @@ packages: resolution: {integrity: sha512-1UZ0whrn9k18wrvIIvsdAYRCGInNA4qcrMcxsqSV7ADQnPsw8c+ARkIyzQrqaM2bzvPa3O+aVXnZ7g2oTrhtFA==} engines: {node: '>=12.0.0'} + '@ckb-lumos/codec@0.0.0-canary-66bbbfd-20240805132534': + resolution: {integrity: sha512-/9RY2SfviZtWkSQbmgNxMHoora0vv6hCFB09n/RXF1jyfmmVNKuhtcbDKAiTl4WwWmxdJd1DhgPP0ARvM7VKfg==} + engines: {node: '>=12.0.0'} + '@ckb-lumos/codec@0.22.0-next.5': resolution: {integrity: sha512-AVvREoEc0zSbGSdFZOph1WL9QtUKUjQ2PABpApROMTqDCryG9/FpBrInjm2y50USZlwcEE3+ocEqFqJjfuTXKQ==} engines: {node: '>=12.0.0'} @@ -602,8 +610,8 @@ packages: resolution: {integrity: sha512-Dv+MKa664zsD9Nxrnor04Hb52MNcwgtEutgzdlE+qtAB/skp6P0NVnU9uQF7RiYHNpdmUe8PROlkzB4jaKz9NA==} engines: {node: '>=12.0.0'} - '@ckb-lumos/molecule@0.22.2': - resolution: {integrity: sha512-9XwQUP/LCV+B+N4hWKiGZ2Di23chYTzKi9iWjCYCoVpjer2b19AZwEyJF/43pu9bw1C586LhtzQG6swSvKYLFA==} + '@ckb-lumos/molecule@0.0.0-canary-66bbbfd-20240805132534': + resolution: {integrity: sha512-YvUQUctRuRWoeiohjq+KNpXQ3FAuJHpiJxlFNbn+WoBX7TlIgIR69B/cRRvoa7gD5QuoA8q5zf9+0l4eYmm1Qw==} engines: {node: '>=12.0.0'} hasBin: true @@ -4833,6 +4841,10 @@ snapshots: js-xxhash: 1.0.4 lodash.isequal: 4.5.0 + '@ckb-lumos/bi@0.0.0-canary-66bbbfd-20240805132534': + dependencies: + jsbi: 4.3.0 + '@ckb-lumos/bi@0.22.0-next.5': dependencies: jsbi: 4.3.0 @@ -4853,6 +4865,10 @@ snapshots: transitivePeerDependencies: - encoding + '@ckb-lumos/codec@0.0.0-canary-66bbbfd-20240805132534': + dependencies: + '@ckb-lumos/bi': 0.0.0-canary-66bbbfd-20240805132534 + '@ckb-lumos/codec@0.22.0-next.5': dependencies: '@ckb-lumos/bi': 0.22.0-next.5 @@ -4936,10 +4952,10 @@ snapshots: transitivePeerDependencies: - encoding - '@ckb-lumos/molecule@0.22.2': + '@ckb-lumos/molecule@0.0.0-canary-66bbbfd-20240805132534': dependencies: - '@ckb-lumos/bi': 0.22.2 - '@ckb-lumos/codec': 0.22.2 + '@ckb-lumos/bi': 0.0.0-canary-66bbbfd-20240805132534 + '@ckb-lumos/codec': 0.0.0-canary-66bbbfd-20240805132534 '@types/moo': 0.5.9 '@types/nearley': 2.11.5 glob: 10.4.5