1 | 'use strict';
|
2 | Object.defineProperty(exports, '__esModule', { value: true });
|
3 | exports.getEccLib = exports.initEccLib = void 0;
|
4 | const _ECCLIB_CACHE = {};
|
5 | function initEccLib(eccLib) {
|
6 | if (!eccLib) {
|
7 |
|
8 | _ECCLIB_CACHE.eccLib = eccLib;
|
9 | } else if (eccLib !== _ECCLIB_CACHE.eccLib) {
|
10 |
|
11 | verifyEcc(eccLib);
|
12 | _ECCLIB_CACHE.eccLib = eccLib;
|
13 | }
|
14 | }
|
15 | exports.initEccLib = initEccLib;
|
16 | function getEccLib() {
|
17 | if (!_ECCLIB_CACHE.eccLib)
|
18 | throw new Error(
|
19 | 'No ECC Library provided. You must call initEccLib() with a valid TinySecp256k1Interface instance',
|
20 | );
|
21 | return _ECCLIB_CACHE.eccLib;
|
22 | }
|
23 | exports.getEccLib = getEccLib;
|
24 | const h = hex => Buffer.from(hex, 'hex');
|
25 | function verifyEcc(ecc) {
|
26 | assert(typeof ecc.isXOnlyPoint === 'function');
|
27 | assert(
|
28 | ecc.isXOnlyPoint(
|
29 | h('79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'),
|
30 | ),
|
31 | );
|
32 | assert(
|
33 | ecc.isXOnlyPoint(
|
34 | h('fffffffffffffffffffffffffffffffffffffffffffffffffffffffeeffffc2e'),
|
35 | ),
|
36 | );
|
37 | assert(
|
38 | ecc.isXOnlyPoint(
|
39 | h('f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9'),
|
40 | ),
|
41 | );
|
42 | assert(
|
43 | ecc.isXOnlyPoint(
|
44 | h('0000000000000000000000000000000000000000000000000000000000000001'),
|
45 | ),
|
46 | );
|
47 | assert(
|
48 | !ecc.isXOnlyPoint(
|
49 | h('0000000000000000000000000000000000000000000000000000000000000000'),
|
50 | ),
|
51 | );
|
52 | assert(
|
53 | !ecc.isXOnlyPoint(
|
54 | h('fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f'),
|
55 | ),
|
56 | );
|
57 | assert(typeof ecc.xOnlyPointAddTweak === 'function');
|
58 | tweakAddVectors.forEach(t => {
|
59 | const r = ecc.xOnlyPointAddTweak(h(t.pubkey), h(t.tweak));
|
60 | if (t.result === null) {
|
61 | assert(r === null);
|
62 | } else {
|
63 | assert(r !== null);
|
64 | assert(r.parity === t.parity);
|
65 | assert(Buffer.from(r.xOnlyPubkey).equals(h(t.result)));
|
66 | }
|
67 | });
|
68 | }
|
69 | function assert(bool) {
|
70 | if (!bool) throw new Error('ecc library invalid');
|
71 | }
|
72 | const tweakAddVectors = [
|
73 | {
|
74 | pubkey: '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
|
75 | tweak: 'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140',
|
76 | parity: -1,
|
77 | result: null,
|
78 | },
|
79 | {
|
80 | pubkey: '1617d38ed8d8657da4d4761e8057bc396ea9e4b9d29776d4be096016dbd2509b',
|
81 | tweak: 'a8397a935f0dfceba6ba9618f6451ef4d80637abf4e6af2669fbc9de6a8fd2ac',
|
82 | parity: 1,
|
83 | result: 'e478f99dab91052ab39a33ea35fd5e6e4933f4d28023cd597c9a1f6760346adf',
|
84 | },
|
85 | {
|
86 | pubkey: '2c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991',
|
87 | tweak: '823c3cd2142744b075a87eade7e1b8678ba308d566226a0056ca2b7a76f86b47',
|
88 | parity: 0,
|
89 | result: '9534f8dc8c6deda2dc007655981c78b49c5d96c778fbf363462a11ec9dfd948c',
|
90 | },
|
91 | ];
|