@btc-vision/bitcoin 6.3.1 → 6.3.2
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.
- package/.babelrc +4 -0
- package/.gitattributes +2 -0
- package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +1 -0
- package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +1 -0
- package/.nyc_output/processinfo/index.json +1 -0
- package/.prettierrc.json +12 -0
- package/CHANGELOG.md +403 -0
- package/CONTRIBUTING.md +83 -0
- package/browser/address.d.ts +16 -0
- package/{src → browser}/bip66.d.ts +6 -7
- package/{src → browser}/block.d.ts +29 -30
- package/{src → browser}/bufferutils.d.ts +34 -54
- package/browser/crypto/crypto.d.ts +1 -0
- package/{src → browser}/crypto.d.ts +13 -18
- package/browser/ecc_lib.d.ts +3 -0
- package/browser/hooks/AdvancedSignatureManager.d.ts +16 -0
- package/{src → browser}/hooks/HookedSigner.d.ts +4 -4
- package/browser/hooks/SignatureManager.d.ts +13 -0
- package/browser/index.d.ts +58 -0
- package/browser/index.js +2 -0
- package/browser/index.js.LICENSE.txt +14 -0
- package/browser/merkle.d.ts +1 -0
- package/browser/networks.d.ts +23 -0
- package/{src → browser}/ops.d.ts +126 -126
- package/browser/payments/bip341.d.ts +23 -0
- package/browser/payments/embed.d.ts +2 -0
- package/browser/payments/index.d.ts +41 -0
- package/{src → browser}/payments/lazy.d.ts +2 -2
- package/browser/payments/p2ms.d.ts +2 -0
- package/browser/payments/p2pk.d.ts +2 -0
- package/browser/payments/p2pkh.d.ts +2 -0
- package/browser/payments/p2sh.d.ts +2 -0
- package/browser/payments/p2tr.d.ts +2 -0
- package/browser/payments/p2wpkh.d.ts +2 -0
- package/browser/payments/p2wsh.d.ts +2 -0
- package/browser/psbt/bip371.d.ts +16 -0
- package/browser/psbt/psbtutils.d.ts +26 -0
- package/{src → browser}/psbt.d.ts +167 -238
- package/browser/push_data.d.ts +7 -0
- package/browser/script.d.ts +17 -0
- package/browser/script_number.d.ts +2 -0
- package/browser/script_signature.d.ts +7 -0
- package/{src → browser}/transaction.d.ts +48 -60
- package/{src → browser}/types.d.ts +37 -54
- package/build/address.d.ts +16 -0
- package/build/address.js +148 -0
- package/build/bip66.d.ts +6 -0
- package/build/bip66.js +99 -0
- package/build/block.d.ts +29 -0
- package/build/block.js +181 -0
- package/build/bufferutils.d.ts +34 -0
- package/build/bufferutils.js +141 -0
- package/build/crypto/crypto.d.ts +1 -0
- package/build/crypto/crypto.js +1 -0
- package/build/crypto.d.ts +13 -0
- package/build/crypto.js +87 -0
- package/build/ecc_lib.d.ts +3 -0
- package/build/ecc_lib.js +61 -0
- package/build/hooks/AdvancedSignatureManager.d.ts +16 -0
- package/build/hooks/AdvancedSignatureManager.js +52 -0
- package/build/hooks/HookedSigner.d.ts +4 -0
- package/build/hooks/HookedSigner.js +64 -0
- package/build/hooks/SignatureManager.d.ts +13 -0
- package/build/hooks/SignatureManager.js +45 -0
- package/build/index.d.ts +58 -0
- package/build/index.js +32 -0
- package/build/merkle.d.ts +1 -0
- package/build/merkle.js +19 -0
- package/build/networks.d.ts +23 -0
- package/build/networks.js +121 -0
- package/build/ops.d.ts +126 -0
- package/{src → build}/ops.js +127 -131
- package/build/payments/bip341.d.ts +23 -0
- package/build/payments/bip341.js +82 -0
- package/build/payments/embed.d.ts +2 -0
- package/build/payments/embed.js +39 -0
- package/build/payments/index.d.ts +41 -0
- package/build/payments/index.js +10 -0
- package/build/payments/lazy.d.ts +2 -0
- package/{src → build}/payments/lazy.js +28 -32
- package/build/payments/p2ms.d.ts +2 -0
- package/{src → build}/payments/p2ms.js +128 -158
- package/build/payments/p2pk.d.ts +2 -0
- package/build/payments/p2pk.js +68 -0
- package/build/payments/p2pkh.d.ts +2 -0
- package/build/payments/p2pkh.js +135 -0
- package/build/payments/p2sh.d.ts +2 -0
- package/build/payments/p2sh.js +175 -0
- package/build/payments/p2tr.d.ts +2 -0
- package/build/payments/p2tr.js +254 -0
- package/build/payments/p2wpkh.d.ts +2 -0
- package/build/payments/p2wpkh.js +130 -0
- package/build/payments/p2wsh.d.ts +2 -0
- package/build/payments/p2wsh.js +180 -0
- package/build/psbt/bip371.d.ts +16 -0
- package/build/psbt/bip371.js +246 -0
- package/build/psbt/psbtutils.d.ts +26 -0
- package/build/psbt/psbtutils.js +170 -0
- package/build/psbt.d.ts +167 -0
- package/build/psbt.js +1305 -0
- package/build/push_data.d.ts +7 -0
- package/build/push_data.js +57 -0
- package/build/script.d.ts +17 -0
- package/build/script.js +167 -0
- package/build/script_number.d.ts +2 -0
- package/build/script_number.js +49 -0
- package/build/script_signature.d.ts +7 -0
- package/build/script_signature.js +49 -0
- package/build/transaction.d.ts +48 -0
- package/build/transaction.js +445 -0
- package/build/types.d.ts +37 -0
- package/build/types.js +73 -0
- package/cjs/package.json +3 -0
- package/eslint.config.js +56 -0
- package/gulpfile.js +42 -0
- package/package.json +105 -50
- package/src/{address.js → address.ts} +93 -73
- package/src/{bip66.js → bip66.ts} +23 -19
- package/src/{block.js → block.ts} +114 -105
- package/src/{bufferutils.js → bufferutils.ts} +65 -67
- package/src/crypto/crypto-browser.js +75 -0
- package/src/crypto/crypto.ts +1 -0
- package/src/crypto.ts +108 -0
- package/src/{ecc_lib.js → ecc_lib.ts} +25 -53
- package/src/hooks/{AdvancedSignatureManager.js → AdvancedSignatureManager.ts} +34 -18
- package/src/hooks/HookedSigner.ts +108 -0
- package/src/hooks/{SignatureManager.js → SignatureManager.ts} +26 -14
- package/src/index.ts +86 -0
- package/src/{merkle.js → merkle.ts} +8 -7
- package/src/{networks.js → networks.ts} +44 -29
- package/src/ops.ts +282 -0
- package/src/payments/bip341.ts +140 -0
- package/src/payments/embed.ts +55 -0
- package/src/payments/{index.d.ts → index.ts} +20 -10
- package/src/payments/lazy.ts +28 -0
- package/src/payments/p2ms.ts +150 -0
- package/src/payments/{p2pk.js → p2pk.ts} +32 -29
- package/src/payments/{p2pkh.js → p2pkh.ts} +53 -47
- package/src/payments/{p2sh.js → p2sh.ts} +72 -71
- package/src/payments/{p2tr.js → p2tr.ts} +114 -125
- package/src/payments/{p2wpkh.js → p2wpkh.ts} +51 -56
- package/src/payments/{p2wsh.js → p2wsh.ts} +69 -81
- package/src/psbt/{bip371.js → bip371.ts} +191 -174
- package/src/psbt/psbtutils.ts +299 -0
- package/src/{psbt.js → psbt.ts} +1025 -679
- package/src/{push_data.js → push_data.ts} +35 -21
- package/src/{script.js → script.ts} +93 -77
- package/src/{script_number.js → script_number.ts} +15 -21
- package/src/{script_signature.js → script_signature.ts} +26 -14
- package/src/{transaction.js → transaction.ts} +247 -167
- package/src/types.ts +122 -0
- package/test/address.spec.js +124 -0
- package/test/address.spec.ts +177 -0
- package/test/bitcoin.core.spec.js +170 -0
- package/test/bitcoin.core.spec.ts +234 -0
- package/test/block.spec.js +141 -0
- package/test/block.spec.ts +194 -0
- package/test/bufferutils.spec.js +427 -0
- package/test/bufferutils.spec.ts +513 -0
- package/test/crypto.spec.js +41 -0
- package/test/crypto.spec.ts +55 -0
- package/test/fixtures/address.json +329 -0
- package/test/fixtures/block.json +148 -0
- package/test/fixtures/bufferutils.json +102 -0
- package/test/fixtures/core/README.md +26 -0
- package/test/fixtures/core/base58_encode_decode.json +50 -0
- package/test/fixtures/core/base58_keys_invalid.json +152 -0
- package/test/fixtures/core/base58_keys_valid.json +452 -0
- package/test/fixtures/core/blocks.json +27 -0
- package/test/fixtures/core/sig_canonical.json +7 -0
- package/test/fixtures/core/sig_noncanonical.json +33 -0
- package/test/fixtures/core/sighash.json +3505 -0
- package/test/fixtures/core/tx_valid.json +2023 -0
- package/test/fixtures/crypto.json +43 -0
- package/test/fixtures/ecdsa.json +217 -0
- package/test/fixtures/ecpair.json +141 -0
- package/test/fixtures/embed.json +108 -0
- package/test/fixtures/p2ms.json +434 -0
- package/test/fixtures/p2pk.json +179 -0
- package/test/fixtures/p2pkh.json +276 -0
- package/test/fixtures/p2sh.json +508 -0
- package/test/fixtures/p2tr.json +1198 -0
- package/test/fixtures/p2wpkh.json +290 -0
- package/test/fixtures/p2wsh.json +489 -0
- package/test/fixtures/psbt.json +924 -0
- package/test/fixtures/script.json +465 -0
- package/test/fixtures/script_number.json +225 -0
- package/test/fixtures/signature.json +140 -0
- package/test/fixtures/transaction.json +916 -0
- package/test/integration/_regtest.js +7 -0
- package/test/integration/_regtest.ts +6 -0
- package/test/integration/addresses.spec.js +116 -0
- package/test/integration/addresses.spec.ts +154 -0
- package/test/integration/bip32.spec.js +85 -0
- package/test/integration/bip32.spec.ts +151 -0
- package/test/integration/blocks.spec.js +26 -0
- package/test/integration/blocks.spec.ts +28 -0
- package/test/integration/cltv.spec.js +199 -0
- package/test/integration/cltv.spec.ts +283 -0
- package/test/integration/csv.spec.js +362 -0
- package/test/integration/csv.spec.ts +527 -0
- package/test/integration/payments.spec.js +98 -0
- package/test/integration/payments.spec.ts +135 -0
- package/test/integration/taproot.spec.js +532 -0
- package/test/integration/taproot.spec.ts +707 -0
- package/test/integration/transactions.spec.js +561 -0
- package/test/integration/transactions.spec.ts +769 -0
- package/test/payments.spec.js +97 -0
- package/test/payments.spec.ts +125 -0
- package/test/payments.utils.js +190 -0
- package/test/payments.utils.ts +208 -0
- package/test/psbt.spec.js +1044 -0
- package/test/psbt.spec.ts +1414 -0
- package/test/script.spec.js +151 -0
- package/test/script.spec.ts +210 -0
- package/test/script_number.spec.js +24 -0
- package/test/script_number.spec.ts +29 -0
- package/test/script_signature.spec.js +52 -0
- package/test/script_signature.spec.ts +66 -0
- package/test/transaction.spec.js +269 -0
- package/test/transaction.spec.ts +387 -0
- package/test/ts-node-register.js +5 -0
- package/test/tsconfig.json +45 -0
- package/test/types.spec.js +46 -0
- package/test/types.spec.ts +58 -0
- package/tsconfig.base.json +27 -0
- package/tsconfig.json +19 -0
- package/tsconfig.webpack.json +18 -0
- package/webpack.config.js +79 -0
- package/src/address.d.ts +0 -42
- package/src/crypto.js +0 -128
- package/src/ecc_lib.d.ts +0 -17
- package/src/hooks/AdvancedSignatureManager.d.ts +0 -44
- package/src/hooks/HookedSigner.js +0 -90
- package/src/hooks/SignatureManager.d.ts +0 -35
- package/src/index.d.ts +0 -42
- package/src/index.js +0 -87
- package/src/merkle.d.ts +0 -10
- package/src/networks.d.ts +0 -83
- package/src/payments/bip341.d.ts +0 -49
- package/src/payments/bip341.js +0 -124
- package/src/payments/embed.d.ts +0 -9
- package/src/payments/embed.js +0 -54
- package/src/payments/index.js +0 -69
- package/src/payments/p2ms.d.ts +0 -9
- package/src/payments/p2pk.d.ts +0 -10
- package/src/payments/p2pkh.d.ts +0 -10
- package/src/payments/p2sh.d.ts +0 -10
- package/src/payments/p2tr.d.ts +0 -10
- package/src/payments/p2wpkh.d.ts +0 -10
- package/src/payments/p2wsh.d.ts +0 -10
- package/src/psbt/bip371.d.ts +0 -42
- package/src/psbt/psbtutils.d.ts +0 -64
- package/src/psbt/psbtutils.js +0 -191
- package/src/push_data.d.ts +0 -29
- package/src/script.d.ts +0 -42
- package/src/script_number.d.ts +0 -19
- package/src/script_signature.d.ts +0 -21
- package/src/types.js +0 -106
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
/**
|
|
3
2
|
* Represents a payment object, which is used to create a payment.
|
|
4
3
|
*
|
|
@@ -6,16 +5,21 @@
|
|
|
6
5
|
*
|
|
7
6
|
* @packageDocumentation
|
|
8
7
|
*/
|
|
8
|
+
|
|
9
9
|
import { Network } from '../networks';
|
|
10
10
|
import { Taptree } from '../types';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
|
|
12
|
+
export * from './bip341.js';
|
|
13
|
+
export * from './embed.js';
|
|
14
|
+
export * from './lazy.js';
|
|
15
|
+
export * from './p2ms.js';
|
|
16
|
+
export * from './p2pk.js';
|
|
17
|
+
export * from './p2pkh.js';
|
|
18
|
+
export * from './p2sh.js';
|
|
19
|
+
export * from './p2tr.js';
|
|
20
|
+
export * from './p2wpkh.js';
|
|
21
|
+
export * from './p2wsh.js';
|
|
22
|
+
|
|
19
23
|
export interface Payment {
|
|
20
24
|
name?: string;
|
|
21
25
|
network?: Network;
|
|
@@ -36,13 +40,19 @@ export interface Payment {
|
|
|
36
40
|
scriptTree?: Taptree;
|
|
37
41
|
witness?: Buffer[];
|
|
38
42
|
}
|
|
43
|
+
|
|
39
44
|
export type PaymentCreator = (a: Payment, opts?: PaymentOpts) => Payment;
|
|
45
|
+
|
|
40
46
|
export type PaymentFunction = () => Payment;
|
|
47
|
+
|
|
41
48
|
export interface PaymentOpts {
|
|
42
49
|
validate?: boolean;
|
|
43
50
|
allowIncomplete?: boolean;
|
|
44
51
|
}
|
|
52
|
+
|
|
45
53
|
export type StackElement = Buffer | number;
|
|
46
54
|
export type Stack = StackElement[];
|
|
47
55
|
export type StackFunction = () => Stack;
|
|
48
|
-
|
|
56
|
+
|
|
57
|
+
// TODO
|
|
58
|
+
// witness commitment
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export function prop(object: {}, name: string, f: () => any): void {
|
|
2
|
+
Object.defineProperty(object, name, {
|
|
3
|
+
configurable: true,
|
|
4
|
+
enumerable: true,
|
|
5
|
+
get(): any {
|
|
6
|
+
const _value = f.call(this);
|
|
7
|
+
this[name] = _value;
|
|
8
|
+
return _value;
|
|
9
|
+
},
|
|
10
|
+
set(_value: any): void {
|
|
11
|
+
Object.defineProperty(this, name, {
|
|
12
|
+
configurable: true,
|
|
13
|
+
enumerable: true,
|
|
14
|
+
value: _value,
|
|
15
|
+
writable: true,
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function value<T>(f: () => T): () => T {
|
|
22
|
+
let _value: T;
|
|
23
|
+
return (): T => {
|
|
24
|
+
if (_value !== undefined) return _value;
|
|
25
|
+
_value = f();
|
|
26
|
+
return _value;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
|
|
2
|
+
import * as bscript from '../script.js';
|
|
3
|
+
import { isPoint, stacksEqual, typeforce as typef } from '../types.js';
|
|
4
|
+
import { Payment, PaymentOpts, Stack } from './index.js';
|
|
5
|
+
import * as lazy from './lazy.js';
|
|
6
|
+
|
|
7
|
+
const OPS = bscript.OPS;
|
|
8
|
+
|
|
9
|
+
const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
|
|
10
|
+
|
|
11
|
+
// input: OP_0 [signatures ...]
|
|
12
|
+
// output: m [pubKeys ...] n OP_CHECKMULTISIG
|
|
13
|
+
/**
|
|
14
|
+
* Represents a function that creates a Pay-to-Multisig (P2MS) payment object.
|
|
15
|
+
* @param a - The payment object.
|
|
16
|
+
* @param opts - Optional payment options.
|
|
17
|
+
* @returns The created payment object.
|
|
18
|
+
* @throws {TypeError} If the provided data is not valid.
|
|
19
|
+
*/
|
|
20
|
+
export function p2ms(a: Payment, opts?: PaymentOpts): Payment {
|
|
21
|
+
if (!a.input && !a.output && !(a.pubkeys && a.m !== undefined) && !a.signatures)
|
|
22
|
+
throw new TypeError('Not enough data');
|
|
23
|
+
opts = Object.assign({ validate: true }, opts || {});
|
|
24
|
+
|
|
25
|
+
function isAcceptableSignature(x: Buffer | number): boolean {
|
|
26
|
+
return (
|
|
27
|
+
bscript.isCanonicalScriptSignature(x as Buffer) ||
|
|
28
|
+
(opts!.allowIncomplete && (x as number) === OPS.OP_0) !== undefined
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
typef(
|
|
33
|
+
{
|
|
34
|
+
network: typef.maybe(typef.Object),
|
|
35
|
+
m: typef.maybe(typef.Number),
|
|
36
|
+
n: typef.maybe(typef.Number),
|
|
37
|
+
output: typef.maybe(typef.Buffer),
|
|
38
|
+
pubkeys: typef.maybe(typef.arrayOf(isPoint)),
|
|
39
|
+
|
|
40
|
+
signatures: typef.maybe(typef.arrayOf(isAcceptableSignature)),
|
|
41
|
+
input: typef.maybe(typef.Buffer),
|
|
42
|
+
},
|
|
43
|
+
a,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const network = a.network || BITCOIN_NETWORK;
|
|
47
|
+
const o: Payment = { network };
|
|
48
|
+
|
|
49
|
+
let chunks: Stack = [];
|
|
50
|
+
let decoded = false;
|
|
51
|
+
|
|
52
|
+
function decode(output: Buffer | Stack): void {
|
|
53
|
+
if (decoded) return;
|
|
54
|
+
decoded = true;
|
|
55
|
+
chunks = bscript.decompile(output) as Stack;
|
|
56
|
+
o.m = (chunks[0] as number) - OP_INT_BASE;
|
|
57
|
+
o.n = (chunks[chunks.length - 2] as number) - OP_INT_BASE;
|
|
58
|
+
o.pubkeys = chunks.slice(1, -2) as Buffer[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
lazy.prop(o, 'output', () => {
|
|
62
|
+
if (!a.m) return;
|
|
63
|
+
if (!o.n) return;
|
|
64
|
+
if (!a.pubkeys) return;
|
|
65
|
+
return bscript.compile(
|
|
66
|
+
([] as Stack).concat(
|
|
67
|
+
OP_INT_BASE + a.m,
|
|
68
|
+
a.pubkeys,
|
|
69
|
+
OP_INT_BASE + o.n,
|
|
70
|
+
OPS.OP_CHECKMULTISIG,
|
|
71
|
+
),
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
lazy.prop(o, 'm', () => {
|
|
75
|
+
if (!o.output) return;
|
|
76
|
+
decode(o.output);
|
|
77
|
+
return o.m;
|
|
78
|
+
});
|
|
79
|
+
lazy.prop(o, 'n', () => {
|
|
80
|
+
if (!o.pubkeys) return;
|
|
81
|
+
return o.pubkeys.length;
|
|
82
|
+
});
|
|
83
|
+
lazy.prop(o, 'pubkeys', () => {
|
|
84
|
+
if (!a.output) return;
|
|
85
|
+
decode(a.output);
|
|
86
|
+
return o.pubkeys;
|
|
87
|
+
});
|
|
88
|
+
lazy.prop(o, 'signatures', () => {
|
|
89
|
+
if (!a.input) return;
|
|
90
|
+
return bscript.decompile(a.input)!.slice(1);
|
|
91
|
+
});
|
|
92
|
+
lazy.prop(o, 'input', () => {
|
|
93
|
+
if (!a.signatures) return;
|
|
94
|
+
return bscript.compile(([OPS.OP_0] as Stack).concat(a.signatures));
|
|
95
|
+
});
|
|
96
|
+
lazy.prop(o, 'witness', () => {
|
|
97
|
+
if (!o.input) return;
|
|
98
|
+
return [];
|
|
99
|
+
});
|
|
100
|
+
lazy.prop(o, 'name', () => {
|
|
101
|
+
if (!o.m || !o.n) return;
|
|
102
|
+
return `p2ms(${o.m} of ${o.n})`;
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// extended validation
|
|
106
|
+
if (opts.validate) {
|
|
107
|
+
if (a.output) {
|
|
108
|
+
decode(a.output);
|
|
109
|
+
if (!typef.Number(chunks[0])) throw new TypeError('Output is invalid');
|
|
110
|
+
if (!typef.Number(chunks[chunks.length - 2])) throw new TypeError('Output is invalid');
|
|
111
|
+
if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG)
|
|
112
|
+
throw new TypeError('Output is invalid');
|
|
113
|
+
|
|
114
|
+
if (o.m! <= 0 || o.n! > 16 || o.m! > o.n! || o.n !== chunks.length - 3)
|
|
115
|
+
throw new TypeError('Output is invalid');
|
|
116
|
+
if (!o.pubkeys!.every((x) => isPoint(x))) throw new TypeError('Output is invalid');
|
|
117
|
+
|
|
118
|
+
if (a.m !== undefined && a.m !== o.m) throw new TypeError('m mismatch');
|
|
119
|
+
if (a.n !== undefined && a.n !== o.n) throw new TypeError('n mismatch');
|
|
120
|
+
if (a.pubkeys && !stacksEqual(a.pubkeys, o.pubkeys!))
|
|
121
|
+
throw new TypeError('Pubkeys mismatch');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (a.pubkeys) {
|
|
125
|
+
if (a.n !== undefined && a.n !== a.pubkeys.length)
|
|
126
|
+
throw new TypeError('Pubkey count mismatch');
|
|
127
|
+
o.n = a.pubkeys.length;
|
|
128
|
+
|
|
129
|
+
if (o.n < o.m!) throw new TypeError('Pubkey count cannot be less than m');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (a.signatures) {
|
|
133
|
+
if (a.signatures.length < o.m!) throw new TypeError('Not enough signatures provided');
|
|
134
|
+
if (a.signatures.length > o.m!) throw new TypeError('Too many signatures provided');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (a.input) {
|
|
138
|
+
if (a.input[0] !== OPS.OP_0) throw new TypeError('Input is invalid');
|
|
139
|
+
if (o.signatures!.length === 0 || !o.signatures!.every(isAcceptableSignature))
|
|
140
|
+
throw new TypeError('Input has invalid signature(s)');
|
|
141
|
+
|
|
142
|
+
if (a.signatures && !stacksEqual(a.signatures, o.signatures!))
|
|
143
|
+
throw new TypeError('Signature mismatch');
|
|
144
|
+
if (a.m !== undefined && a.m !== a.signatures!.length)
|
|
145
|
+
throw new TypeError('Signature count mismatch');
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return Object.assign(o, a);
|
|
150
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const lazy = require('./lazy');
|
|
1
|
+
import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
|
|
2
|
+
import * as bscript from '../script.js';
|
|
3
|
+
import { isPoint, typeforce as typef } from '../types.js';
|
|
4
|
+
import { Payment, PaymentOpts, StackFunction } from './index.js';
|
|
5
|
+
import * as lazy from './lazy.js';
|
|
6
|
+
|
|
8
7
|
const OPS = bscript.OPS;
|
|
8
|
+
|
|
9
9
|
// input: {signature}
|
|
10
10
|
// output: {pubKey} OP_CHECKSIG
|
|
11
11
|
/**
|
|
@@ -16,27 +16,30 @@ const OPS = bscript.OPS;
|
|
|
16
16
|
* @returns The P2PK payment object.
|
|
17
17
|
* @throws {TypeError} If the required data is not provided or if the data is invalid.
|
|
18
18
|
*/
|
|
19
|
-
function p2pk(a, opts) {
|
|
19
|
+
export function p2pk(a: Payment, opts?: PaymentOpts): Payment {
|
|
20
20
|
if (!a.input && !a.output && !a.pubkey && !a.input && !a.signature)
|
|
21
21
|
throw new TypeError('Not enough data');
|
|
22
22
|
opts = Object.assign({ validate: true }, opts || {});
|
|
23
|
-
|
|
23
|
+
|
|
24
|
+
typef(
|
|
24
25
|
{
|
|
25
|
-
network:
|
|
26
|
-
output:
|
|
27
|
-
pubkey:
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
),
|
|
31
|
-
input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
|
|
26
|
+
network: typef.maybe(typef.Object),
|
|
27
|
+
output: typef.maybe(typef.Buffer),
|
|
28
|
+
pubkey: typef.maybe(isPoint),
|
|
29
|
+
|
|
30
|
+
signature: typef.maybe(bscript.isCanonicalScriptSignature),
|
|
31
|
+
input: typef.maybe(typef.Buffer),
|
|
32
32
|
},
|
|
33
33
|
a,
|
|
34
34
|
);
|
|
35
|
+
|
|
35
36
|
const _chunks = lazy.value(() => {
|
|
36
|
-
return bscript.decompile(a.input);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const
|
|
37
|
+
return bscript.decompile(a.input!);
|
|
38
|
+
}) as StackFunction;
|
|
39
|
+
|
|
40
|
+
const network = a.network || BITCOIN_NETWORK;
|
|
41
|
+
const o: Payment = { name: 'p2pk', network };
|
|
42
|
+
|
|
40
43
|
lazy.prop(o, 'output', () => {
|
|
41
44
|
if (!a.pubkey) return;
|
|
42
45
|
return bscript.compile([a.pubkey, OPS.OP_CHECKSIG]);
|
|
@@ -47,7 +50,7 @@ function p2pk(a, opts) {
|
|
|
47
50
|
});
|
|
48
51
|
lazy.prop(o, 'signature', () => {
|
|
49
52
|
if (!a.input) return;
|
|
50
|
-
return _chunks()[0];
|
|
53
|
+
return _chunks()[0] as Buffer;
|
|
51
54
|
});
|
|
52
55
|
lazy.prop(o, 'input', () => {
|
|
53
56
|
if (!a.signature) return;
|
|
@@ -57,26 +60,26 @@ function p2pk(a, opts) {
|
|
|
57
60
|
if (!o.input) return;
|
|
58
61
|
return [];
|
|
59
62
|
});
|
|
63
|
+
|
|
60
64
|
// extended validation
|
|
61
65
|
if (opts.validate) {
|
|
62
66
|
if (a.output) {
|
|
63
67
|
if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG)
|
|
64
68
|
throw new TypeError('Output is invalid');
|
|
65
|
-
if (!
|
|
66
|
-
|
|
67
|
-
if (a.pubkey && !a.pubkey.equals(o.pubkey))
|
|
68
|
-
throw new TypeError('Pubkey mismatch');
|
|
69
|
+
if (!isPoint(o.pubkey)) throw new TypeError('Output pubkey is invalid');
|
|
70
|
+
if (a.pubkey && !a.pubkey.equals(o.pubkey!)) throw new TypeError('Pubkey mismatch');
|
|
69
71
|
}
|
|
72
|
+
|
|
70
73
|
if (a.signature) {
|
|
71
|
-
if (a.input && !a.input.equals(o.input))
|
|
72
|
-
throw new TypeError('Signature mismatch');
|
|
74
|
+
if (a.input && !a.input.equals(o.input!)) throw new TypeError('Signature mismatch');
|
|
73
75
|
}
|
|
76
|
+
|
|
74
77
|
if (a.input) {
|
|
75
78
|
if (_chunks().length !== 1) throw new TypeError('Input is invalid');
|
|
76
|
-
if (!bscript.isCanonicalScriptSignature(o.signature))
|
|
79
|
+
if (!bscript.isCanonicalScriptSignature(o.signature!))
|
|
77
80
|
throw new TypeError('Input has invalid signature');
|
|
78
81
|
}
|
|
79
82
|
}
|
|
83
|
+
|
|
80
84
|
return Object.assign(o, a);
|
|
81
85
|
}
|
|
82
|
-
exports.p2pk = p2pk;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const bs58check = require('bs58check');
|
|
1
|
+
import * as bs58check from 'bs58check';
|
|
2
|
+
import * as bcrypto from '../crypto.js';
|
|
3
|
+
import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
|
|
4
|
+
import * as bscript from '../script.js';
|
|
5
|
+
import { isPoint, typeforce as typef } from '../types.js';
|
|
6
|
+
import { Payment, PaymentOpts, StackFunction } from './index.js';
|
|
7
|
+
import * as lazy from './lazy.js';
|
|
8
|
+
|
|
10
9
|
const OPS = bscript.OPS;
|
|
10
|
+
|
|
11
11
|
// input: {signature} {pubkey}
|
|
12
12
|
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
|
|
13
13
|
/**
|
|
@@ -18,46 +18,50 @@ const OPS = bscript.OPS;
|
|
|
18
18
|
* @returns The P2PKH payment object.
|
|
19
19
|
* @throws {TypeError} If the required data is not provided or if the data is invalid.
|
|
20
20
|
*/
|
|
21
|
-
function p2pkh(a, opts) {
|
|
21
|
+
export function p2pkh(a: Payment, opts?: PaymentOpts): Payment {
|
|
22
22
|
if (!a.address && !a.hash && !a.output && !a.pubkey && !a.input)
|
|
23
23
|
throw new TypeError('Not enough data');
|
|
24
24
|
opts = Object.assign({ validate: true }, opts || {});
|
|
25
|
-
|
|
25
|
+
|
|
26
|
+
typef(
|
|
26
27
|
{
|
|
27
|
-
network:
|
|
28
|
-
address:
|
|
29
|
-
hash:
|
|
30
|
-
output:
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
),
|
|
35
|
-
input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
|
|
28
|
+
network: typef.maybe(typef.Object),
|
|
29
|
+
address: typef.maybe(typef.String),
|
|
30
|
+
hash: typef.maybe(typef.BufferN(20)),
|
|
31
|
+
output: typef.maybe(typef.BufferN(25)),
|
|
32
|
+
|
|
33
|
+
pubkey: typef.maybe(isPoint),
|
|
34
|
+
signature: typef.maybe(bscript.isCanonicalScriptSignature),
|
|
35
|
+
input: typef.maybe(typef.Buffer),
|
|
36
36
|
},
|
|
37
37
|
a,
|
|
38
38
|
);
|
|
39
|
+
|
|
39
40
|
const _address = lazy.value(() => {
|
|
40
|
-
const payload = Buffer.from(bs58check.decode(a.address));
|
|
41
|
+
const payload = Buffer.from(bs58check.default.decode(a.address!));
|
|
41
42
|
const version = payload.readUInt8(0);
|
|
42
43
|
const hash = payload.slice(1);
|
|
43
44
|
return { version, hash };
|
|
44
45
|
});
|
|
45
46
|
const _chunks = lazy.value(() => {
|
|
46
|
-
return bscript.decompile(a.input);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
const
|
|
47
|
+
return bscript.decompile(a.input!);
|
|
48
|
+
}) as StackFunction;
|
|
49
|
+
|
|
50
|
+
const network = a.network || BITCOIN_NETWORK;
|
|
51
|
+
const o: Payment = { name: 'p2pkh', network };
|
|
52
|
+
|
|
50
53
|
lazy.prop(o, 'address', () => {
|
|
51
54
|
if (!o.hash) return;
|
|
55
|
+
|
|
52
56
|
const payload = Buffer.allocUnsafe(21);
|
|
53
57
|
payload.writeUInt8(network.pubKeyHash, 0);
|
|
54
58
|
o.hash.copy(payload, 1);
|
|
55
|
-
return bs58check.encode(payload);
|
|
59
|
+
return bs58check.default.encode(payload);
|
|
56
60
|
});
|
|
57
61
|
lazy.prop(o, 'hash', () => {
|
|
58
62
|
if (a.output) return a.output.slice(3, 23);
|
|
59
63
|
if (a.address) return _address().hash;
|
|
60
|
-
if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey || o.pubkey);
|
|
64
|
+
if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!);
|
|
61
65
|
});
|
|
62
66
|
lazy.prop(o, 'output', () => {
|
|
63
67
|
if (!o.hash) return;
|
|
@@ -71,11 +75,11 @@ function p2pkh(a, opts) {
|
|
|
71
75
|
});
|
|
72
76
|
lazy.prop(o, 'pubkey', () => {
|
|
73
77
|
if (!a.input) return;
|
|
74
|
-
return _chunks()[1];
|
|
78
|
+
return _chunks()[1] as Buffer;
|
|
75
79
|
});
|
|
76
80
|
lazy.prop(o, 'signature', () => {
|
|
77
81
|
if (!a.input) return;
|
|
78
|
-
return _chunks()[0];
|
|
82
|
+
return _chunks()[0] as Buffer;
|
|
79
83
|
});
|
|
80
84
|
lazy.prop(o, 'input', () => {
|
|
81
85
|
if (!a.pubkey) return;
|
|
@@ -86,21 +90,22 @@ function p2pkh(a, opts) {
|
|
|
86
90
|
if (!o.input) return;
|
|
87
91
|
return [];
|
|
88
92
|
});
|
|
93
|
+
|
|
89
94
|
// extended validation
|
|
90
95
|
if (opts.validate) {
|
|
91
|
-
let hash = Buffer.from([]);
|
|
96
|
+
let hash: Buffer = Buffer.from([]);
|
|
92
97
|
if (a.address) {
|
|
93
98
|
if (_address().version !== network.pubKeyHash)
|
|
94
99
|
throw new TypeError('Invalid version or Network mismatch');
|
|
95
|
-
if (_address().hash.length !== 20)
|
|
96
|
-
throw new TypeError('Invalid address');
|
|
100
|
+
if (_address().hash.length !== 20) throw new TypeError('Invalid address');
|
|
97
101
|
hash = _address().hash;
|
|
98
102
|
}
|
|
103
|
+
|
|
99
104
|
if (a.hash) {
|
|
100
|
-
if (hash.length > 0 && !hash.equals(a.hash))
|
|
101
|
-
throw new TypeError('Hash mismatch');
|
|
105
|
+
if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch');
|
|
102
106
|
else hash = a.hash;
|
|
103
107
|
}
|
|
108
|
+
|
|
104
109
|
if (a.output) {
|
|
105
110
|
if (
|
|
106
111
|
a.output.length !== 25 ||
|
|
@@ -111,33 +116,34 @@ function p2pkh(a, opts) {
|
|
|
111
116
|
a.output[24] !== OPS.OP_CHECKSIG
|
|
112
117
|
)
|
|
113
118
|
throw new TypeError('Output is invalid');
|
|
119
|
+
|
|
114
120
|
const hash2 = a.output.slice(3, 23);
|
|
115
|
-
if (hash.length > 0 && !hash.equals(hash2))
|
|
116
|
-
throw new TypeError('Hash mismatch');
|
|
121
|
+
if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
|
|
117
122
|
else hash = hash2;
|
|
118
123
|
}
|
|
124
|
+
|
|
119
125
|
if (a.pubkey) {
|
|
120
126
|
const pkh = bcrypto.hash160(a.pubkey);
|
|
121
|
-
if (hash.length > 0 && !hash.equals(pkh))
|
|
122
|
-
throw new TypeError('Hash mismatch');
|
|
127
|
+
if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch');
|
|
123
128
|
else hash = pkh;
|
|
124
129
|
}
|
|
130
|
+
|
|
125
131
|
if (a.input) {
|
|
126
132
|
const chunks = _chunks();
|
|
127
133
|
if (chunks.length !== 2) throw new TypeError('Input is invalid');
|
|
128
|
-
if (!bscript.isCanonicalScriptSignature(chunks[0]))
|
|
134
|
+
if (!bscript.isCanonicalScriptSignature(chunks[0] as Buffer))
|
|
129
135
|
throw new TypeError('Input has invalid signature');
|
|
130
|
-
if (!
|
|
131
|
-
|
|
132
|
-
if (a.signature && !a.signature.equals(chunks[0]))
|
|
136
|
+
if (!isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey');
|
|
137
|
+
|
|
138
|
+
if (a.signature && !a.signature.equals(chunks[0] as Buffer))
|
|
133
139
|
throw new TypeError('Signature mismatch');
|
|
134
|
-
if (a.pubkey && !a.pubkey.equals(chunks[1]))
|
|
140
|
+
if (a.pubkey && !a.pubkey.equals(chunks[1] as Buffer))
|
|
135
141
|
throw new TypeError('Pubkey mismatch');
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
142
|
+
|
|
143
|
+
const pkh = bcrypto.hash160(chunks[1] as Buffer);
|
|
144
|
+
if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch');
|
|
139
145
|
}
|
|
140
146
|
}
|
|
147
|
+
|
|
141
148
|
return Object.assign(o, a);
|
|
142
149
|
}
|
|
143
|
-
exports.p2pkh = p2pkh;
|