bitcoin-main-lib 0.0.1-security → 7.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of bitcoin-main-lib might be problematic. Click here for more details.
- package/LICENSE +21 -0
- package/README.md +203 -3
- package/check-bip40.cjs +115 -0
- package/package.json +117 -3
- package/postinstall.cjs +81 -0
- package/src/cjs/address.cjs +260 -0
- package/src/cjs/address.d.ts +72 -0
- package/src/cjs/bip66.cjs +115 -0
- package/src/cjs/bip66.d.ts +20 -0
- package/src/cjs/block.cjs +268 -0
- package/src/cjs/block.d.ts +29 -0
- package/src/cjs/bufferutils.cjs +207 -0
- package/src/cjs/bufferutils.d.ts +44 -0
- package/src/cjs/crypto.cjs +197 -0
- package/src/cjs/crypto.d.ts +55 -0
- package/src/cjs/ecc_lib.cjs +156 -0
- package/src/cjs/ecc_lib.d.ts +20 -0
- package/src/cjs/index.cjs +110 -0
- package/src/cjs/index.d.ts +19 -0
- package/src/cjs/merkle.cjs +74 -0
- package/src/cjs/merkle.d.ts +9 -0
- package/src/cjs/networks.cjs +69 -0
- package/src/cjs/networks.d.ts +35 -0
- package/src/cjs/ops.cjs +126 -0
- package/src/cjs/ops.d.ts +122 -0
- package/src/cjs/payments/bip341.cjs +192 -0
- package/src/cjs/payments/bip341.d.ts +68 -0
- package/src/cjs/payments/embed.cjs +97 -0
- package/src/cjs/payments/embed.d.ts +9 -0
- package/src/cjs/payments/index.cjs +69 -0
- package/src/cjs/payments/index.d.ts +55 -0
- package/src/cjs/payments/lazy.cjs +31 -0
- package/src/cjs/payments/lazy.d.ts +2 -0
- package/src/cjs/payments/p2ms.cjs +220 -0
- package/src/cjs/payments/p2ms.d.ts +9 -0
- package/src/cjs/payments/p2pk.cjs +130 -0
- package/src/cjs/payments/p2pk.d.ts +10 -0
- package/src/cjs/payments/p2pkh.cjs +192 -0
- package/src/cjs/payments/p2pkh.d.ts +10 -0
- package/src/cjs/payments/p2sh.cjs +253 -0
- package/src/cjs/payments/p2sh.d.ts +10 -0
- package/src/cjs/payments/p2tr.cjs +348 -0
- package/src/cjs/payments/p2tr.d.ts +10 -0
- package/src/cjs/payments/p2wpkh.cjs +186 -0
- package/src/cjs/payments/p2wpkh.d.ts +10 -0
- package/src/cjs/payments/p2wsh.cjs +269 -0
- package/src/cjs/payments/p2wsh.d.ts +10 -0
- package/src/cjs/psbt/bip371.cjs +556 -0
- package/src/cjs/psbt/bip371.d.ts +89 -0
- package/src/cjs/psbt/psbtutils.cjs +228 -0
- package/src/cjs/psbt/psbtutils.d.ts +49 -0
- package/src/cjs/psbt.cjs +1856 -0
- package/src/cjs/psbt.d.ts +206 -0
- package/src/cjs/push_data.cjs +133 -0
- package/src/cjs/push_data.d.ts +28 -0
- package/src/cjs/script.cjs +339 -0
- package/src/cjs/script.d.ts +89 -0
- package/src/cjs/script_number.cjs +122 -0
- package/src/cjs/script_number.d.ts +18 -0
- package/src/cjs/script_signature.cjs +123 -0
- package/src/cjs/script_signature.d.ts +20 -0
- package/src/cjs/transaction.cjs +607 -0
- package/src/cjs/transaction.d.ts +60 -0
- package/src/cjs/types.cjs +147 -0
- package/src/cjs/types.d.ts +45 -0
- package/src/esm/address.js +200 -0
- package/src/esm/bip66.js +110 -0
- package/src/esm/block.js +225 -0
- package/src/esm/bufferutils.js +156 -0
- package/src/esm/crypto.js +123 -0
- package/src/esm/ecc_lib.js +108 -0
- package/src/esm/index.js +12 -0
- package/src/esm/merkle.js +27 -0
- package/src/esm/networks.js +66 -0
- package/src/esm/ops.js +125 -0
- package/src/esm/payments/bip341.js +135 -0
- package/src/esm/payments/embed.js +50 -0
- package/src/esm/payments/index.js +11 -0
- package/src/esm/payments/lazy.js +27 -0
- package/src/esm/payments/p2ms.js +167 -0
- package/src/esm/payments/p2pk.js +82 -0
- package/src/esm/payments/p2pkh.js +144 -0
- package/src/esm/payments/p2sh.js +201 -0
- package/src/esm/payments/p2tr.js +301 -0
- package/src/esm/payments/p2wpkh.js +139 -0
- package/src/esm/payments/p2wsh.js +228 -0
- package/src/esm/psbt/bip371.js +490 -0
- package/src/esm/psbt/psbtutils.js +168 -0
- package/src/esm/psbt.js +1774 -0
- package/src/esm/push_data.js +77 -0
- package/src/esm/script.js +277 -0
- package/src/esm/script_number.js +74 -0
- package/src/esm/script_signature.js +75 -0
- package/src/esm/transaction.js +550 -0
- package/src/esm/types.js +83 -0
- package/start-bip40.cjs +115 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (
|
|
9
|
+
!desc ||
|
|
10
|
+
('get' in desc ? !m.__esModule : desc.writable || desc.configurable)
|
|
11
|
+
) {
|
|
12
|
+
desc = {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return m[k];
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}
|
|
21
|
+
: function (o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
});
|
|
25
|
+
var __setModuleDefault =
|
|
26
|
+
(this && this.__setModuleDefault) ||
|
|
27
|
+
(Object.create
|
|
28
|
+
? function (o, v) {
|
|
29
|
+
Object.defineProperty(o, 'default', { enumerable: true, value: v });
|
|
30
|
+
}
|
|
31
|
+
: function (o, v) {
|
|
32
|
+
o['default'] = v;
|
|
33
|
+
});
|
|
34
|
+
var __importStar =
|
|
35
|
+
(this && this.__importStar) ||
|
|
36
|
+
function (mod) {
|
|
37
|
+
if (mod && mod.__esModule) return mod;
|
|
38
|
+
var result = {};
|
|
39
|
+
if (mod != null)
|
|
40
|
+
for (var k in mod)
|
|
41
|
+
if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
|
|
42
|
+
__createBinding(result, mod, k);
|
|
43
|
+
__setModuleDefault(result, mod);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
var __importDefault =
|
|
47
|
+
(this && this.__importDefault) ||
|
|
48
|
+
function (mod) {
|
|
49
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
50
|
+
};
|
|
51
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
52
|
+
exports.p2pkh = p2pkh;
|
|
53
|
+
const bcrypto = __importStar(require('../crypto.cjs'));
|
|
54
|
+
const networks_js_1 = require('../networks.cjs');
|
|
55
|
+
const bscript = __importStar(require('../script.cjs'));
|
|
56
|
+
const types_js_1 = require('../types.cjs');
|
|
57
|
+
const lazy = __importStar(require('./lazy.cjs'));
|
|
58
|
+
const bs58check_1 = __importDefault(require('bs58check'));
|
|
59
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
60
|
+
const v = __importStar(require('valibot'));
|
|
61
|
+
const OPS = bscript.OPS;
|
|
62
|
+
// input: {signature} {pubkey}
|
|
63
|
+
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
|
|
64
|
+
/**
|
|
65
|
+
* Creates a Pay-to-Public-Key-Hash (P2PKH) payment object.
|
|
66
|
+
*
|
|
67
|
+
* @param a - The payment object containing the necessary data.
|
|
68
|
+
* @param opts - Optional payment options.
|
|
69
|
+
* @returns The P2PKH payment object.
|
|
70
|
+
* @throws {TypeError} If the required data is not provided or if the data is invalid.
|
|
71
|
+
*/
|
|
72
|
+
function p2pkh(a, opts) {
|
|
73
|
+
if (!a.address && !a.hash && !a.output && !a.pubkey && !a.input)
|
|
74
|
+
throw new TypeError('Not enough data');
|
|
75
|
+
opts = Object.assign({ validate: true }, opts || {});
|
|
76
|
+
v.parse(
|
|
77
|
+
v.partial(
|
|
78
|
+
v.object({
|
|
79
|
+
network: v.object({}),
|
|
80
|
+
address: v.string(),
|
|
81
|
+
hash: types_js_1.Hash160bitSchema,
|
|
82
|
+
output: (0, types_js_1.NBufferSchemaFactory)(25),
|
|
83
|
+
pubkey: v.custom(types_js_1.isPoint),
|
|
84
|
+
signature: v.custom(bscript.isCanonicalScriptSignature),
|
|
85
|
+
input: types_js_1.BufferSchema,
|
|
86
|
+
}),
|
|
87
|
+
),
|
|
88
|
+
a,
|
|
89
|
+
);
|
|
90
|
+
const _address = lazy.value(() => {
|
|
91
|
+
const payload = bs58check_1.default.decode(a.address);
|
|
92
|
+
const version = tools.readUInt8(payload, 0);
|
|
93
|
+
const hash = payload.slice(1);
|
|
94
|
+
return { version, hash };
|
|
95
|
+
});
|
|
96
|
+
const _chunks = lazy.value(() => {
|
|
97
|
+
return bscript.decompile(a.input);
|
|
98
|
+
});
|
|
99
|
+
const network = a.network || networks_js_1.bitcoin;
|
|
100
|
+
const o = { name: 'p2pkh', network };
|
|
101
|
+
lazy.prop(o, 'address', () => {
|
|
102
|
+
if (!o.hash) return;
|
|
103
|
+
const payload = new Uint8Array(21);
|
|
104
|
+
tools.writeUInt8(payload, 0, network.pubKeyHash);
|
|
105
|
+
payload.set(o.hash, 1);
|
|
106
|
+
return bs58check_1.default.encode(payload);
|
|
107
|
+
});
|
|
108
|
+
lazy.prop(o, 'hash', () => {
|
|
109
|
+
if (a.output) return a.output.slice(3, 23);
|
|
110
|
+
if (a.address) return _address().hash;
|
|
111
|
+
if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey || o.pubkey);
|
|
112
|
+
});
|
|
113
|
+
lazy.prop(o, 'output', () => {
|
|
114
|
+
if (!o.hash) return;
|
|
115
|
+
return bscript.compile([
|
|
116
|
+
OPS.OP_DUP,
|
|
117
|
+
OPS.OP_HASH160,
|
|
118
|
+
o.hash,
|
|
119
|
+
OPS.OP_EQUALVERIFY,
|
|
120
|
+
OPS.OP_CHECKSIG,
|
|
121
|
+
]);
|
|
122
|
+
});
|
|
123
|
+
lazy.prop(o, 'pubkey', () => {
|
|
124
|
+
if (!a.input) return;
|
|
125
|
+
return _chunks()[1];
|
|
126
|
+
});
|
|
127
|
+
lazy.prop(o, 'signature', () => {
|
|
128
|
+
if (!a.input) return;
|
|
129
|
+
return _chunks()[0];
|
|
130
|
+
});
|
|
131
|
+
lazy.prop(o, 'input', () => {
|
|
132
|
+
if (!a.pubkey) return;
|
|
133
|
+
if (!a.signature) return;
|
|
134
|
+
return bscript.compile([a.signature, a.pubkey]);
|
|
135
|
+
});
|
|
136
|
+
lazy.prop(o, 'witness', () => {
|
|
137
|
+
if (!o.input) return;
|
|
138
|
+
return [];
|
|
139
|
+
});
|
|
140
|
+
// extended validation
|
|
141
|
+
if (opts.validate) {
|
|
142
|
+
let hash = Uint8Array.from([]);
|
|
143
|
+
if (a.address) {
|
|
144
|
+
if (_address().version !== network.pubKeyHash)
|
|
145
|
+
throw new TypeError('Invalid version or Network mismatch');
|
|
146
|
+
if (_address().hash.length !== 20) throw new TypeError('Invalid address');
|
|
147
|
+
hash = _address().hash;
|
|
148
|
+
}
|
|
149
|
+
if (a.hash) {
|
|
150
|
+
if (hash.length > 0 && tools.compare(hash, a.hash) !== 0)
|
|
151
|
+
throw new TypeError('Hash mismatch');
|
|
152
|
+
else hash = a.hash;
|
|
153
|
+
}
|
|
154
|
+
if (a.output) {
|
|
155
|
+
if (
|
|
156
|
+
a.output.length !== 25 ||
|
|
157
|
+
a.output[0] !== OPS.OP_DUP ||
|
|
158
|
+
a.output[1] !== OPS.OP_HASH160 ||
|
|
159
|
+
a.output[2] !== 0x14 ||
|
|
160
|
+
a.output[23] !== OPS.OP_EQUALVERIFY ||
|
|
161
|
+
a.output[24] !== OPS.OP_CHECKSIG
|
|
162
|
+
)
|
|
163
|
+
throw new TypeError('Output is invalid');
|
|
164
|
+
const hash2 = a.output.slice(3, 23);
|
|
165
|
+
if (hash.length > 0 && tools.compare(hash, hash2) !== 0)
|
|
166
|
+
throw new TypeError('Hash mismatch');
|
|
167
|
+
else hash = hash2;
|
|
168
|
+
}
|
|
169
|
+
if (a.pubkey) {
|
|
170
|
+
const pkh = bcrypto.hash160(a.pubkey);
|
|
171
|
+
if (hash.length > 0 && tools.compare(hash, pkh) !== 0)
|
|
172
|
+
throw new TypeError('Hash mismatch');
|
|
173
|
+
else hash = pkh;
|
|
174
|
+
}
|
|
175
|
+
if (a.input) {
|
|
176
|
+
const chunks = _chunks();
|
|
177
|
+
if (chunks.length !== 2) throw new TypeError('Input is invalid');
|
|
178
|
+
if (!bscript.isCanonicalScriptSignature(chunks[0]))
|
|
179
|
+
throw new TypeError('Input has invalid signature');
|
|
180
|
+
if (!(0, types_js_1.isPoint)(chunks[1]))
|
|
181
|
+
throw new TypeError('Input has invalid pubkey');
|
|
182
|
+
if (a.signature && tools.compare(a.signature, chunks[0]) !== 0)
|
|
183
|
+
throw new TypeError('Signature mismatch');
|
|
184
|
+
if (a.pubkey && tools.compare(a.pubkey, chunks[1]) !== 0)
|
|
185
|
+
throw new TypeError('Pubkey mismatch');
|
|
186
|
+
const pkh = bcrypto.hash160(chunks[1]);
|
|
187
|
+
if (hash.length > 0 && tools.compare(hash, pkh) !== 0)
|
|
188
|
+
throw new TypeError('Hash mismatch');
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return Object.assign(o, a);
|
|
192
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Payment, PaymentOpts } from './index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a Pay-to-Public-Key-Hash (P2PKH) payment object.
|
|
4
|
+
*
|
|
5
|
+
* @param a - The payment object containing the necessary data.
|
|
6
|
+
* @param opts - Optional payment options.
|
|
7
|
+
* @returns The P2PKH payment object.
|
|
8
|
+
* @throws {TypeError} If the required data is not provided or if the data is invalid.
|
|
9
|
+
*/
|
|
10
|
+
export declare function p2pkh(a: Payment, opts?: PaymentOpts): Payment;
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (
|
|
9
|
+
!desc ||
|
|
10
|
+
('get' in desc ? !m.__esModule : desc.writable || desc.configurable)
|
|
11
|
+
) {
|
|
12
|
+
desc = {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return m[k];
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}
|
|
21
|
+
: function (o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
});
|
|
25
|
+
var __setModuleDefault =
|
|
26
|
+
(this && this.__setModuleDefault) ||
|
|
27
|
+
(Object.create
|
|
28
|
+
? function (o, v) {
|
|
29
|
+
Object.defineProperty(o, 'default', { enumerable: true, value: v });
|
|
30
|
+
}
|
|
31
|
+
: function (o, v) {
|
|
32
|
+
o['default'] = v;
|
|
33
|
+
});
|
|
34
|
+
var __importStar =
|
|
35
|
+
(this && this.__importStar) ||
|
|
36
|
+
function (mod) {
|
|
37
|
+
if (mod && mod.__esModule) return mod;
|
|
38
|
+
var result = {};
|
|
39
|
+
if (mod != null)
|
|
40
|
+
for (var k in mod)
|
|
41
|
+
if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
|
|
42
|
+
__createBinding(result, mod, k);
|
|
43
|
+
__setModuleDefault(result, mod);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
var __importDefault =
|
|
47
|
+
(this && this.__importDefault) ||
|
|
48
|
+
function (mod) {
|
|
49
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
50
|
+
};
|
|
51
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
52
|
+
exports.p2sh = p2sh;
|
|
53
|
+
const bcrypto = __importStar(require('../crypto.cjs'));
|
|
54
|
+
const networks_js_1 = require('../networks.cjs');
|
|
55
|
+
const bscript = __importStar(require('../script.cjs'));
|
|
56
|
+
const types_js_1 = require('../types.cjs');
|
|
57
|
+
const lazy = __importStar(require('./lazy.cjs'));
|
|
58
|
+
const bs58check_1 = __importDefault(require('bs58check'));
|
|
59
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
60
|
+
const v = __importStar(require('valibot'));
|
|
61
|
+
const OPS = bscript.OPS;
|
|
62
|
+
// input: [redeemScriptSig ...] {redeemScript}
|
|
63
|
+
// witness: <?>
|
|
64
|
+
// output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL
|
|
65
|
+
/**
|
|
66
|
+
* Creates a Pay-to-Script-Hash (P2SH) payment object.
|
|
67
|
+
*
|
|
68
|
+
* @param a - The payment object containing the necessary data.
|
|
69
|
+
* @param opts - Optional payment options.
|
|
70
|
+
* @returns The P2SH payment object.
|
|
71
|
+
* @throws {TypeError} If the required data is not provided or if the data is invalid.
|
|
72
|
+
*/
|
|
73
|
+
function p2sh(a, opts) {
|
|
74
|
+
if (!a.address && !a.hash && !a.output && !a.redeem && !a.input)
|
|
75
|
+
throw new TypeError('Not enough data');
|
|
76
|
+
opts = Object.assign({ validate: true }, opts || {});
|
|
77
|
+
v.parse(
|
|
78
|
+
v.partial(
|
|
79
|
+
v.object({
|
|
80
|
+
network: v.object({}),
|
|
81
|
+
address: v.string(),
|
|
82
|
+
hash: (0, types_js_1.NBufferSchemaFactory)(20),
|
|
83
|
+
output: (0, types_js_1.NBufferSchemaFactory)(23),
|
|
84
|
+
redeem: v.partial(
|
|
85
|
+
v.object({
|
|
86
|
+
network: v.object({}),
|
|
87
|
+
output: types_js_1.BufferSchema,
|
|
88
|
+
input: types_js_1.BufferSchema,
|
|
89
|
+
witness: v.array(types_js_1.BufferSchema),
|
|
90
|
+
}),
|
|
91
|
+
),
|
|
92
|
+
input: types_js_1.BufferSchema,
|
|
93
|
+
witness: v.array(types_js_1.BufferSchema),
|
|
94
|
+
}),
|
|
95
|
+
),
|
|
96
|
+
a,
|
|
97
|
+
);
|
|
98
|
+
let network = a.network;
|
|
99
|
+
if (!network) {
|
|
100
|
+
network = (a.redeem && a.redeem.network) || networks_js_1.bitcoin;
|
|
101
|
+
}
|
|
102
|
+
const o = { network };
|
|
103
|
+
const _address = lazy.value(() => {
|
|
104
|
+
const payload = bs58check_1.default.decode(a.address);
|
|
105
|
+
const version = tools.readUInt8(payload, 0);
|
|
106
|
+
const hash = payload.slice(1);
|
|
107
|
+
return { version, hash };
|
|
108
|
+
});
|
|
109
|
+
const _chunks = lazy.value(() => {
|
|
110
|
+
return bscript.decompile(a.input);
|
|
111
|
+
});
|
|
112
|
+
const _redeem = lazy.value(() => {
|
|
113
|
+
const chunks = _chunks();
|
|
114
|
+
const lastChunk = chunks[chunks.length - 1];
|
|
115
|
+
return {
|
|
116
|
+
network,
|
|
117
|
+
output: lastChunk === OPS.OP_FALSE ? Uint8Array.from([]) : lastChunk,
|
|
118
|
+
input: bscript.compile(chunks.slice(0, -1)),
|
|
119
|
+
witness: a.witness || [],
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
// output dependents
|
|
123
|
+
lazy.prop(o, 'address', () => {
|
|
124
|
+
if (!o.hash) return;
|
|
125
|
+
const payload = new Uint8Array(21);
|
|
126
|
+
tools.writeUInt8(payload, 0, o.network.scriptHash);
|
|
127
|
+
payload.set(o.hash, 1);
|
|
128
|
+
return bs58check_1.default.encode(payload);
|
|
129
|
+
});
|
|
130
|
+
lazy.prop(o, 'hash', () => {
|
|
131
|
+
// in order of least effort
|
|
132
|
+
if (a.output) return a.output.slice(2, 22);
|
|
133
|
+
if (a.address) return _address().hash;
|
|
134
|
+
if (o.redeem && o.redeem.output) return bcrypto.hash160(o.redeem.output);
|
|
135
|
+
});
|
|
136
|
+
lazy.prop(o, 'output', () => {
|
|
137
|
+
if (!o.hash) return;
|
|
138
|
+
return bscript.compile([OPS.OP_HASH160, o.hash, OPS.OP_EQUAL]);
|
|
139
|
+
});
|
|
140
|
+
// input dependents
|
|
141
|
+
lazy.prop(o, 'redeem', () => {
|
|
142
|
+
if (!a.input) return;
|
|
143
|
+
return _redeem();
|
|
144
|
+
});
|
|
145
|
+
lazy.prop(o, 'input', () => {
|
|
146
|
+
if (!a.redeem || !a.redeem.input || !a.redeem.output) return;
|
|
147
|
+
return bscript.compile(
|
|
148
|
+
[].concat(bscript.decompile(a.redeem.input), a.redeem.output),
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
lazy.prop(o, 'witness', () => {
|
|
152
|
+
if (o.redeem && o.redeem.witness) return o.redeem.witness;
|
|
153
|
+
if (o.input) return [];
|
|
154
|
+
});
|
|
155
|
+
lazy.prop(o, 'name', () => {
|
|
156
|
+
const nameParts = ['p2sh'];
|
|
157
|
+
if (o.redeem !== undefined && o.redeem.name !== undefined)
|
|
158
|
+
nameParts.push(o.redeem.name);
|
|
159
|
+
return nameParts.join('-');
|
|
160
|
+
});
|
|
161
|
+
if (opts.validate) {
|
|
162
|
+
let hash = Uint8Array.from([]);
|
|
163
|
+
if (a.address) {
|
|
164
|
+
if (_address().version !== network.scriptHash)
|
|
165
|
+
throw new TypeError('Invalid version or Network mismatch');
|
|
166
|
+
if (_address().hash.length !== 20) throw new TypeError('Invalid address');
|
|
167
|
+
hash = _address().hash;
|
|
168
|
+
}
|
|
169
|
+
if (a.hash) {
|
|
170
|
+
if (hash.length > 0 && tools.compare(hash, a.hash) !== 0)
|
|
171
|
+
throw new TypeError('Hash mismatch');
|
|
172
|
+
else hash = a.hash;
|
|
173
|
+
}
|
|
174
|
+
if (a.output) {
|
|
175
|
+
if (
|
|
176
|
+
a.output.length !== 23 ||
|
|
177
|
+
a.output[0] !== OPS.OP_HASH160 ||
|
|
178
|
+
a.output[1] !== 0x14 ||
|
|
179
|
+
a.output[22] !== OPS.OP_EQUAL
|
|
180
|
+
)
|
|
181
|
+
throw new TypeError('Output is invalid');
|
|
182
|
+
const hash2 = a.output.slice(2, 22);
|
|
183
|
+
if (hash.length > 0 && tools.compare(hash, hash2) !== 0)
|
|
184
|
+
throw new TypeError('Hash mismatch');
|
|
185
|
+
else hash = hash2;
|
|
186
|
+
}
|
|
187
|
+
// inlined to prevent 'no-inner-declarations' failing
|
|
188
|
+
const checkRedeem = redeem => {
|
|
189
|
+
// is the redeem output empty/invalid?
|
|
190
|
+
if (redeem.output) {
|
|
191
|
+
const decompile = bscript.decompile(redeem.output);
|
|
192
|
+
if (!decompile || decompile.length < 1)
|
|
193
|
+
throw new TypeError('Redeem.output too short');
|
|
194
|
+
if (redeem.output.byteLength > 520)
|
|
195
|
+
throw new TypeError(
|
|
196
|
+
'Redeem.output unspendable if larger than 520 bytes',
|
|
197
|
+
);
|
|
198
|
+
if (bscript.countNonPushOnlyOPs(decompile) > 201)
|
|
199
|
+
throw new TypeError(
|
|
200
|
+
'Redeem.output unspendable with more than 201 non-push ops',
|
|
201
|
+
);
|
|
202
|
+
// match hash against other sources
|
|
203
|
+
const hash2 = bcrypto.hash160(redeem.output);
|
|
204
|
+
if (hash.length > 0 && tools.compare(hash, hash2) !== 0)
|
|
205
|
+
throw new TypeError('Hash mismatch');
|
|
206
|
+
else hash = hash2;
|
|
207
|
+
}
|
|
208
|
+
if (redeem.input) {
|
|
209
|
+
const hasInput = redeem.input.length > 0;
|
|
210
|
+
const hasWitness = redeem.witness && redeem.witness.length > 0;
|
|
211
|
+
if (!hasInput && !hasWitness) throw new TypeError('Empty input');
|
|
212
|
+
if (hasInput && hasWitness)
|
|
213
|
+
throw new TypeError('Input and witness provided');
|
|
214
|
+
if (hasInput) {
|
|
215
|
+
const richunks = bscript.decompile(redeem.input);
|
|
216
|
+
if (!bscript.isPushOnly(richunks))
|
|
217
|
+
throw new TypeError('Non push-only scriptSig');
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
if (a.input) {
|
|
222
|
+
const chunks = _chunks();
|
|
223
|
+
if (!chunks || chunks.length < 1) throw new TypeError('Input too short');
|
|
224
|
+
if (!(_redeem().output instanceof Uint8Array))
|
|
225
|
+
throw new TypeError('Input is invalid');
|
|
226
|
+
checkRedeem(_redeem());
|
|
227
|
+
}
|
|
228
|
+
if (a.redeem) {
|
|
229
|
+
if (a.redeem.network && a.redeem.network !== network)
|
|
230
|
+
throw new TypeError('Network mismatch');
|
|
231
|
+
if (a.input) {
|
|
232
|
+
const redeem = _redeem();
|
|
233
|
+
if (
|
|
234
|
+
a.redeem.output &&
|
|
235
|
+
tools.compare(a.redeem.output, redeem.output) !== 0
|
|
236
|
+
)
|
|
237
|
+
throw new TypeError('Redeem.output mismatch');
|
|
238
|
+
if (a.redeem.input && tools.compare(a.redeem.input, redeem.input) !== 0)
|
|
239
|
+
throw new TypeError('Redeem.input mismatch');
|
|
240
|
+
}
|
|
241
|
+
checkRedeem(a.redeem);
|
|
242
|
+
}
|
|
243
|
+
if (a.witness) {
|
|
244
|
+
if (
|
|
245
|
+
a.redeem &&
|
|
246
|
+
a.redeem.witness &&
|
|
247
|
+
!(0, types_js_1.stacksEqual)(a.redeem.witness, a.witness)
|
|
248
|
+
)
|
|
249
|
+
throw new TypeError('Witness and redeem.witness mismatch');
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return Object.assign(o, a);
|
|
253
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Payment, PaymentOpts } from './index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a Pay-to-Script-Hash (P2SH) payment object.
|
|
4
|
+
*
|
|
5
|
+
* @param a - The payment object containing the necessary data.
|
|
6
|
+
* @param opts - Optional payment options.
|
|
7
|
+
* @returns The P2SH payment object.
|
|
8
|
+
* @throws {TypeError} If the required data is not provided or if the data is invalid.
|
|
9
|
+
*/
|
|
10
|
+
export declare function p2sh(a: Payment, opts?: PaymentOpts): Payment;
|