UNPKG

@okxweb3/coin-bitcoin

Version:

@ok/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.

202 lines 6.86 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.signature = exports.number = exports.isCanonicalScriptSignature = exports.isDefinedHashType = exports.isCanonicalPubKey = exports.toStack = exports.fromASM = exports.toASM = exports.decompile = exports.compile = exports.isPushOnly = exports.OPS = void 0; const bip66 = __importStar(require("./bip66")); const ops_1 = require("./ops"); Object.defineProperty(exports, "OPS", { enumerable: true, get: function () { return ops_1.OPS; } }); const pushdata = __importStar(require("./push_data")); const scriptNumber = __importStar(require("./script_number")); const scriptSignature = __importStar(require("./script_signature")); const types = __importStar(require("./types")); const { typeforce } = types; const OP_INT_BASE = ops_1.OPS.OP_RESERVED; function isOPInt(value) { return (types.Number(value) && (value === ops_1.OPS.OP_0 || (value >= ops_1.OPS.OP_1 && value <= ops_1.OPS.OP_16) || value === ops_1.OPS.OP_1NEGATE)); } function isPushOnlyChunk(value) { return types.Buffer(value) || isOPInt(value); } function isPushOnly(value) { return types.Array(value) && value.every(isPushOnlyChunk); } exports.isPushOnly = isPushOnly; function asMinimalOP(buffer) { if (buffer.length === 0) return ops_1.OPS.OP_0; if (buffer.length !== 1) return; if (buffer[0] >= 1 && buffer[0] <= 16) return OP_INT_BASE + buffer[0]; if (buffer[0] === 0x81) return ops_1.OPS.OP_1NEGATE; } function chunksIsBuffer(buf) { return Buffer.isBuffer(buf); } function chunksIsArray(buf) { return types.Array(buf); } function singleChunkIsBuffer(buf) { return Buffer.isBuffer(buf); } function compile(chunks) { if (chunksIsBuffer(chunks)) return chunks; typeforce(types.Array, chunks); const bufferSize = chunks.reduce((accum, chunk) => { if (singleChunkIsBuffer(chunk)) { if (chunk.length === 1 && asMinimalOP(chunk) !== undefined) { return accum + 1; } return accum + pushdata.encodingLength(chunk.length) + chunk.length; } return accum + 1; }, 0.0); const buffer = Buffer.allocUnsafe(bufferSize); let offset = 0; chunks.forEach(chunk => { if (singleChunkIsBuffer(chunk)) { const opcode = asMinimalOP(chunk); if (opcode !== undefined) { buffer.writeUInt8(opcode, offset); offset += 1; return; } offset += pushdata.encode(buffer, chunk.length, offset); chunk.copy(buffer, offset); offset += chunk.length; } else { buffer.writeUInt8(chunk, offset); offset += 1; } }); if (offset !== buffer.length) throw new Error('Could not decode chunks'); return buffer; } exports.compile = compile; function decompile(buffer) { if (chunksIsArray(buffer)) return buffer; typeforce(types.Buffer, buffer); const chunks = []; let i = 0; while (i < buffer.length) { const opcode = buffer[i]; if (opcode > ops_1.OPS.OP_0 && opcode <= ops_1.OPS.OP_PUSHDATA4) { const d = pushdata.decode(buffer, i); if (d === null) return null; i += d.size; if (i + d.number > buffer.length) return null; const data = buffer.slice(i, i + d.number); i += d.number; const op = asMinimalOP(data); if (op !== undefined) { chunks.push(op); } else { chunks.push(data); } } else { chunks.push(opcode); i += 1; } } return chunks; } exports.decompile = decompile; function toASM(chunks) { if (chunksIsBuffer(chunks)) { chunks = decompile(chunks); } return chunks .map(chunk => { if (singleChunkIsBuffer(chunk)) { const op = asMinimalOP(chunk); if (op === undefined) return chunk.toString('hex'); chunk = op; } return ops_1.REVERSE_OPS[chunk]; }) .join(' '); } exports.toASM = toASM; function fromASM(asm) { typeforce(types.String, asm); return compile(asm.split(' ').map(chunkStr => { if (ops_1.OPS[chunkStr] !== undefined) return ops_1.OPS[chunkStr]; typeforce(types.Hex, chunkStr); return Buffer.from(chunkStr, 'hex'); })); } exports.fromASM = fromASM; function toStack(chunks) { chunks = decompile(chunks); typeforce(isPushOnly, chunks); return chunks.map(op => { if (singleChunkIsBuffer(op)) return op; if (op === ops_1.OPS.OP_0) return Buffer.allocUnsafe(0); return scriptNumber.encode(op - OP_INT_BASE); }); } exports.toStack = toStack; function isCanonicalPubKey(buffer) { return types.isPoint(buffer); } exports.isCanonicalPubKey = isCanonicalPubKey; function isDefinedHashType(hashType) { let hashTypeMod = hashType; if (hashTypeMod & 0x80) { hashTypeMod = hashTypeMod & ~0x80; } if (hashTypeMod & 0x40) { hashTypeMod = hashTypeMod & ~0x40; } return hashTypeMod > 0x00 && hashTypeMod < 0x04; } exports.isDefinedHashType = isDefinedHashType; function isCanonicalScriptSignature(buffer) { if (!Buffer.isBuffer(buffer)) return false; if (!isDefinedHashType(buffer[buffer.length - 1])) return false; return bip66.check(buffer.slice(0, -1)); } exports.isCanonicalScriptSignature = isCanonicalScriptSignature; exports.number = scriptNumber; exports.signature = scriptSignature; //# sourceMappingURL=script.js.map