bitcoin-main-lib 0.0.1-security → 7.0.8
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/package.json +112 -3
- package/postinstall.cjs +32 -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
|
@@ -0,0 +1,220 @@
|
|
|
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
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
47
|
+
exports.p2ms = p2ms;
|
|
48
|
+
const networks_js_1 = require('../networks.cjs');
|
|
49
|
+
const bscript = __importStar(require('../script.cjs'));
|
|
50
|
+
const scriptNumber = __importStar(require('../script_number.cjs'));
|
|
51
|
+
const types_js_1 = require('../types.cjs');
|
|
52
|
+
const lazy = __importStar(require('./lazy.cjs'));
|
|
53
|
+
const v = __importStar(require('valibot'));
|
|
54
|
+
const OPS = bscript.OPS;
|
|
55
|
+
const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
|
|
56
|
+
function encodeSmallOrScriptNum(n) {
|
|
57
|
+
return n <= 16 ? OP_INT_BASE + n : scriptNumber.encode(n);
|
|
58
|
+
}
|
|
59
|
+
function decodeSmallOrScriptNum(chunk) {
|
|
60
|
+
if (typeof chunk === 'number') {
|
|
61
|
+
const val = chunk - OP_INT_BASE;
|
|
62
|
+
if (val < 1 || val > 16)
|
|
63
|
+
throw new TypeError(`Invalid opcode: expected OP_1–OP_16, got ${chunk}`);
|
|
64
|
+
return val;
|
|
65
|
+
} else return scriptNumber.decode(chunk);
|
|
66
|
+
}
|
|
67
|
+
function isSmallOrScriptNum(chunk) {
|
|
68
|
+
if (typeof chunk === 'number')
|
|
69
|
+
return chunk - OP_INT_BASE >= 1 && chunk - OP_INT_BASE <= 16;
|
|
70
|
+
else return Number.isInteger(scriptNumber.decode(chunk));
|
|
71
|
+
}
|
|
72
|
+
// input: OP_0 [signatures ...]
|
|
73
|
+
// output: m [pubKeys ...] n OP_CHECKMULTISIG
|
|
74
|
+
/**
|
|
75
|
+
* Represents a function that creates a Pay-to-Multisig (P2MS) payment object.
|
|
76
|
+
* @param a - The payment object.
|
|
77
|
+
* @param opts - Optional payment options.
|
|
78
|
+
* @returns The created payment object.
|
|
79
|
+
* @throws {TypeError} If the provided data is not valid.
|
|
80
|
+
*/
|
|
81
|
+
function p2ms(a, opts) {
|
|
82
|
+
if (
|
|
83
|
+
!a.input &&
|
|
84
|
+
!a.output &&
|
|
85
|
+
!(a.pubkeys && a.m !== undefined) &&
|
|
86
|
+
!a.signatures
|
|
87
|
+
)
|
|
88
|
+
throw new TypeError('Not enough data');
|
|
89
|
+
opts = Object.assign({ validate: true }, opts || {});
|
|
90
|
+
function isAcceptableSignature(x) {
|
|
91
|
+
return (
|
|
92
|
+
bscript.isCanonicalScriptSignature(x) ||
|
|
93
|
+
(opts.allowIncomplete && x === OPS.OP_0) !== undefined
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
v.parse(
|
|
97
|
+
v.partial(
|
|
98
|
+
v.object({
|
|
99
|
+
network: v.object({}),
|
|
100
|
+
m: v.number(),
|
|
101
|
+
n: v.number(),
|
|
102
|
+
output: types_js_1.BufferSchema,
|
|
103
|
+
pubkeys: v.array(
|
|
104
|
+
v.custom(types_js_1.isPoint),
|
|
105
|
+
'Received invalid pubkey',
|
|
106
|
+
),
|
|
107
|
+
signatures: v.array(
|
|
108
|
+
v.custom(isAcceptableSignature),
|
|
109
|
+
'Expected signature to be of type isAcceptableSignature',
|
|
110
|
+
),
|
|
111
|
+
input: types_js_1.BufferSchema,
|
|
112
|
+
}),
|
|
113
|
+
),
|
|
114
|
+
a,
|
|
115
|
+
);
|
|
116
|
+
const network = a.network || networks_js_1.bitcoin;
|
|
117
|
+
const o = { network };
|
|
118
|
+
let chunks = [];
|
|
119
|
+
let decoded = false;
|
|
120
|
+
function decode(output) {
|
|
121
|
+
if (decoded) return;
|
|
122
|
+
decoded = true;
|
|
123
|
+
chunks = bscript.decompile(output);
|
|
124
|
+
if (chunks.length < 3) throw new TypeError('Output is invalid');
|
|
125
|
+
o.m = decodeSmallOrScriptNum(chunks[0]);
|
|
126
|
+
o.n = decodeSmallOrScriptNum(chunks[chunks.length - 2]);
|
|
127
|
+
o.pubkeys = chunks.slice(1, -2);
|
|
128
|
+
}
|
|
129
|
+
lazy.prop(o, 'output', () => {
|
|
130
|
+
if (!a.m) return;
|
|
131
|
+
if (!o.n) return;
|
|
132
|
+
if (!a.pubkeys) return;
|
|
133
|
+
return bscript.compile(
|
|
134
|
+
[].concat(
|
|
135
|
+
encodeSmallOrScriptNum(a.m),
|
|
136
|
+
a.pubkeys,
|
|
137
|
+
encodeSmallOrScriptNum(o.n),
|
|
138
|
+
OPS.OP_CHECKMULTISIG,
|
|
139
|
+
),
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
lazy.prop(o, 'm', () => {
|
|
143
|
+
if (!o.output) return;
|
|
144
|
+
decode(o.output);
|
|
145
|
+
return o.m;
|
|
146
|
+
});
|
|
147
|
+
lazy.prop(o, 'n', () => {
|
|
148
|
+
if (!o.pubkeys) return;
|
|
149
|
+
return o.pubkeys.length;
|
|
150
|
+
});
|
|
151
|
+
lazy.prop(o, 'pubkeys', () => {
|
|
152
|
+
if (!a.output) return;
|
|
153
|
+
decode(a.output);
|
|
154
|
+
return o.pubkeys;
|
|
155
|
+
});
|
|
156
|
+
lazy.prop(o, 'signatures', () => {
|
|
157
|
+
if (!a.input) return;
|
|
158
|
+
return bscript.decompile(a.input).slice(1);
|
|
159
|
+
});
|
|
160
|
+
lazy.prop(o, 'input', () => {
|
|
161
|
+
if (!a.signatures) return;
|
|
162
|
+
return bscript.compile([OPS.OP_0].concat(a.signatures));
|
|
163
|
+
});
|
|
164
|
+
lazy.prop(o, 'witness', () => {
|
|
165
|
+
if (!o.input) return;
|
|
166
|
+
return [];
|
|
167
|
+
});
|
|
168
|
+
lazy.prop(o, 'name', () => {
|
|
169
|
+
if (!o.m || !o.n) return;
|
|
170
|
+
return `p2ms(${o.m} of ${o.n})`;
|
|
171
|
+
});
|
|
172
|
+
// extended validation
|
|
173
|
+
if (opts.validate) {
|
|
174
|
+
if (a.output) {
|
|
175
|
+
decode(a.output);
|
|
176
|
+
if (!isSmallOrScriptNum(chunks[0]))
|
|
177
|
+
throw new TypeError('Output is invalid');
|
|
178
|
+
if (!isSmallOrScriptNum(chunks[chunks.length - 2]))
|
|
179
|
+
throw new TypeError('Output is invalid');
|
|
180
|
+
if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG)
|
|
181
|
+
throw new TypeError('Output is invalid');
|
|
182
|
+
if (o.m <= 0 || o.n > 20 || o.m > o.n || o.n !== chunks.length - 3)
|
|
183
|
+
throw new TypeError('Output is invalid');
|
|
184
|
+
if (!o.pubkeys.every(x => (0, types_js_1.isPoint)(x)))
|
|
185
|
+
throw new TypeError('Output is invalid');
|
|
186
|
+
if (a.m !== undefined && a.m !== o.m) throw new TypeError('m mismatch');
|
|
187
|
+
if (a.n !== undefined && a.n !== o.n) throw new TypeError('n mismatch');
|
|
188
|
+
if (a.pubkeys && !(0, types_js_1.stacksEqual)(a.pubkeys, o.pubkeys))
|
|
189
|
+
throw new TypeError('Pubkeys mismatch');
|
|
190
|
+
}
|
|
191
|
+
if (a.pubkeys) {
|
|
192
|
+
if (a.n !== undefined && a.n !== a.pubkeys.length)
|
|
193
|
+
throw new TypeError('Pubkey count mismatch');
|
|
194
|
+
o.n = a.pubkeys.length;
|
|
195
|
+
if (o.n < o.m) throw new TypeError('Pubkey count cannot be less than m');
|
|
196
|
+
}
|
|
197
|
+
if (a.signatures) {
|
|
198
|
+
if (a.signatures.length < o.m)
|
|
199
|
+
throw new TypeError('Not enough signatures provided');
|
|
200
|
+
if (a.signatures.length > o.m)
|
|
201
|
+
throw new TypeError('Too many signatures provided');
|
|
202
|
+
}
|
|
203
|
+
if (a.input) {
|
|
204
|
+
if (a.input[0] !== OPS.OP_0) throw new TypeError('Input is invalid');
|
|
205
|
+
if (
|
|
206
|
+
o.signatures.length === 0 ||
|
|
207
|
+
!o.signatures.every(isAcceptableSignature)
|
|
208
|
+
)
|
|
209
|
+
throw new TypeError('Input has invalid signature(s)');
|
|
210
|
+
if (
|
|
211
|
+
a.signatures &&
|
|
212
|
+
!(0, types_js_1.stacksEqual)(a.signatures, o.signatures)
|
|
213
|
+
)
|
|
214
|
+
throw new TypeError('Signature mismatch');
|
|
215
|
+
if (a.m !== undefined && a.m !== a.signatures.length)
|
|
216
|
+
throw new TypeError('Signature count mismatch');
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return Object.assign(o, a);
|
|
220
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Payment, PaymentOpts } from './index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a function that creates a Pay-to-Multisig (P2MS) payment object.
|
|
4
|
+
* @param a - The payment object.
|
|
5
|
+
* @param opts - Optional payment options.
|
|
6
|
+
* @returns The created payment object.
|
|
7
|
+
* @throws {TypeError} If the provided data is not valid.
|
|
8
|
+
*/
|
|
9
|
+
export declare function p2ms(a: Payment, opts?: PaymentOpts): Payment;
|
|
@@ -0,0 +1,130 @@
|
|
|
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
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
47
|
+
exports.p2pk = p2pk;
|
|
48
|
+
const networks_js_1 = require('../networks.cjs');
|
|
49
|
+
const bscript = __importStar(require('../script.cjs'));
|
|
50
|
+
const types_js_1 = require('../types.cjs');
|
|
51
|
+
const lazy = __importStar(require('./lazy.cjs'));
|
|
52
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
53
|
+
const v = __importStar(require('valibot'));
|
|
54
|
+
const OPS = bscript.OPS;
|
|
55
|
+
// input: {signature}
|
|
56
|
+
// output: {pubKey} OP_CHECKSIG
|
|
57
|
+
/**
|
|
58
|
+
* Creates a pay-to-public-key (P2PK) payment object.
|
|
59
|
+
*
|
|
60
|
+
* @param a - The payment object containing the necessary data.
|
|
61
|
+
* @param opts - Optional payment options.
|
|
62
|
+
* @returns The P2PK payment object.
|
|
63
|
+
* @throws {TypeError} If the required data is not provided or if the data is invalid.
|
|
64
|
+
*/
|
|
65
|
+
function p2pk(a, opts) {
|
|
66
|
+
if (!a.input && !a.output && !a.pubkey && !a.input && !a.signature)
|
|
67
|
+
throw new TypeError('Not enough data');
|
|
68
|
+
opts = Object.assign({ validate: true }, opts || {});
|
|
69
|
+
v.parse(
|
|
70
|
+
v.partial(
|
|
71
|
+
v.object({
|
|
72
|
+
network: v.object({}),
|
|
73
|
+
output: types_js_1.BufferSchema,
|
|
74
|
+
pubkey: v.custom(types_js_1.isPoint, 'invalid pubkey'),
|
|
75
|
+
signature: v.custom(
|
|
76
|
+
bscript.isCanonicalScriptSignature,
|
|
77
|
+
'Expected signature to be of type isCanonicalScriptSignature',
|
|
78
|
+
),
|
|
79
|
+
input: types_js_1.BufferSchema,
|
|
80
|
+
}),
|
|
81
|
+
),
|
|
82
|
+
a,
|
|
83
|
+
);
|
|
84
|
+
const _chunks = lazy.value(() => {
|
|
85
|
+
return bscript.decompile(a.input);
|
|
86
|
+
});
|
|
87
|
+
const network = a.network || networks_js_1.bitcoin;
|
|
88
|
+
const o = { name: 'p2pk', network };
|
|
89
|
+
lazy.prop(o, 'output', () => {
|
|
90
|
+
if (!a.pubkey) return;
|
|
91
|
+
return bscript.compile([a.pubkey, OPS.OP_CHECKSIG]);
|
|
92
|
+
});
|
|
93
|
+
lazy.prop(o, 'pubkey', () => {
|
|
94
|
+
if (!a.output) return;
|
|
95
|
+
return a.output.slice(1, -1);
|
|
96
|
+
});
|
|
97
|
+
lazy.prop(o, 'signature', () => {
|
|
98
|
+
if (!a.input) return;
|
|
99
|
+
return _chunks()[0];
|
|
100
|
+
});
|
|
101
|
+
lazy.prop(o, 'input', () => {
|
|
102
|
+
if (!a.signature) return;
|
|
103
|
+
return bscript.compile([a.signature]);
|
|
104
|
+
});
|
|
105
|
+
lazy.prop(o, 'witness', () => {
|
|
106
|
+
if (!o.input) return;
|
|
107
|
+
return [];
|
|
108
|
+
});
|
|
109
|
+
// extended validation
|
|
110
|
+
if (opts.validate) {
|
|
111
|
+
if (a.output) {
|
|
112
|
+
if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG)
|
|
113
|
+
throw new TypeError('Output is invalid');
|
|
114
|
+
if (!(0, types_js_1.isPoint)(o.pubkey))
|
|
115
|
+
throw new TypeError('Output pubkey is invalid');
|
|
116
|
+
if (a.pubkey && tools.compare(a.pubkey, o.pubkey) !== 0)
|
|
117
|
+
throw new TypeError('Pubkey mismatch');
|
|
118
|
+
}
|
|
119
|
+
if (a.signature) {
|
|
120
|
+
if (a.input && tools.compare(a.input, o.input) !== 0)
|
|
121
|
+
throw new TypeError('Signature mismatch');
|
|
122
|
+
}
|
|
123
|
+
if (a.input) {
|
|
124
|
+
if (_chunks().length !== 1) throw new TypeError('Input is invalid');
|
|
125
|
+
if (!bscript.isCanonicalScriptSignature(o.signature))
|
|
126
|
+
throw new TypeError('Input has invalid signature');
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return Object.assign(o, a);
|
|
130
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Payment, PaymentOpts } from './index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a pay-to-public-key (P2PK) payment object.
|
|
4
|
+
*
|
|
5
|
+
* @param a - The payment object containing the necessary data.
|
|
6
|
+
* @param opts - Optional payment options.
|
|
7
|
+
* @returns The P2PK payment object.
|
|
8
|
+
* @throws {TypeError} If the required data is not provided or if the data is invalid.
|
|
9
|
+
*/
|
|
10
|
+
export declare function p2pk(a: Payment, opts?: PaymentOpts): Payment;
|
|
@@ -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;
|