UNPKG

@okxweb3/coin-bitcoin

Version:

@okxweb3/coin-bitcoin is a Bitcoin SDK for building Web3 wallets and applications. It supports BTC, BSV, DOGE, LTC, and TBTC, enabling private key management, transaction signing, address generation, and inscriptions like BRC-20, Runes, CAT, and Atomicals

207 lines 7.17 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildRuneMainMintOp = exports.buildRuneMainMintData = exports.buildRuneData = exports.encodeToVecV2 = exports.toVarIntV2 = exports.fromVarInt = exports.encodeToVec = exports.toVarInt = void 0; const bscript = __importStar(require("./bitcoinjs-lib/script")); const ops_1 = require("./bitcoinjs-lib/ops"); const wallet_1 = require("./wallet"); const crypto_lib_1 = require("@okxweb3/crypto-lib"); const TAG_BODY = BigInt(0); const TAG_Flags = BigInt(2); const TAG_Rune = BigInt(4); const TAG_Premine = BigInt(6); const TAG_Cap = BigInt(8); const TAG_Amount = BigInt(10); const TAG_HeightStart = BigInt(12); const TAG_HeightEnd = BigInt(14); const TAG_OffsetStart = BigInt(16); const TAG_OffsetEnd = BigInt(118); const TAG_Mint = BigInt(20); const TAG_Pointer = BigInt(22); const TAG_Cenotaph = BigInt(126); const TAG_Divisibility = BigInt(1); const TAG_Spacers = BigInt(3); const TAG_Symbol = BigInt(5); const TAG_Nop = BigInt(127); function encode(n) { let payload = []; encodeToVec(n, payload); return new Uint8Array(payload); } exports.toVarInt = encode; function encodeToVec(n, payload) { let i = 18; const out = new Array(19).fill(0); out[i] = Number(n & BigInt(0x7F)); while (n > BigInt(0x7F)) { n = n / BigInt(128) - BigInt(1); i--; out[i] = Number(n & BigInt(0xFF)) | 0x80; } payload.push(...out.slice(i)); } exports.encodeToVec = encodeToVec; function encodeV2(n) { let payload = []; encodeToVecV2(n, payload); return new Uint8Array(payload); } exports.toVarIntV2 = encodeV2; function encodeToVecV2(n, payload) { while (n >> 7n > 0n) { payload.push(Number((n & 0x7fn) | 0x80n)); n >>= 7n; } payload.push(Number(n & 0x7fn)); return payload; } exports.encodeToVecV2 = encodeToVecV2; function decode(buffer) { let n = BigInt(0); let i = 0; while (true) { const b = BigInt(buffer[i]); if (b < BigInt(128)) { return [n + b, i + 1]; } n += b - BigInt(127); n = n * BigInt(128); i++; if (i >= buffer.length) { throw new Error("Varint decoding error: buffer overflow"); } } } exports.fromVarInt = decode; function buildRuneData(isMainnet, edicts) { let payload = []; for (let edict of edicts) { if (typeof edict.amount === "string") { edict.amount = BigInt(edict.amount); } } if (edicts.length > 0) { encodeToVec(TAG_BODY, payload); edicts.sort((a, b) => a.id - b.id); let id = 0; for (const edict of edicts) { encodeToVec(BigInt(edict.id - id), payload); encodeToVec(BigInt(edict.amount), payload); encodeToVec(BigInt(edict.output), payload); id = edict.id; } } let prefix; if (isMainnet) { prefix = 'R'; } else { prefix = 'RUNE_TEST'; } const opReturnScript = bscript.compile([ops_1.OPS.OP_RETURN, Buffer.from(prefix), Buffer.from(payload)]); return opReturnScript; } exports.buildRuneData = buildRuneData; function buildRuneMainMintData(isMainnet, edicts, useDefaultOutput, defaultOutput, mint, mintNum) { let payload = []; for (let edict of edicts) { if (typeof edict.amount === "string") { edict.amount = BigInt(edict.amount); } } if ((mint != undefined) && mint && edicts[0].block != undefined) { encodeToVecV2(TAG_Mint, payload); encodeToVecV2(BigInt(edicts[0].block), payload); encodeToVecV2(TAG_Mint, payload); encodeToVecV2(BigInt(edicts[0].id), payload); } if (useDefaultOutput) { encodeToVecV2(TAG_Pointer, payload); encodeToVecV2(BigInt(defaultOutput), payload); } if (edicts.length > 0) { encodeToVecV2(TAG_BODY, payload); edicts.sort((a, b) => { if (a.block === b.block) { if (a.id < b.id) { return -1; } if (a.id > b.id) { return 1; } return 0; } return a.block - b.block; }); let id = 0; let block = 0; for (const edict of edicts) { encodeToVecV2(BigInt(edict.block - block), payload); encodeToVecV2(BigInt(edict.id - id), payload); encodeToVecV2(BigInt(edict.amount), payload); encodeToVecV2(BigInt(edict.output), payload); id = edict.id; block = edict.block; } } if (payload.length > 80) { throw new Error(JSON.stringify({ errCode: wallet_1.ErrCodeOpreturnExceeds, date: { payloadLenth: payload.length } })); } const opReturnScript = bscript.compile([ops_1.OPS.OP_RETURN, ops_1.OPS.OP_13, Buffer.from(payload)]); return opReturnScript; } exports.buildRuneMainMintData = buildRuneMainMintData; function buildRuneMainMintOp(id, useDefaultOutput, defaultOutput, mint) { let payload = []; let block = parseInt(id.split(":")[0]); let txindex = parseInt(id.split(":")[1]); if ((mint != undefined) && mint && txindex != undefined) { encodeToVecV2(TAG_Mint, payload); encodeToVecV2(BigInt(block), payload); encodeToVecV2(TAG_Mint, payload); encodeToVecV2(BigInt(txindex), payload); } if (useDefaultOutput) { encodeToVecV2(TAG_Pointer, payload); encodeToVecV2(BigInt(defaultOutput), payload); } if (payload.length > 80) { throw new Error(JSON.stringify({ errCode: wallet_1.ErrCodeOpreturnExceeds, date: { payloadLenth: payload.length } })); } const opReturnScript = bscript.compile([ops_1.OPS.OP_RETURN, ops_1.OPS.OP_13, Buffer.from(payload)]); return { address: '', amount: 0, omniScript: crypto_lib_1.base.toHex(opReturnScript) }; } exports.buildRuneMainMintOp = buildRuneMainMintOp; //# sourceMappingURL=rune.js.map