Skip to content

Commit

Permalink
feat: implement official CBOR based metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcrazycoder committed Nov 8, 2023
1 parent 71f90a7 commit 07c06dd
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 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 @@ -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)
}

Expand Down

0 comments on commit 07c06dd

Please sign in to comment.