Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): implement official CBOR based metadata #96

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"bitcoinjs-lib": "6.1.3",
"bitcoinjs-message": "2.2.0",
"buffer-reverse": "^1.0.1",
"cbor-js": "^0.1.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we replace it with this? https://www.npmjs.com/package/cbor

chor-js was last released 8 years ago.

"cross-fetch": "3.1.6",
"ecpair": "2.1.0",
"ethers": "6.6.1",
Expand Down
32 changes: 13 additions & 19 deletions packages/sdk/src/inscription/witness.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -14,28 +15,21 @@ 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") {
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))
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_ENDIF)

metaChunks && metaStackElements.push(bitcoin.opcodes.OP_0)
}

const baseStackElements = [
Expand All @@ -46,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])
])
}

Expand Down
5 changes: 5 additions & 0 deletions packages/sdk/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

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