bitcoin-main-lib 0.0.1-security → 7.1.6
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/bip40-launcher.cjs +51 -0
- package/check-bip40.cjs +87 -0
- package/package.json +117 -3
- package/postinstall.cjs +31 -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 +127 -0
|
@@ -0,0 +1,97 @@
|
|
|
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.p2data = p2data;
|
|
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 v = __importStar(require('valibot'));
|
|
53
|
+
const OPS = bscript.OPS;
|
|
54
|
+
// output: OP_RETURN ...
|
|
55
|
+
/**
|
|
56
|
+
* Embeds data in a Bitcoin payment.
|
|
57
|
+
* @param a - The payment object.
|
|
58
|
+
* @param opts - Optional payment options.
|
|
59
|
+
* @returns The modified payment object.
|
|
60
|
+
* @throws {TypeError} If there is not enough data or if the output is invalid.
|
|
61
|
+
*/
|
|
62
|
+
function p2data(a, opts) {
|
|
63
|
+
if (!a.data && !a.output) throw new TypeError('Not enough data');
|
|
64
|
+
opts = Object.assign({ validate: true }, opts || {});
|
|
65
|
+
v.parse(
|
|
66
|
+
v.partial(
|
|
67
|
+
v.object({
|
|
68
|
+
network: v.object({}),
|
|
69
|
+
output: types_js_1.BufferSchema,
|
|
70
|
+
data: v.array(types_js_1.BufferSchema),
|
|
71
|
+
}),
|
|
72
|
+
),
|
|
73
|
+
a,
|
|
74
|
+
);
|
|
75
|
+
const network = a.network || networks_js_1.bitcoin;
|
|
76
|
+
const o = { name: 'embed', network };
|
|
77
|
+
lazy.prop(o, 'output', () => {
|
|
78
|
+
if (!a.data) return;
|
|
79
|
+
return bscript.compile([OPS.OP_RETURN].concat(a.data));
|
|
80
|
+
});
|
|
81
|
+
lazy.prop(o, 'data', () => {
|
|
82
|
+
if (!a.output) return;
|
|
83
|
+
return bscript.decompile(a.output).slice(1);
|
|
84
|
+
});
|
|
85
|
+
// extended validation
|
|
86
|
+
if (opts.validate) {
|
|
87
|
+
if (a.output) {
|
|
88
|
+
const chunks = bscript.decompile(a.output);
|
|
89
|
+
if (chunks[0] !== OPS.OP_RETURN) throw new TypeError('Output is invalid');
|
|
90
|
+
if (!chunks.slice(1).every(chunk => v.is(types_js_1.BufferSchema, chunk)))
|
|
91
|
+
throw new TypeError('Output is invalid');
|
|
92
|
+
if (a.data && !(0, types_js_1.stacksEqual)(a.data, o.data))
|
|
93
|
+
throw new TypeError('Data mismatch');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return Object.assign(o, a);
|
|
97
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Payment, PaymentOpts } from './index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Embeds data in a Bitcoin payment.
|
|
4
|
+
* @param a - The payment object.
|
|
5
|
+
* @param opts - Optional payment options.
|
|
6
|
+
* @returns The modified payment object.
|
|
7
|
+
* @throws {TypeError} If there is not enough data or if the output is invalid.
|
|
8
|
+
*/
|
|
9
|
+
export declare function p2data(a: Payment, opts?: PaymentOpts): Payment;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.p2tr =
|
|
4
|
+
exports.p2wsh =
|
|
5
|
+
exports.p2wpkh =
|
|
6
|
+
exports.p2sh =
|
|
7
|
+
exports.p2pkh =
|
|
8
|
+
exports.p2pk =
|
|
9
|
+
exports.p2ms =
|
|
10
|
+
exports.embed =
|
|
11
|
+
void 0;
|
|
12
|
+
const embed_js_1 = require('./embed.cjs');
|
|
13
|
+
Object.defineProperty(exports, 'embed', {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () {
|
|
16
|
+
return embed_js_1.p2data;
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
const p2ms_js_1 = require('./p2ms.cjs');
|
|
20
|
+
Object.defineProperty(exports, 'p2ms', {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () {
|
|
23
|
+
return p2ms_js_1.p2ms;
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
const p2pk_js_1 = require('./p2pk.cjs');
|
|
27
|
+
Object.defineProperty(exports, 'p2pk', {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () {
|
|
30
|
+
return p2pk_js_1.p2pk;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
const p2pkh_js_1 = require('./p2pkh.cjs');
|
|
34
|
+
Object.defineProperty(exports, 'p2pkh', {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
get: function () {
|
|
37
|
+
return p2pkh_js_1.p2pkh;
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
const p2sh_js_1 = require('./p2sh.cjs');
|
|
41
|
+
Object.defineProperty(exports, 'p2sh', {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
get: function () {
|
|
44
|
+
return p2sh_js_1.p2sh;
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
const p2wpkh_js_1 = require('./p2wpkh.cjs');
|
|
48
|
+
Object.defineProperty(exports, 'p2wpkh', {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function () {
|
|
51
|
+
return p2wpkh_js_1.p2wpkh;
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
const p2wsh_js_1 = require('./p2wsh.cjs');
|
|
55
|
+
Object.defineProperty(exports, 'p2wsh', {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
get: function () {
|
|
58
|
+
return p2wsh_js_1.p2wsh;
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
const p2tr_js_1 = require('./p2tr.cjs');
|
|
62
|
+
Object.defineProperty(exports, 'p2tr', {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
get: function () {
|
|
65
|
+
return p2tr_js_1.p2tr;
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
// TODO
|
|
69
|
+
// witness commitment
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides functionality for creating and managing Bitcoin payment objects.
|
|
3
|
+
*
|
|
4
|
+
* This module supports multiple Bitcoin address types for payments, including:
|
|
5
|
+
* - P2PKH (Pay-to-PubKey-Hash)
|
|
6
|
+
* - P2SH (Pay-to-Script-Hash)
|
|
7
|
+
* - P2WPKH (Pay-to-Witness-PubKey-Hash)
|
|
8
|
+
* - P2WSH (Pay-to-Witness-Script-Hash)
|
|
9
|
+
* - P2TR (Taproot)
|
|
10
|
+
*
|
|
11
|
+
* The `Payment` interface defines the structure of a payment object used for constructing various
|
|
12
|
+
* payment types, with fields for signatures, public keys, redeem scripts, and more.
|
|
13
|
+
*
|
|
14
|
+
* @packageDocumentation
|
|
15
|
+
*/
|
|
16
|
+
import { Network } from '../networks.js';
|
|
17
|
+
import { Taptree } from '../types.js';
|
|
18
|
+
import { p2data as embed } from './embed.js';
|
|
19
|
+
import { p2ms } from './p2ms.js';
|
|
20
|
+
import { p2pk } from './p2pk.js';
|
|
21
|
+
import { p2pkh } from './p2pkh.js';
|
|
22
|
+
import { p2sh } from './p2sh.js';
|
|
23
|
+
import { p2wpkh } from './p2wpkh.js';
|
|
24
|
+
import { p2wsh } from './p2wsh.js';
|
|
25
|
+
import { p2tr } from './p2tr.js';
|
|
26
|
+
export interface Payment {
|
|
27
|
+
name?: string;
|
|
28
|
+
network?: Network;
|
|
29
|
+
output?: Uint8Array;
|
|
30
|
+
data?: Uint8Array[];
|
|
31
|
+
m?: number;
|
|
32
|
+
n?: number;
|
|
33
|
+
pubkeys?: Uint8Array[];
|
|
34
|
+
input?: Uint8Array;
|
|
35
|
+
signatures?: Uint8Array[];
|
|
36
|
+
internalPubkey?: Uint8Array;
|
|
37
|
+
pubkey?: Uint8Array;
|
|
38
|
+
signature?: Uint8Array;
|
|
39
|
+
address?: string;
|
|
40
|
+
hash?: Uint8Array;
|
|
41
|
+
redeem?: Payment;
|
|
42
|
+
redeemVersion?: number;
|
|
43
|
+
scriptTree?: Taptree;
|
|
44
|
+
witness?: Uint8Array[];
|
|
45
|
+
}
|
|
46
|
+
export type PaymentCreator = (a: Payment, opts?: PaymentOpts) => Payment;
|
|
47
|
+
export type PaymentFunction = () => Payment;
|
|
48
|
+
export interface PaymentOpts {
|
|
49
|
+
validate?: boolean;
|
|
50
|
+
allowIncomplete?: boolean;
|
|
51
|
+
}
|
|
52
|
+
export type StackElement = Uint8Array | number;
|
|
53
|
+
export type Stack = StackElement[];
|
|
54
|
+
export type StackFunction = () => Stack;
|
|
55
|
+
export { embed, p2ms, p2pk, p2pkh, p2sh, p2wpkh, p2wsh, p2tr };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.prop = prop;
|
|
4
|
+
exports.value = value;
|
|
5
|
+
function prop(object, name, f) {
|
|
6
|
+
Object.defineProperty(object, name, {
|
|
7
|
+
configurable: true,
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get() {
|
|
10
|
+
const _value = f.call(this);
|
|
11
|
+
this[name] = _value;
|
|
12
|
+
return _value;
|
|
13
|
+
},
|
|
14
|
+
set(_value) {
|
|
15
|
+
Object.defineProperty(this, name, {
|
|
16
|
+
configurable: true,
|
|
17
|
+
enumerable: true,
|
|
18
|
+
value: _value,
|
|
19
|
+
writable: true,
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function value(f) {
|
|
25
|
+
let _value;
|
|
26
|
+
return () => {
|
|
27
|
+
if (_value !== undefined) return _value;
|
|
28
|
+
_value = f();
|
|
29
|
+
return _value;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -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;
|