@xyo-network/elliptic 3.14.5

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/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ [![logo][]](https://xyo.network)
2
+
3
+ Part of [sdk-xyo-client-js](https://www.npmjs.com/package/@xyo-network/sdk-xyo-client-js)
4
+
5
+ ## License
6
+
7
+ > See the [LICENSE](LICENSE) file for license details
8
+
9
+ ## Credits
10
+
11
+ [Made with 🔥 and ❄️ by XYO](https://xyo.network)
12
+
13
+ [logo]: https://cdn.xy.company/img/brand/XYO_full_colored.png
@@ -0,0 +1,86 @@
1
+ // src/Elliptic.ts
2
+ import { instantiateSecp256k1 } from "@bitauth/libauth";
3
+ import { toUint8Array } from "@xylabs/arraybuffer";
4
+ import { assertEx } from "@xylabs/assert";
5
+ import { toHex } from "@xylabs/hex";
6
+ import { Data } from "@xyo-network/data";
7
+ import { WasmSupport } from "@xyo-network/wasm";
8
+ import { Mutex } from "async-mutex";
9
+ var wasmSupportStatic = new WasmSupport(["bigInt", "mutableGlobals", "referenceTypes", "saturatedFloatToInt", "signExtensions", "simd"]);
10
+ var recoveryIds = [0, 1, 2, 3];
11
+ function compareArrayBuffers(b1, b2) {
12
+ if (b1.byteLength !== b2.byteLength) {
13
+ return false;
14
+ }
15
+ const a1 = new Uint8Array(b1);
16
+ const a2 = new Uint8Array(b2);
17
+ for (let i = 0; i < b1.byteLength; i++) {
18
+ if (a1[1] !== a2[1]) {
19
+ return false;
20
+ }
21
+ }
22
+ return true;
23
+ }
24
+ var Elliptic = class {
25
+ static wasmSupport = wasmSupportStatic;
26
+ static _secp256k1;
27
+ static _secp256k1Mutex = new Mutex();
28
+ static addressFromPublicKey(key) {
29
+ return new Data(64, key).keccak256.slice(12);
30
+ }
31
+ static initialize() {
32
+ return this.secp256k1();
33
+ }
34
+ static async publicKeyFromPrivateKey(privateKey, prefix = false) {
35
+ const { derivePublicKeyUncompressed } = await this.secp256k1();
36
+ if (BigInt(toHex(privateKey, { prefix: true })) === 0n) {
37
+ throw new Error(`Invalid private key [${toHex(privateKey)}]`);
38
+ }
39
+ const derivedPublicKey = derivePublicKeyUncompressed(new Uint8Array(privateKey));
40
+ const fullPublicKey = typeof derivedPublicKey === "string" ? toUint8Array(derivedPublicKey) : derivedPublicKey;
41
+ return (prefix ? fullPublicKey : fullPublicKey.slice(1)).buffer;
42
+ }
43
+ static ready() {
44
+ return !!this._secp256k1;
45
+ }
46
+ static async secp256k1() {
47
+ return await this._secp256k1Mutex.runExclusive(async () => {
48
+ if (this._secp256k1) return this._secp256k1;
49
+ await wasmSupportStatic.initialize();
50
+ const secp256k1 = assertEx(await instantiateSecp256k1(), () => "No Wasm Support");
51
+ this._secp256k1 = secp256k1;
52
+ return secp256k1;
53
+ });
54
+ }
55
+ static async sign(hash, key) {
56
+ const { signMessageHashCompact } = await this.secp256k1();
57
+ const signature = signMessageHashCompact(new Uint8Array(key), toUint8Array(hash));
58
+ return (typeof signature === "string" ? toUint8Array(signature) : signature).buffer;
59
+ }
60
+ static async verify(msg, signature, address) {
61
+ const verifier = await this.secp256k1();
62
+ if (verifier && this.wasmSupport.canUseWasm) {
63
+ for (const recoveryId of recoveryIds) {
64
+ try {
65
+ const rawRecoveredPublicKey = verifier.recoverPublicKeyUncompressed(toUint8Array(signature), recoveryId, toUint8Array(msg));
66
+ const recoveredPublicKey = (typeof rawRecoveredPublicKey === "string" ? toUint8Array(rawRecoveredPublicKey) : rawRecoveredPublicKey).slice(1).buffer;
67
+ const recoveredAddress = this.addressFromPublicKey(recoveredPublicKey);
68
+ if (compareArrayBuffers(address, recoveredAddress)) {
69
+ return true;
70
+ }
71
+ } catch (ex) {
72
+ const error = ex;
73
+ console.log(error.message);
74
+ console.log(error.stack);
75
+ continue;
76
+ }
77
+ }
78
+ return false;
79
+ }
80
+ throw new Error("No wasm support");
81
+ }
82
+ };
83
+ export {
84
+ Elliptic
85
+ };
86
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/Elliptic.ts"],"sourcesContent":["import type { Secp256k1 } from '@bitauth/libauth'\nimport { instantiateSecp256k1 } from '@bitauth/libauth'\nimport { toUint8Array } from '@xylabs/arraybuffer'\nimport { assertEx } from '@xylabs/assert'\nimport { toHex } from '@xylabs/hex'\nimport { Data } from '@xyo-network/data'\nimport { WasmSupport } from '@xyo-network/wasm'\nimport { Mutex } from 'async-mutex'\n\nconst wasmSupportStatic = new WasmSupport(['bigInt', 'mutableGlobals', 'referenceTypes', 'saturatedFloatToInt', 'signExtensions', 'simd'])\nconst recoveryIds = [0, 1, 2, 3] as const\n\nfunction compareArrayBuffers(b1: ArrayBufferLike, b2: ArrayBufferLike) {\n if (b1.byteLength !== b2.byteLength) {\n return false\n }\n\n const a1 = new Uint8Array(b1)\n const a2 = new Uint8Array(b2)\n\n for (let i = 0; i < b1.byteLength; i++) {\n if (a1[1] !== a2[1]) {\n return false\n }\n }\n\n return true\n}\n\nexport class Elliptic {\n static readonly wasmSupport = wasmSupportStatic\n protected static _secp256k1: Secp256k1 | undefined\n private static _secp256k1Mutex = new Mutex()\n\n static addressFromPublicKey(key: ArrayBufferLike): ArrayBufferLike {\n return new Data(64, key).keccak256.slice(12)\n }\n\n static initialize() {\n return this.secp256k1()\n }\n\n static async publicKeyFromPrivateKey(privateKey: ArrayBufferLike, prefix = false): Promise<ArrayBufferLike> {\n const { derivePublicKeyUncompressed } = await this.secp256k1()\n if (BigInt(toHex(privateKey, { prefix: true })) === 0n) {\n throw new Error(`Invalid private key [${toHex(privateKey)}]`)\n }\n const derivedPublicKey = derivePublicKeyUncompressed(new Uint8Array(privateKey))\n const fullPublicKey = typeof derivedPublicKey === 'string' ? toUint8Array(derivedPublicKey) : derivedPublicKey\n return (prefix ? fullPublicKey : fullPublicKey.slice(1)).buffer\n }\n\n static ready() {\n return !!this._secp256k1\n }\n\n static async secp256k1(): Promise<Secp256k1> {\n return await this._secp256k1Mutex.runExclusive(async () => {\n if (this._secp256k1) return this._secp256k1\n\n await wasmSupportStatic.initialize()\n const secp256k1 = assertEx(await instantiateSecp256k1(), () => 'No Wasm Support')\n this._secp256k1 = secp256k1\n return secp256k1\n })\n }\n\n static async sign(hash: ArrayBufferLike, key: ArrayBufferLike) {\n const { signMessageHashCompact } = await this.secp256k1()\n const signature = signMessageHashCompact(new Uint8Array(key), toUint8Array(hash))\n return (typeof signature === 'string' ? toUint8Array(signature) : signature).buffer\n }\n\n static async verify(msg: ArrayBufferLike, signature: ArrayBufferLike, address: ArrayBufferLike) {\n const verifier = await this.secp256k1()\n if (verifier && this.wasmSupport.canUseWasm) {\n for (const recoveryId of recoveryIds) {\n try {\n const rawRecoveredPublicKey = verifier.recoverPublicKeyUncompressed(toUint8Array(signature), recoveryId, toUint8Array(msg))\n const recoveredPublicKey = (typeof rawRecoveredPublicKey === 'string' ? toUint8Array(rawRecoveredPublicKey) : rawRecoveredPublicKey).slice(1).buffer\n const recoveredAddress = this.addressFromPublicKey(recoveredPublicKey)\n if (compareArrayBuffers(address, recoveredAddress)) {\n return true\n }\n } catch (ex) {\n const error = ex as Error\n console.log(error.message)\n console.log(error.stack)\n continue\n }\n }\n return false\n }\n // In all failure modes default to the JS implementation\n throw new Error('No wasm support')\n }\n}\n"],"mappings":";AACA,SAAS,4BAA4B;AACrC,SAAS,oBAAoB;AAC7B,SAAS,gBAAgB;AACzB,SAAS,aAAa;AACtB,SAAS,YAAY;AACrB,SAAS,mBAAmB;AAC5B,SAAS,aAAa;AAEtB,IAAM,oBAAoB,IAAI,YAAY,CAAC,UAAU,kBAAkB,kBAAkB,uBAAuB,kBAAkB,MAAM,CAAC;AACzI,IAAM,cAAc,CAAC,GAAG,GAAG,GAAG,CAAC;AAE/B,SAAS,oBAAoB,IAAqB,IAAqB;AACrE,MAAI,GAAG,eAAe,GAAG,YAAY;AACnC,WAAO;AAAA,EACT;AAEA,QAAM,KAAK,IAAI,WAAW,EAAE;AAC5B,QAAM,KAAK,IAAI,WAAW,EAAE;AAE5B,WAAS,IAAI,GAAG,IAAI,GAAG,YAAY,KAAK;AACtC,QAAI,GAAG,CAAC,MAAM,GAAG,CAAC,GAAG;AACnB,aAAO;AAAA,IACT;AAAA,EACF;AAEA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IAAI,WAAW;AAAA,EAC/E;AAAA,EAEA,aAAa,OAAO,KAAsB,WAA4B,SAA0B;AAC9F,UAAM,WAAW,MAAM,KAAK,UAAU;AACtC,QAAI,YAAY,KAAK,YAAY,YAAY;AAC3C,iBAAW,cAAc,aAAa;AACpC,YAAI;AACF,gBAAM,wBAAwB,SAAS,6BAA6B,aAAa,SAAS,GAAG,YAAY,aAAa,GAAG,CAAC;AAC1H,gBAAM,sBAAsB,OAAO,0BAA0B,WAAW,aAAa,qBAAqB,IAAI,uBAAuB,MAAM,CAAC,EAAE;AAC9I,gBAAM,mBAAmB,KAAK,qBAAqB,kBAAkB;AACrE,cAAI,oBAAoB,SAAS,gBAAgB,GAAG;AAClD,mBAAO;AAAA,UACT;AAAA,QACF,SAAS,IAAI;AACX,gBAAM,QAAQ;AACd,kBAAQ,IAAI,MAAM,OAAO;AACzB,kBAAQ,IAAI,MAAM,KAAK;AACvB;AAAA,QACF;AAAA,MACF;AACA,aAAO;AAAA,IACT;AAEA,UAAM,IAAI,MAAM,iBAAiB;AAAA,EACnC;AACF;","names":[]}
@@ -0,0 +1,86 @@
1
+ // src/Elliptic.ts
2
+ import { instantiateSecp256k1 } from "@bitauth/libauth";
3
+ import { toUint8Array } from "@xylabs/arraybuffer";
4
+ import { assertEx } from "@xylabs/assert";
5
+ import { toHex } from "@xylabs/hex";
6
+ import { Data } from "@xyo-network/data";
7
+ import { WasmSupport } from "@xyo-network/wasm";
8
+ import { Mutex } from "async-mutex";
9
+ var wasmSupportStatic = new WasmSupport(["bigInt", "mutableGlobals", "referenceTypes", "saturatedFloatToInt", "signExtensions", "simd"]);
10
+ var recoveryIds = [0, 1, 2, 3];
11
+ function compareArrayBuffers(b1, b2) {
12
+ if (b1.byteLength !== b2.byteLength) {
13
+ return false;
14
+ }
15
+ const a1 = new Uint8Array(b1);
16
+ const a2 = new Uint8Array(b2);
17
+ for (let i = 0; i < b1.byteLength; i++) {
18
+ if (a1[1] !== a2[1]) {
19
+ return false;
20
+ }
21
+ }
22
+ return true;
23
+ }
24
+ var Elliptic = class {
25
+ static wasmSupport = wasmSupportStatic;
26
+ static _secp256k1;
27
+ static _secp256k1Mutex = new Mutex();
28
+ static addressFromPublicKey(key) {
29
+ return new Data(64, key).keccak256.slice(12);
30
+ }
31
+ static initialize() {
32
+ return this.secp256k1();
33
+ }
34
+ static async publicKeyFromPrivateKey(privateKey, prefix = false) {
35
+ const { derivePublicKeyUncompressed } = await this.secp256k1();
36
+ if (BigInt(toHex(privateKey, { prefix: true })) === 0n) {
37
+ throw new Error(`Invalid private key [${toHex(privateKey)}]`);
38
+ }
39
+ const derivedPublicKey = derivePublicKeyUncompressed(new Uint8Array(privateKey));
40
+ const fullPublicKey = typeof derivedPublicKey === "string" ? toUint8Array(derivedPublicKey) : derivedPublicKey;
41
+ return (prefix ? fullPublicKey : fullPublicKey.slice(1)).buffer;
42
+ }
43
+ static ready() {
44
+ return !!this._secp256k1;
45
+ }
46
+ static async secp256k1() {
47
+ return await this._secp256k1Mutex.runExclusive(async () => {
48
+ if (this._secp256k1) return this._secp256k1;
49
+ await wasmSupportStatic.initialize();
50
+ const secp256k1 = assertEx(await instantiateSecp256k1(), () => "No Wasm Support");
51
+ this._secp256k1 = secp256k1;
52
+ return secp256k1;
53
+ });
54
+ }
55
+ static async sign(hash, key) {
56
+ const { signMessageHashCompact } = await this.secp256k1();
57
+ const signature = signMessageHashCompact(new Uint8Array(key), toUint8Array(hash));
58
+ return (typeof signature === "string" ? toUint8Array(signature) : signature).buffer;
59
+ }
60
+ static async verify(msg, signature, address) {
61
+ const verifier = await this.secp256k1();
62
+ if (verifier && this.wasmSupport.canUseWasm) {
63
+ for (const recoveryId of recoveryIds) {
64
+ try {
65
+ const rawRecoveredPublicKey = verifier.recoverPublicKeyUncompressed(toUint8Array(signature), recoveryId, toUint8Array(msg));
66
+ const recoveredPublicKey = (typeof rawRecoveredPublicKey === "string" ? toUint8Array(rawRecoveredPublicKey) : rawRecoveredPublicKey).slice(1).buffer;
67
+ const recoveredAddress = this.addressFromPublicKey(recoveredPublicKey);
68
+ if (compareArrayBuffers(address, recoveredAddress)) {
69
+ return true;
70
+ }
71
+ } catch (ex) {
72
+ const error = ex;
73
+ console.log(error.message);
74
+ console.log(error.stack);
75
+ continue;
76
+ }
77
+ }
78
+ return false;
79
+ }
80
+ throw new Error("No wasm support");
81
+ }
82
+ };
83
+ export {
84
+ Elliptic
85
+ };
86
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/Elliptic.ts"],"sourcesContent":["import type { Secp256k1 } from '@bitauth/libauth'\nimport { instantiateSecp256k1 } from '@bitauth/libauth'\nimport { toUint8Array } from '@xylabs/arraybuffer'\nimport { assertEx } from '@xylabs/assert'\nimport { toHex } from '@xylabs/hex'\nimport { Data } from '@xyo-network/data'\nimport { WasmSupport } from '@xyo-network/wasm'\nimport { Mutex } from 'async-mutex'\n\nconst wasmSupportStatic = new WasmSupport(['bigInt', 'mutableGlobals', 'referenceTypes', 'saturatedFloatToInt', 'signExtensions', 'simd'])\nconst recoveryIds = [0, 1, 2, 3] as const\n\nfunction compareArrayBuffers(b1: ArrayBufferLike, b2: ArrayBufferLike) {\n if (b1.byteLength !== b2.byteLength) {\n return false\n }\n\n const a1 = new Uint8Array(b1)\n const a2 = new Uint8Array(b2)\n\n for (let i = 0; i < b1.byteLength; i++) {\n if (a1[1] !== a2[1]) {\n return false\n }\n }\n\n return true\n}\n\nexport class Elliptic {\n static readonly wasmSupport = wasmSupportStatic\n protected static _secp256k1: Secp256k1 | undefined\n private static _secp256k1Mutex = new Mutex()\n\n static addressFromPublicKey(key: ArrayBufferLike): ArrayBufferLike {\n return new Data(64, key).keccak256.slice(12)\n }\n\n static initialize() {\n return this.secp256k1()\n }\n\n static async publicKeyFromPrivateKey(privateKey: ArrayBufferLike, prefix = false): Promise<ArrayBufferLike> {\n const { derivePublicKeyUncompressed } = await this.secp256k1()\n if (BigInt(toHex(privateKey, { prefix: true })) === 0n) {\n throw new Error(`Invalid private key [${toHex(privateKey)}]`)\n }\n const derivedPublicKey = derivePublicKeyUncompressed(new Uint8Array(privateKey))\n const fullPublicKey = typeof derivedPublicKey === 'string' ? toUint8Array(derivedPublicKey) : derivedPublicKey\n return (prefix ? fullPublicKey : fullPublicKey.slice(1)).buffer\n }\n\n static ready() {\n return !!this._secp256k1\n }\n\n static async secp256k1(): Promise<Secp256k1> {\n return await this._secp256k1Mutex.runExclusive(async () => {\n if (this._secp256k1) return this._secp256k1\n\n await wasmSupportStatic.initialize()\n const secp256k1 = assertEx(await instantiateSecp256k1(), () => 'No Wasm Support')\n this._secp256k1 = secp256k1\n return secp256k1\n })\n }\n\n static async sign(hash: ArrayBufferLike, key: ArrayBufferLike) {\n const { signMessageHashCompact } = await this.secp256k1()\n const signature = signMessageHashCompact(new Uint8Array(key), toUint8Array(hash))\n return (typeof signature === 'string' ? toUint8Array(signature) : signature).buffer\n }\n\n static async verify(msg: ArrayBufferLike, signature: ArrayBufferLike, address: ArrayBufferLike) {\n const verifier = await this.secp256k1()\n if (verifier && this.wasmSupport.canUseWasm) {\n for (const recoveryId of recoveryIds) {\n try {\n const rawRecoveredPublicKey = verifier.recoverPublicKeyUncompressed(toUint8Array(signature), recoveryId, toUint8Array(msg))\n const recoveredPublicKey = (typeof rawRecoveredPublicKey === 'string' ? toUint8Array(rawRecoveredPublicKey) : rawRecoveredPublicKey).slice(1).buffer\n const recoveredAddress = this.addressFromPublicKey(recoveredPublicKey)\n if (compareArrayBuffers(address, recoveredAddress)) {\n return true\n }\n } catch (ex) {\n const error = ex as Error\n console.log(error.message)\n console.log(error.stack)\n continue\n }\n }\n return false\n }\n // In all failure modes default to the JS implementation\n throw new Error('No wasm support')\n }\n}\n"],"mappings":";AACA,SAAS,4BAA4B;AACrC,SAAS,oBAAoB;AAC7B,SAAS,gBAAgB;AACzB,SAAS,aAAa;AACtB,SAAS,YAAY;AACrB,SAAS,mBAAmB;AAC5B,SAAS,aAAa;AA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{ assertEx } from "@xylabs/assert";
5
+ import { toHex } from "@xylabs/hex";
6
+ import { Data } from "@xyo-network/data";
7
+ import { WasmSupport } from "@xyo-network/wasm";
8
+ import { Mutex } from "async-mutex";
9
+ var wasmSupportStatic = new WasmSupport(["bigInt", "mutableGlobals", "referenceTypes", "saturatedFloatToInt", "signExtensions", "simd"]);
10
+ var recoveryIds = [0, 1, 2, 3];
11
+ function compareArrayBuffers(b1, b2) {
12
+ if (b1.byteLength !== b2.byteLength) {
13
+ return false;
14
+ }
15
+ const a1 = new Uint8Array(b1);
16
+ const a2 = new Uint8Array(b2);
17
+ for (let i = 0; i < b1.byteLength; i++) {
18
+ if (a1[1] !== a2[1]) {
19
+ return false;
20
+ }
21
+ }
22
+ return true;
23
+ }
24
+ var Elliptic = class {
25
+ static wasmSupport = wasmSupportStatic;
26
+ static _secp256k1;
27
+ static _secp256k1Mutex = new Mutex();
28
+ static addressFromPublicKey(key) {
29
+ return new Data(64, key).keccak256.slice(12);
30
+ }
31
+ static initialize() {
32
+ return this.secp256k1();
33
+ }
34
+ static async publicKeyFromPrivateKey(privateKey, prefix = false) {
35
+ const { derivePublicKeyUncompressed } = await this.secp256k1();
36
+ if (BigInt(toHex(privateKey, { prefix: true })) === 0n) {
37
+ throw new Error(`Invalid private key [${toHex(privateKey)}]`);
38
+ }
39
+ const derivedPublicKey = derivePublicKeyUncompressed(new Uint8Array(privateKey));
40
+ const fullPublicKey = typeof derivedPublicKey === "string" ? toUint8Array(derivedPublicKey) : derivedPublicKey;
41
+ return (prefix ? fullPublicKey : fullPublicKey.slice(1)).buffer;
42
+ }
43
+ static ready() {
44
+ return !!this._secp256k1;
45
+ }
46
+ static async secp256k1() {
47
+ return await this._secp256k1Mutex.runExclusive(async () => {
48
+ if (this._secp256k1) return this._secp256k1;
49
+ await wasmSupportStatic.initialize();
50
+ const secp256k1 = assertEx(await instantiateSecp256k1(), () => "No Wasm Support");
51
+ this._secp256k1 = secp256k1;
52
+ return secp256k1;
53
+ });
54
+ }
55
+ static async sign(hash, key) {
56
+ const { signMessageHashCompact } = await this.secp256k1();
57
+ const signature = signMessageHashCompact(new Uint8Array(key), toUint8Array(hash));
58
+ return (typeof signature === "string" ? toUint8Array(signature) : signature).buffer;
59
+ }
60
+ static async verify(msg, signature, address) {
61
+ const verifier = await this.secp256k1();
62
+ if (verifier && this.wasmSupport.canUseWasm) {
63
+ for (const recoveryId of recoveryIds) {
64
+ try {
65
+ const rawRecoveredPublicKey = verifier.recoverPublicKeyUncompressed(toUint8Array(signature), recoveryId, toUint8Array(msg));
66
+ const recoveredPublicKey = (typeof rawRecoveredPublicKey === "string" ? toUint8Array(rawRecoveredPublicKey) : rawRecoveredPublicKey).slice(1).buffer;
67
+ const recoveredAddress = this.addressFromPublicKey(recoveredPublicKey);
68
+ if (compareArrayBuffers(address, recoveredAddress)) {
69
+ return true;
70
+ }
71
+ } catch (ex) {
72
+ const error = ex;
73
+ console.log(error.message);
74
+ console.log(error.stack);
75
+ continue;
76
+ }
77
+ }
78
+ return false;
79
+ }
80
+ throw new Error("No wasm support");
81
+ }
82
+ };
83
+ export {
84
+ Elliptic
85
+ };
86
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/Elliptic.ts"],"sourcesContent":["import type { Secp256k1 } from '@bitauth/libauth'\nimport { instantiateSecp256k1 } from '@bitauth/libauth'\nimport { toUint8Array } from '@xylabs/arraybuffer'\nimport { assertEx } from '@xylabs/assert'\nimport { toHex } from '@xylabs/hex'\nimport { Data } from '@xyo-network/data'\nimport { WasmSupport } from '@xyo-network/wasm'\nimport { Mutex } from 'async-mutex'\n\nconst wasmSupportStatic = new WasmSupport(['bigInt', 'mutableGlobals', 'referenceTypes', 'saturatedFloatToInt', 'signExtensions', 'simd'])\nconst recoveryIds = [0, 1, 2, 3] as const\n\nfunction compareArrayBuffers(b1: ArrayBufferLike, b2: ArrayBufferLike) {\n if (b1.byteLength !== b2.byteLength) {\n return false\n }\n\n const a1 = new Uint8Array(b1)\n const a2 = new Uint8Array(b2)\n\n for (let i = 0; i < b1.byteLength; i++) {\n if (a1[1] !== a2[1]) {\n return false\n }\n }\n\n return true\n}\n\nexport class Elliptic {\n static readonly wasmSupport = wasmSupportStatic\n protected static _secp256k1: Secp256k1 | undefined\n private static _secp256k1Mutex = new Mutex()\n\n static addressFromPublicKey(key: ArrayBufferLike): ArrayBufferLike {\n return new Data(64, key).keccak256.slice(12)\n }\n\n static initialize() {\n return this.secp256k1()\n }\n\n static async publicKeyFromPrivateKey(privateKey: ArrayBufferLike, prefix = false): Promise<ArrayBufferLike> {\n const { derivePublicKeyUncompressed } = await this.secp256k1()\n if (BigInt(toHex(privateKey, { prefix: true })) === 0n) {\n throw new Error(`Invalid private key [${toHex(privateKey)}]`)\n }\n const derivedPublicKey = derivePublicKeyUncompressed(new Uint8Array(privateKey))\n const fullPublicKey = typeof derivedPublicKey === 'string' ? toUint8Array(derivedPublicKey) : derivedPublicKey\n return (prefix ? fullPublicKey : fullPublicKey.slice(1)).buffer\n }\n\n static ready() {\n return !!this._secp256k1\n }\n\n static async secp256k1(): Promise<Secp256k1> {\n return await this._secp256k1Mutex.runExclusive(async () => {\n if (this._secp256k1) return this._secp256k1\n\n await wasmSupportStatic.initialize()\n const secp256k1 = assertEx(await instantiateSecp256k1(), () => 'No Wasm Support')\n this._secp256k1 = secp256k1\n return secp256k1\n })\n }\n\n static async sign(hash: ArrayBufferLike, key: ArrayBufferLike) {\n const { signMessageHashCompact } = await this.secp256k1()\n const signature = signMessageHashCompact(new Uint8Array(key), toUint8Array(hash))\n return (typeof signature === 'string' ? toUint8Array(signature) : signature).buffer\n }\n\n static async verify(msg: ArrayBufferLike, signature: ArrayBufferLike, address: ArrayBufferLike) {\n const verifier = await this.secp256k1()\n if (verifier && this.wasmSupport.canUseWasm) {\n for (const recoveryId of recoveryIds) {\n try {\n const rawRecoveredPublicKey = verifier.recoverPublicKeyUncompressed(toUint8Array(signature), recoveryId, toUint8Array(msg))\n const recoveredPublicKey = (typeof rawRecoveredPublicKey === 'string' ? toUint8Array(rawRecoveredPublicKey) : rawRecoveredPublicKey).slice(1).buffer\n const recoveredAddress = this.addressFromPublicKey(recoveredPublicKey)\n if (compareArrayBuffers(address, recoveredAddress)) {\n return true\n }\n } catch (ex) {\n const error = ex as Error\n console.log(error.message)\n console.log(error.stack)\n continue\n }\n }\n return false\n }\n // In all failure modes default to the JS implementation\n throw new Error('No wasm support')\n }\n}\n"],"mappings":";AACA,SAAS,4BAA4B;AACrC,SAAS,oBAAoB;AAC7B,SAAS,gBAAgB;AACzB,SAAS,aAAa;AACtB,SAAS,YAAY;AACrB,SAAS,mBAAmB;AAC5B,SAAS,aAAa;AAEtB,IAAM,oBAAoB,IAAI,YAAY,CAAC,UAAU,kBAAkB,kBAAkB,uBAAuB,kBAAkB,MAAM,CAAC;AACzI,IAAM,cAAc,CAAC,GAAG,GAAG,GAAG,CAAC;AAE/B,SAAS,oBAAoB,IAAqB,IAAqB;AACrE,MAAI,GAAG,eAAe,GAAG,YAAY;AACnC,WAAO;AAAA,EACT;AAEA,QAAM,KAAK,IAAI,WAAW,EAAE;AAC5B,QAAM,KAAK,IAAI,WAAW,EAAE;AAE5B,WAAS,IAAI,GAAG,IAAI,GAAG,YAAY,KAAK;AACtC,QAAI,GAAG,CAAC,MAAM,GAAG,CAAC,GAAG;AACnB,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;AAEO,IAAM,WAAN,MAAe;AAAA,EACpB,OAAgB,cAAc;AAAA,EAC9B,OAAiB;AAAA,EACjB,OAAe,kBAAkB,IAAI,MAAM;AAAA,EAE3C,OAAO,qBAAqB,KAAuC;AACjE,WAAO,IAAI,KAAK,IAAI,GAAG,EAAE,UAAU,MAAM,EAAE;AAAA,EAC7C;AAAA,EAEA,OAAO,aAAa;AAClB,WAAO,KAAK,UAAU;AAAA,EACxB;AAAA,EAEA,aAAa,wBAAwB,YAA6B,SAAS,OAAiC;AAC1G,UAAM,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;AAC9I,gBAAM,mBAAmB,KAAK,qBAAqB,kBAAkB;AACrE,cAAI,oBAAoB,SAAS,gBAAgB,GAAG;AAClD,mBAAO;AAAA,UACT;AAAA,QACF,SAAS,IAAI;AACX,gBAAM,QAAQ;AACd,kBAAQ,IAAI,MAAM,OAAO;AACzB,kBAAQ,IAAI,MAAM,KAAK;AACvB;AAAA,QACF;AAAA,MACF;AACA,aAAO;AAAA,IACT;AAEA,UAAM,IAAI,MAAM,iBAAiB;AAAA,EACnC;AACF;","names":[]}
@@ -0,0 +1,15 @@
1
+ import type { Secp256k1 } from '@bitauth/libauth';
2
+ import { WasmSupport } from '@xyo-network/wasm';
3
+ export declare class Elliptic {
4
+ static readonly wasmSupport: WasmSupport;
5
+ protected static _secp256k1: Secp256k1 | undefined;
6
+ private static _secp256k1Mutex;
7
+ static addressFromPublicKey(key: ArrayBufferLike): ArrayBufferLike;
8
+ static initialize(): Promise<Secp256k1>;
9
+ static publicKeyFromPrivateKey(privateKey: ArrayBufferLike, prefix?: boolean): Promise<ArrayBufferLike>;
10
+ static ready(): boolean;
11
+ static secp256k1(): Promise<Secp256k1>;
12
+ static sign(hash: ArrayBufferLike, key: ArrayBufferLike): Promise<ArrayBufferLike>;
13
+ static verify(msg: ArrayBufferLike, signature: ArrayBufferLike, address: ArrayBufferLike): Promise<boolean>;
14
+ }
15
+ //# sourceMappingURL=Elliptic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Elliptic.d.ts","sourceRoot":"","sources":["../../src/Elliptic.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAMjD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAuB/C,qBAAa,QAAQ;IACnB,MAAM,CAAC,QAAQ,CAAC,WAAW,cAAoB;IAC/C,SAAS,CAAC,MAAM,CAAC,UAAU,EAAE,SAAS,GAAG,SAAS,CAAA;IAClD,OAAO,CAAC,MAAM,CAAC,eAAe,CAAc;IAE5C,MAAM,CAAC,oBAAoB,CAAC,GAAG,EAAE,eAAe,GAAG,eAAe;IAIlE,MAAM,CAAC,UAAU;WAIJ,uBAAuB,CAAC,UAAU,EAAE,eAAe,EAAE,MAAM,UAAQ,GAAG,OAAO,CAAC,eAAe,CAAC;IAU3G,MAAM,CAAC,KAAK;WAIC,SAAS,IAAI,OAAO,CAAC,SAAS,CAAC;WAW/B,IAAI,CAAC,IAAI,EAAE,eAAe,EAAE,GAAG,EAAE,eAAe;WAMhD,MAAM,CAAC,GAAG,EAAE,eAAe,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,eAAe;CAuB/F"}
@@ -0,0 +1,2 @@
1
+ export * from './Elliptic.ts';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@xyo-network/elliptic",
3
+ "version": "3.14.5",
4
+ "description": "Primary SDK for using XYO Protocol 2.0",
5
+ "homepage": "https://xyo.network",
6
+ "bugs": {
7
+ "url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues",
8
+ "email": "support@xyo.network"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
13
+ },
14
+ "license": "LGPL-3.0-only",
15
+ "author": {
16
+ "name": "XYO Development Team",
17
+ "email": "support@xyo.network",
18
+ "url": "https://xyo.network"
19
+ },
20
+ "sideEffects": false,
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/types/index.d.ts",
25
+ "default": "./dist/neutral/index.mjs"
26
+ },
27
+ "./package.json": "./package.json"
28
+ },
29
+ "module": "dist/neutral/index.mjs",
30
+ "types": "dist/types/index.d.ts",
31
+ "dependencies": {
32
+ "@xylabs/hex": "^4.8.7",
33
+ "@xyo-network/data": "^3.14.5"
34
+ },
35
+ "devDependencies": {
36
+ "@xylabs/ts-scripts-yarn3": "^6.3.5",
37
+ "@xylabs/tsconfig": "^6.3.5",
38
+ "typescript": "^5.8.3"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ }
43
+ }
@@ -0,0 +1,97 @@
1
+ import type { Secp256k1 } from '@bitauth/libauth'
2
+ import { instantiateSecp256k1 } from '@bitauth/libauth'
3
+ import { toUint8Array } from '@xylabs/arraybuffer'
4
+ import { assertEx } from '@xylabs/assert'
5
+ import { toHex } from '@xylabs/hex'
6
+ import { Data } from '@xyo-network/data'
7
+ import { WasmSupport } from '@xyo-network/wasm'
8
+ import { Mutex } from 'async-mutex'
9
+
10
+ const wasmSupportStatic = new WasmSupport(['bigInt', 'mutableGlobals', 'referenceTypes', 'saturatedFloatToInt', 'signExtensions', 'simd'])
11
+ const recoveryIds = [0, 1, 2, 3] as const
12
+
13
+ function compareArrayBuffers(b1: ArrayBufferLike, b2: ArrayBufferLike) {
14
+ if (b1.byteLength !== b2.byteLength) {
15
+ return false
16
+ }
17
+
18
+ const a1 = new Uint8Array(b1)
19
+ const a2 = new Uint8Array(b2)
20
+
21
+ for (let i = 0; i < b1.byteLength; i++) {
22
+ if (a1[1] !== a2[1]) {
23
+ return false
24
+ }
25
+ }
26
+
27
+ return true
28
+ }
29
+
30
+ export class Elliptic {
31
+ static readonly wasmSupport = wasmSupportStatic
32
+ protected static _secp256k1: Secp256k1 | undefined
33
+ private static _secp256k1Mutex = new Mutex()
34
+
35
+ static addressFromPublicKey(key: ArrayBufferLike): ArrayBufferLike {
36
+ return new Data(64, key).keccak256.slice(12)
37
+ }
38
+
39
+ static initialize() {
40
+ return this.secp256k1()
41
+ }
42
+
43
+ static async publicKeyFromPrivateKey(privateKey: ArrayBufferLike, prefix = false): Promise<ArrayBufferLike> {
44
+ const { derivePublicKeyUncompressed } = await this.secp256k1()
45
+ if (BigInt(toHex(privateKey, { prefix: true })) === 0n) {
46
+ throw new Error(`Invalid private key [${toHex(privateKey)}]`)
47
+ }
48
+ const derivedPublicKey = derivePublicKeyUncompressed(new Uint8Array(privateKey))
49
+ const fullPublicKey = typeof derivedPublicKey === 'string' ? toUint8Array(derivedPublicKey) : derivedPublicKey
50
+ return (prefix ? fullPublicKey : fullPublicKey.slice(1)).buffer
51
+ }
52
+
53
+ static ready() {
54
+ return !!this._secp256k1
55
+ }
56
+
57
+ static async secp256k1(): Promise<Secp256k1> {
58
+ return await this._secp256k1Mutex.runExclusive(async () => {
59
+ if (this._secp256k1) return this._secp256k1
60
+
61
+ await wasmSupportStatic.initialize()
62
+ const secp256k1 = assertEx(await instantiateSecp256k1(), () => 'No Wasm Support')
63
+ this._secp256k1 = secp256k1
64
+ return secp256k1
65
+ })
66
+ }
67
+
68
+ static async sign(hash: ArrayBufferLike, key: ArrayBufferLike) {
69
+ const { signMessageHashCompact } = await this.secp256k1()
70
+ const signature = signMessageHashCompact(new Uint8Array(key), toUint8Array(hash))
71
+ return (typeof signature === 'string' ? toUint8Array(signature) : signature).buffer
72
+ }
73
+
74
+ static async verify(msg: ArrayBufferLike, signature: ArrayBufferLike, address: ArrayBufferLike) {
75
+ const verifier = await this.secp256k1()
76
+ if (verifier && this.wasmSupport.canUseWasm) {
77
+ for (const recoveryId of recoveryIds) {
78
+ try {
79
+ const rawRecoveredPublicKey = verifier.recoverPublicKeyUncompressed(toUint8Array(signature), recoveryId, toUint8Array(msg))
80
+ const recoveredPublicKey = (typeof rawRecoveredPublicKey === 'string' ? toUint8Array(rawRecoveredPublicKey) : rawRecoveredPublicKey).slice(1).buffer
81
+ const recoveredAddress = this.addressFromPublicKey(recoveredPublicKey)
82
+ if (compareArrayBuffers(address, recoveredAddress)) {
83
+ return true
84
+ }
85
+ } catch (ex) {
86
+ const error = ex as Error
87
+ console.log(error.message)
88
+ console.log(error.stack)
89
+ continue
90
+ }
91
+ }
92
+ return false
93
+ }
94
+ // In all failure modes default to the JS implementation
95
+ throw new Error('No wasm support')
96
+ }
97
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './Elliptic.ts'
package/xy.config.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type { XyTsupConfig } from '@xylabs/ts-scripts-yarn3'
2
+ const config: XyTsupConfig = {
3
+ compile: {
4
+ browser: { src: true },
5
+ node: { src: true },
6
+ },
7
+ }
8
+
9
+ export default config