From 71f90a79a2c0c7b523366e39c67d95c774d6c04e Mon Sep 17 00:00:00 2001 From: Nishant Ghodke Date: Wed, 8 Nov 2023 15:09:17 +0530 Subject: [PATCH 1/5] chore: installl cbor-js --- packages/sdk/package.json | 1 + packages/sdk/src/types.d.ts | 5 +++++ pnpm-lock.yaml | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/packages/sdk/package.json b/packages/sdk/package.json index b239a3d7..2ae331ad 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -30,6 +30,7 @@ "bitcoinjs-lib": "6.1.3", "bitcoinjs-message": "2.2.0", "buffer-reverse": "^1.0.1", + "cbor-js": "^0.1.0", "cross-fetch": "3.1.6", "ecpair": "2.1.0", "ethers": "6.6.1", diff --git a/packages/sdk/src/types.d.ts b/packages/sdk/src/types.d.ts index ba47b52c..35293680 100644 --- a/packages/sdk/src/types.d.ts +++ b/packages/sdk/src/types.d.ts @@ -22,3 +22,8 @@ type MetaMask = { declare module "buffer-reverse" { export = (_: Buffer): Buffer => {} } + +declare module "cbor-js" { + function encode(object: NestedObject): ArrayBuffer + function decode(data: ArrayBuffer): NestedObject +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3c6cf319..cc1038f9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,9 @@ importers: '@sadoprotocol/ordit-sdk': specifier: workspace version: link:../../packages/sdk + cbor-js: + specifier: ^0.1.0 + version: 0.1.0 packages/sdk: dependencies: @@ -47,6 +50,9 @@ importers: buffer-reverse: specifier: ^1.0.1 version: 1.0.1 + cbor-js: + specifier: ^0.1.0 + version: 0.1.0 cross-fetch: specifier: 3.1.6 version: 3.1.6 @@ -690,6 +696,10 @@ packages: engines: {node: '>=10'} dev: true + /cbor-js@0.1.0: + resolution: {integrity: sha512-7sQ/TvDZPl7csT1Sif9G0+MA0I0JOVah8+wWlJVQdVEgIbCzlN/ab3x+uvMNsc34TUvO6osQTAmB2ls80JX6tw==} + dev: false + /chalk@2.4.2: resolution: {integrity: sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==} engines: {node: '>=4'} From 07c06dd3e15e7f7115eb667cd5af7e14a6ad3f40 Mon Sep 17 00:00:00 2001 From: Nishant Ghodke Date: Wed, 8 Nov 2023 15:10:13 +0530 Subject: [PATCH 2/5] feat: implement official CBOR based metadata --- packages/sdk/src/inscription/witness.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/packages/sdk/src/inscription/witness.ts b/packages/sdk/src/inscription/witness.ts index 297d718c..624d654c 100644 --- a/packages/sdk/src/inscription/witness.ts +++ b/packages/sdk/src/inscription/witness.ts @@ -1,5 +1,6 @@ import * as ecc from "@bitcoinerlab/secp256k1" import * as bitcoin from "bitcoinjs-lib" +import CBOR from "cbor-js" import { MAXIMUM_SCRIPT_ELEMENT_SIZE } from "../constants" @@ -18,23 +19,18 @@ export function buildWitnessScript({ recover = false, ...options }: WitnessScrip const metaStackElements: (number | Buffer)[] = [] if (typeof options.meta === "object") { - metaStackElements.push( - ...[ - bitcoin.opcodes.OP_FALSE, - bitcoin.opcodes.OP_IF, - opPush("ord"), - 1, - 1, - opPush("application/json;charset=utf-8"), - bitcoin.opcodes.OP_0 - ] - ) - const metaChunks = chunkContent(JSON.stringify(options.meta)) + metaStackElements.push(...[bitcoin.opcodes.OP_FALSE, bitcoin.opcodes.OP_IF]) + const encoded = Buffer.from(new Uint8Array(CBOR.encode(options.meta))).toString("hex") + const metaChunks = chunkContent(encoded, "hex") metaChunks && metaChunks.forEach((chunk) => { + metaStackElements.push(1) + metaStackElements.push(5) metaStackElements.push(opPush(chunk)) }) + + metaChunks && metaStackElements.push(bitcoin.opcodes.OP_0) metaChunks && metaStackElements.push(bitcoin.opcodes.OP_ENDIF) } From b17995f5fb5d7ab808b718b89077b799cfd5df9b Mon Sep 17 00:00:00 2001 From: Nishant Ghodke Date: Wed, 8 Nov 2023 18:01:17 +0530 Subject: [PATCH 3/5] refactor: merge metadata in 1st envelope --- packages/sdk/src/inscription/witness.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/sdk/src/inscription/witness.ts b/packages/sdk/src/inscription/witness.ts index 624d654c..d07e4cf8 100644 --- a/packages/sdk/src/inscription/witness.ts +++ b/packages/sdk/src/inscription/witness.ts @@ -19,7 +19,6 @@ export function buildWitnessScript({ recover = false, ...options }: WitnessScrip const metaStackElements: (number | Buffer)[] = [] if (typeof options.meta === "object") { - metaStackElements.push(...[bitcoin.opcodes.OP_FALSE, bitcoin.opcodes.OP_IF]) const encoded = Buffer.from(new Uint8Array(CBOR.encode(options.meta))).toString("hex") const metaChunks = chunkContent(encoded, "hex") @@ -31,7 +30,6 @@ export function buildWitnessScript({ recover = false, ...options }: WitnessScrip }) metaChunks && metaStackElements.push(bitcoin.opcodes.OP_0) - metaChunks && metaStackElements.push(bitcoin.opcodes.OP_ENDIF) } const baseStackElements = [ From 2ec57c101a7dda5b06d5f5fb882d5b2f09ccdc95 Mon Sep 17 00:00:00 2001 From: Nishant Ghodke Date: Wed, 8 Nov 2023 18:02:03 +0530 Subject: [PATCH 4/5] refactor: move content to end of witness stack --- packages/sdk/src/inscription/witness.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/sdk/src/inscription/witness.ts b/packages/sdk/src/inscription/witness.ts index d07e4cf8..74d330f1 100644 --- a/packages/sdk/src/inscription/witness.ts +++ b/packages/sdk/src/inscription/witness.ts @@ -15,7 +15,7 @@ export function buildWitnessScript({ recover = false, ...options }: WitnessScrip } const contentChunks = chunkContent(options.mediaContent, !options.mediaType.includes("text") ? "base64" : "utf8") - const contentStackElements = contentChunks.map(opPush) + const contentStackElements: (number | Buffer)[] = contentChunks.map(opPush) const metaStackElements: (number | Buffer)[] = [] if (typeof options.meta === "object") { @@ -40,15 +40,15 @@ export function buildWitnessScript({ recover = false, ...options }: WitnessScrip opPush("ord"), 1, 1, - opPush(options.mediaType), - bitcoin.opcodes.OP_0 + opPush(options.mediaType) ] return bitcoin.script.compile([ ...baseStackElements, - ...contentStackElements, - bitcoin.opcodes.OP_ENDIF, - ...metaStackElements + ...metaStackElements, + ...(contentStackElements.length + ? contentStackElements.concat(bitcoin.opcodes.OP_ENDIF) + : [bitcoin.opcodes.OP_ENDIF]) ]) } From 2eff1dd9b043267d113ed960f269c5aea4b34830 Mon Sep 17 00:00:00 2001 From: Nishant Ghodke Date: Wed, 8 Nov 2023 18:09:28 +0530 Subject: [PATCH 5/5] chore: uninstall cbor-js from wrong dir --- pnpm-lock.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cc1038f9..fddb46fb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,9 +23,6 @@ importers: '@sadoprotocol/ordit-sdk': specifier: workspace version: link:../../packages/sdk - cbor-js: - specifier: ^0.1.0 - version: 0.1.0 packages/sdk: dependencies: