liquidjs-lib
Version:
Client-side Liquid JavaScript library
317 lines (316 loc) • 10.2 kB
JavaScript
;
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.p2wsh = void 0;
const baddress = __importStar(require('../address'));
const bcrypto = __importStar(require('../crypto'));
const networks_1 = require('../networks');
const bscript = __importStar(require('../script'));
const types_1 = require('../types');
const lazy = __importStar(require('./lazy'));
const bech32_1 = require('bech32');
const OPS = bscript.OPS;
const EMPTY_BUFFER = Buffer.alloc(0);
function stacksEqual(a, b) {
if (a.length !== b.length) return false;
return a.every((x, i) => {
return x.equals(b[i]);
});
}
function chunkHasUncompressedPubkey(chunk) {
if (
Buffer.isBuffer(chunk) &&
chunk.length === 65 &&
chunk[0] === 0x04 &&
(0, types_1.isPoint)(chunk)
) {
return true;
} else {
return false;
}
}
// input: <>
// witness: [redeemScriptSig ...] {redeemScript}
// output: OP_0 {sha256(redeemScript)}
function p2wsh(a, opts) {
if (
!a.address &&
!a.hash &&
!a.output &&
!a.redeem &&
!a.witness &&
!a.confidentialAddress
)
throw new TypeError('Not enough data');
opts = Object.assign({ validate: true }, opts || {});
(0, types_1.typeforce)(
{
network: types_1.typeforce.maybe(types_1.typeforce.Object),
address: types_1.typeforce.maybe(types_1.typeforce.String),
hash: types_1.typeforce.maybe(types_1.typeforce.BufferN(32)),
output: types_1.typeforce.maybe(types_1.typeforce.BufferN(34)),
redeem: types_1.typeforce.maybe({
input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
network: types_1.typeforce.maybe(types_1.typeforce.Object),
output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
witness: types_1.typeforce.maybe(
types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
),
}),
input: types_1.typeforce.maybe(types_1.typeforce.BufferN(0)),
witness: types_1.typeforce.maybe(
types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
),
blindkey: types_1.typeforce.maybe(types_1.isPoint),
confidentialAddress: types_1.typeforce.maybe(types_1.typeforce.String),
},
a,
);
let network = a.network;
if (!network) {
network = (a.redeem && a.redeem.network) || networks_1.liquid;
}
const _address = lazy.value(() => {
const result = bech32_1.bech32.decode(a.address);
const version = result.words.shift();
const data = bech32_1.bech32.fromWords(result.words);
return {
version,
prefix: result.prefix,
data: Buffer.from(data),
};
});
const _rchunks = lazy.value(() => {
return bscript.decompile(a.redeem.input);
});
const _confidentialAddress = lazy.value(() => {
const result = baddress.fromBlech32(a.confidentialAddress);
return {
blindingKey: result.pubkey,
unconfidentialAddress: baddress.toBech32(
result.data.slice(2),
result.version,
network.bech32,
),
};
});
const o = { network };
lazy.prop(o, 'address', () => {
if (!o.hash) return;
const words = bech32_1.bech32.toWords(o.hash);
words.unshift(0x00);
return bech32_1.bech32.encode(network.bech32, words);
});
lazy.prop(o, 'hash', () => {
if (a.output) return a.output.slice(2);
if (a.address) return _address().data;
if (o.redeem && o.redeem.output) return bcrypto.sha256(o.redeem.output);
if (a.confidentialAddress) {
const addr = _confidentialAddress().unconfidentialAddress;
return baddress.fromBech32(addr).data;
}
});
lazy.prop(o, 'output', () => {
if (!o.hash) return;
return bscript.compile([OPS.OP_0, o.hash]);
});
lazy.prop(o, 'redeem', () => {
if (!a.witness) return;
return {
output: a.witness[a.witness.length - 1],
input: EMPTY_BUFFER,
witness: a.witness.slice(0, -1),
};
});
lazy.prop(o, 'input', () => {
if (!o.witness) return;
return EMPTY_BUFFER;
});
lazy.prop(o, 'witness', () => {
// transform redeem input to witness stack?
if (
a.redeem &&
a.redeem.input &&
a.redeem.input.length > 0 &&
a.redeem.output &&
a.redeem.output.length > 0
) {
const stack = bscript.toStack(_rchunks());
// assign, and blank the existing input
o.redeem = Object.assign({ witness: stack }, a.redeem);
o.redeem.input = EMPTY_BUFFER;
return [].concat(stack, a.redeem.output);
}
if (!a.redeem) return;
if (!a.redeem.output) return;
if (!a.redeem.witness) return;
return [].concat(a.redeem.witness, a.redeem.output);
});
lazy.prop(o, 'name', () => {
const nameParts = ['p2wsh'];
if (o.redeem !== undefined && o.redeem.name !== undefined)
nameParts.push(o.redeem.name);
return nameParts.join('-');
});
lazy.prop(o, 'blindkey', () => {
if (a.confidentialAddress) return _confidentialAddress().blindingKey;
if (a.blindkey) return a.blindkey;
});
lazy.prop(o, 'confidentialAddress', () => {
if (!o.address) return;
if (!o.blindkey) return;
const res = baddress.fromBech32(o.address);
const data = Buffer.concat([
Buffer.from([res.version, res.data.length]),
res.data,
]);
return baddress.toBlech32(data, o.blindkey, o.network.blech32, 0);
});
// extended validation
if (opts.validate) {
let hash = Buffer.from([]);
let blindkey = Buffer.from([]);
if (a.address) {
if (_address().prefix !== network.bech32)
throw new TypeError('Invalid prefix or Network mismatch');
if (_address().version !== 0x00)
throw new TypeError('Invalid address version');
if (_address().data.length !== 32)
throw new TypeError('Invalid address data');
hash = _address().data;
}
if (a.hash) {
if (hash.length > 0 && !hash.equals(a.hash))
throw new TypeError('Hash mismatch');
else hash = a.hash;
}
if (a.output) {
if (
a.output.length !== 34 ||
a.output[0] !== OPS.OP_0 ||
a.output[1] !== 0x20
)
throw new TypeError('Output is invalid');
const hash2 = a.output.slice(2);
if (hash.length > 0 && !hash.equals(hash2))
throw new TypeError('Hash mismatch');
else hash = hash2;
}
if (a.redeem) {
if (a.redeem.network && a.redeem.network !== network)
throw new TypeError('Network mismatch');
// is there two redeem sources?
if (
a.redeem.input &&
a.redeem.input.length > 0 &&
a.redeem.witness &&
a.redeem.witness.length > 0
)
throw new TypeError('Ambiguous witness source');
// is the redeem output non-empty?
if (a.redeem.output) {
if (bscript.decompile(a.redeem.output).length === 0)
throw new TypeError('Redeem.output is invalid');
// match hash against other sources
const hash2 = bcrypto.sha256(a.redeem.output);
if (hash.length > 0 && !hash.equals(hash2))
throw new TypeError('Hash mismatch');
else hash = hash2;
}
if (a.redeem.input && !bscript.isPushOnly(_rchunks()))
throw new TypeError('Non push-only scriptSig');
if (
a.witness &&
a.redeem.witness &&
!stacksEqual(a.witness, a.redeem.witness)
)
throw new TypeError('Witness and redeem.witness mismatch');
if (
(a.redeem.input && _rchunks().some(chunkHasUncompressedPubkey)) ||
(a.redeem.output &&
(bscript.decompile(a.redeem.output) || []).some(
chunkHasUncompressedPubkey,
))
) {
throw new TypeError(
'redeem.input or redeem.output contains uncompressed pubkey',
);
}
}
if (a.witness && a.witness.length > 0) {
const wScript = a.witness[a.witness.length - 1];
if (a.redeem && a.redeem.output && !a.redeem.output.equals(wScript))
throw new TypeError('Witness and redeem.output mismatch');
if (
a.witness.some(chunkHasUncompressedPubkey) ||
(bscript.decompile(wScript) || []).some(chunkHasUncompressedPubkey)
)
throw new TypeError('Witness contains uncompressed pubkey');
}
if (a.confidentialAddress) {
if (
a.address &&
a.address !== _confidentialAddress().unconfidentialAddress
)
throw new TypeError('Address mismatch');
if (
blindkey.length > 0 &&
!blindkey.equals(_confidentialAddress().blindingKey)
)
throw new TypeError('Blindkey mismatch');
else blindkey = _confidentialAddress().blindingKey;
}
if (a.blindkey) {
if (!(0, types_1.isPoint)(a.blindkey))
throw new TypeError('Blindkey is invalid');
if (blindkey.length > 0 && !blindkey.equals(a.blindkey))
throw new TypeError('Blindkey mismatch');
else blindkey = a.blindkey;
}
}
return Object.assign(o, a);
}
exports.p2wsh = p2wsh;