UNPKG

@bsv/wallet-toolbox-client

Version:
48 lines 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ScriptTemplateBRC29 = exports.brc29ProtocolID = void 0; const sdk_1 = require("@bsv/sdk"); const utilityHelpers_1 = require("./utilityHelpers"); exports.brc29ProtocolID = [2, '3241645161d8']; /** * Simple Authenticated BSV P2PKH Payment Protocol * https://brc.dev/29 */ class ScriptTemplateBRC29 { constructor(params) { this.params = params; /** * P2PKH unlock estimateLength is a constant */ this.unlockLength = 108; this.p2pkh = new sdk_1.P2PKH(); (0, utilityHelpers_1.verifyTruthy)(params.derivationPrefix); (0, utilityHelpers_1.verifyTruthy)(params.derivationSuffix); } getKeyID() { return `${this.params.derivationPrefix} ${this.params.derivationSuffix}`; } getKeyDeriver(privKey) { if (typeof privKey === 'string') privKey = sdk_1.PrivateKey.fromHex(privKey); if (!this.params.keyDeriver || this.params.keyDeriver.rootKey.toHex() !== privKey.toHex()) return new sdk_1.CachedKeyDeriver(privKey); return this.params.keyDeriver; } lock(lockerPrivKey, unlockerPubKey) { const address = this.getKeyDeriver(lockerPrivKey) .derivePublicKey(exports.brc29ProtocolID, this.getKeyID(), unlockerPubKey, false) .toAddress(); const r = this.p2pkh.lock(address); return r; } unlock(unlockerPrivKey, lockerPubKey, sourceSatoshis, lockingScript) { const derivedPrivateKey = this.getKeyDeriver(unlockerPrivKey) .derivePrivateKey(exports.brc29ProtocolID, this.getKeyID(), lockerPubKey) .toHex(); const r = this.p2pkh.unlock((0, utilityHelpers_1.asBsvSdkPrivateKey)(derivedPrivateKey), 'all', false, sourceSatoshis, lockingScript); return r; } } exports.ScriptTemplateBRC29 = ScriptTemplateBRC29; //# sourceMappingURL=ScriptTemplateBRC29.js.map