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

77 lines 2.82 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.encode = exports.decode = void 0; const bip66 = __importStar(require("./bip66")); const types = __importStar(require("./types")); const { typeforce } = types; const ZERO = Buffer.alloc(1, 0); function toDER(x) { let i = 0; while (x[i] === 0) ++i; if (i === x.length) return ZERO; x = x.slice(i); if (x[0] & 0x80) return Buffer.concat([ZERO, x], 1 + x.length); return x; } function fromDER(x) { if (x[0] === 0x00) x = x.slice(1); const buffer = Buffer.alloc(32, 0); const bstart = Math.max(0, 32 - x.length); x.copy(buffer, bstart); return buffer; } function decode(buffer) { const hashType = buffer.readUInt8(buffer.length - 1); const hashTypeMod = hashType & ~0xc0; if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType); const decoded = bip66.decode(buffer.slice(0, -1)); const r = fromDER(decoded.r); const s = fromDER(decoded.s); const signature = Buffer.concat([r, s], 64); return { signature, hashType }; } exports.decode = decode; function encode(signature, hashType) { typeforce({ signature: types.BufferN(64), hashType: types.UInt8, }, { signature, hashType }); const hashTypeMod = hashType & ~0xc0; if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType); const hashTypeBuffer = Buffer.allocUnsafe(1); hashTypeBuffer.writeUInt8(hashType, 0); const r = toDER(signature.slice(0, 32)); const s = toDER(signature.slice(32, 64)); return Buffer.concat([bip66.encode(r, s), hashTypeBuffer]); } exports.encode = encode; //# sourceMappingURL=script_signature.js.map