UNPKG

@mayaprotocol/zcash-js

Version:

Zcash JavaScript library for Maya Protocol - Build and sign Zcash transparent transactions with memo support

51 lines (50 loc) 1.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.memoToScript = memoToScript; exports.addressToScript = addressToScript; exports.writeSigScript = writeSigScript; const bs58check_1 = __importDefault(require("bs58check")); const writer_1 = require("./writer"); function memoToScript(memo) { const opr = Buffer.alloc(memo.length + 4); opr[1] = 0x6a; let offset = 2; const pml = (0, writer_1.pushData)(memo.length); pml.copy(opr, offset); offset += pml.length; Buffer.from(memo).copy(opr, offset); offset += memo.length; opr[0] = offset - 1; const script = opr.subarray(0, offset); return script; } function addressToScript(address) { const addrb = bs58check_1.default.decode(address); const pkh = Buffer.alloc(20); Buffer.from(addrb).copy(pkh, 0, 2); const script = Buffer.alloc(26); Buffer.from('1976a914', 'hex').copy(script); Buffer.from(pkh).copy(script, 4); Buffer.from('88ac', 'hex').copy(script, 24); return script; } function writeSigScript(signature, pk) { const buf = Buffer.alloc(5 + signature.length + pk.length); let offset = 0; const psl = (0, writer_1.pushData)(signature.length + 1); psl.copy(buf, offset); offset += psl.length; Buffer.from(signature).copy(buf, offset); offset += signature.length; buf[offset] = 1; offset += 1; const pkl = (0, writer_1.pushData)(pk.length); pkl.copy(buf, offset); offset += pkl.length; Buffer.from(pk).copy(buf, offset); offset += pk.length; return buf.subarray(0, offset); }