@layerzerolabs/lz-foundation 2.3.30-oft-rc.0
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/CHANGELOG.md +10 -0
- package/README.md +3 -0
- package/dist/index.cjs +151 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +46 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.mjs +121 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +46 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var blake2b$1 = require('@noble/hashes/blake2b');
|
|
4
|
+
var sha256 = require('@noble/hashes/sha256');
|
|
5
|
+
var sha3 = require('@noble/hashes/sha3');
|
|
6
|
+
var ed25519 = require('@noble/ed25519');
|
|
7
|
+
var secp256k1 = require('@noble/secp256k1');
|
|
8
|
+
|
|
9
|
+
function _interopNamespace(e) {
|
|
10
|
+
if (e && e.__esModule) return e;
|
|
11
|
+
var n = Object.create(null);
|
|
12
|
+
if (e) {
|
|
13
|
+
Object.keys(e).forEach(function (k) {
|
|
14
|
+
if (k !== 'default') {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return e[k]; }
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
n.default = e;
|
|
24
|
+
return Object.freeze(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var ed25519__namespace = /*#__PURE__*/_interopNamespace(ed25519);
|
|
28
|
+
var secp256k1__namespace = /*#__PURE__*/_interopNamespace(secp256k1);
|
|
29
|
+
|
|
30
|
+
var __defProp = Object.defineProperty;
|
|
31
|
+
var __export = (target, all) => {
|
|
32
|
+
for (var name in all)
|
|
33
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
34
|
+
};
|
|
35
|
+
function keccak_256(message) {
|
|
36
|
+
return keccak_256();
|
|
37
|
+
}
|
|
38
|
+
function blake2b(message) {
|
|
39
|
+
return blake2b$1.blake2b(message);
|
|
40
|
+
}
|
|
41
|
+
function sha3_256(message) {
|
|
42
|
+
return sha3.sha3_256(message);
|
|
43
|
+
}
|
|
44
|
+
function sha2_256(message) {
|
|
45
|
+
return sha256.sha256(message);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/sign/constant.ts
|
|
49
|
+
var SignAlgorithm = /* @__PURE__ */ ((SignAlgorithm2) => {
|
|
50
|
+
SignAlgorithm2[SignAlgorithm2["NATIVE"] = 0] = "NATIVE";
|
|
51
|
+
SignAlgorithm2[SignAlgorithm2["SECP256K1"] = 1] = "SECP256K1";
|
|
52
|
+
SignAlgorithm2[SignAlgorithm2["ED25519"] = 2] = "ED25519";
|
|
53
|
+
return SignAlgorithm2;
|
|
54
|
+
})(SignAlgorithm || {});
|
|
55
|
+
|
|
56
|
+
// src/sign/ed25519.ts
|
|
57
|
+
var ed25519_exports = {};
|
|
58
|
+
__export(ed25519_exports, {
|
|
59
|
+
getPublicKey: () => getPublicKey2,
|
|
60
|
+
signHash: () => signHash
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// src/sign/internal.ts
|
|
64
|
+
function normalizePrivateKey(privateKey) {
|
|
65
|
+
if (typeof privateKey === "string") {
|
|
66
|
+
return privateKey.replace(/^0x/, "");
|
|
67
|
+
} else if (typeof privateKey === "bigint") {
|
|
68
|
+
return privateKey.toString(16).padStart(64, "0");
|
|
69
|
+
} else {
|
|
70
|
+
return privateKey;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/sign/ed25519.ts
|
|
75
|
+
async function signHash(hash, privateKey) {
|
|
76
|
+
if (hash.length !== 32) {
|
|
77
|
+
throw new Error("Hash must be 32 bytes long");
|
|
78
|
+
}
|
|
79
|
+
privateKey = normalizePrivateKey(privateKey);
|
|
80
|
+
const pubKey = await ed25519__namespace.getPublicKey(privateKey);
|
|
81
|
+
const signature = await ed25519__namespace.sign(hash, privateKey);
|
|
82
|
+
const isValid = await ed25519__namespace.verify(signature, hash, pubKey);
|
|
83
|
+
if (!isValid) {
|
|
84
|
+
throw new Error("Invalid signature");
|
|
85
|
+
}
|
|
86
|
+
return signature;
|
|
87
|
+
}
|
|
88
|
+
async function getPublicKey2(privateKey) {
|
|
89
|
+
privateKey = normalizePrivateKey(privateKey);
|
|
90
|
+
return ed25519__namespace.getPublicKey(privateKey);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/sign/secp256k1.ts
|
|
94
|
+
var secp256k1_exports = {};
|
|
95
|
+
__export(secp256k1_exports, {
|
|
96
|
+
getPublicKey: () => getPublicKey4,
|
|
97
|
+
signHash: () => signHash2
|
|
98
|
+
});
|
|
99
|
+
async function signHash2(hash, privateKey) {
|
|
100
|
+
if (hash.length !== 32) {
|
|
101
|
+
throw new Error("Hash must be 32 bytes long");
|
|
102
|
+
}
|
|
103
|
+
privateKey = normalizePrivateKey(privateKey);
|
|
104
|
+
const pubKey = secp256k1__namespace.getPublicKey(privateKey, false);
|
|
105
|
+
const [signature, recoveryId] = await secp256k1__namespace.sign(hash, privateKey, {
|
|
106
|
+
// recovered - true indicates the recovered bit should be included in the result
|
|
107
|
+
recovered: true,
|
|
108
|
+
// canonical - true makes signatures compatible with libsecp256k1, false makes signatures compatible with openssl
|
|
109
|
+
canonical: true,
|
|
110
|
+
// der - whether the returned signature should be in DER format. If false, it would be in Compact format (32-byte r + 32-byte s)
|
|
111
|
+
der: false,
|
|
112
|
+
// extraEntropy - When true, it would automatically be filled with 32 bytes of cryptographically secure entropy. Strongly recommended to pass true to improve security:
|
|
113
|
+
extraEntropy: void 0
|
|
114
|
+
});
|
|
115
|
+
const isValid = secp256k1__namespace.verify(signature, hash, pubKey, { strict: true });
|
|
116
|
+
if (!isValid) {
|
|
117
|
+
throw new Error("Invalid signature");
|
|
118
|
+
}
|
|
119
|
+
return Uint8Array.from([...signature, recoveryId]);
|
|
120
|
+
}
|
|
121
|
+
async function getPublicKey4(privateKey) {
|
|
122
|
+
privateKey = normalizePrivateKey(privateKey);
|
|
123
|
+
return Promise.resolve(secp256k1__namespace.getPublicKey(privateKey, false));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// src/sign/index.ts
|
|
127
|
+
async function signHash3(hash, privateKey, algorithm) {
|
|
128
|
+
if (hash.length !== 32) {
|
|
129
|
+
throw new Error("Hash must be 32 bytes long");
|
|
130
|
+
}
|
|
131
|
+
const privKey = typeof privateKey === "string" ? privateKey.replace(/^0x/, "") : Buffer.from(privateKey).toString("hex");
|
|
132
|
+
switch (algorithm) {
|
|
133
|
+
case 1 /* SECP256K1 */:
|
|
134
|
+
return signHash2(hash, privKey);
|
|
135
|
+
case 2 /* ED25519 */:
|
|
136
|
+
return signHash(hash, privKey);
|
|
137
|
+
default:
|
|
138
|
+
throw new Error(`Unsupported algorithm: ${algorithm}`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
exports.SignAlgorithm = SignAlgorithm;
|
|
143
|
+
exports.blake2b = blake2b;
|
|
144
|
+
exports.ed25519 = ed25519_exports;
|
|
145
|
+
exports.keccak_256 = keccak_256;
|
|
146
|
+
exports.secp256k1 = secp256k1_exports;
|
|
147
|
+
exports.sha2_256 = sha2_256;
|
|
148
|
+
exports.sha3_256 = sha3_256;
|
|
149
|
+
exports.signHash = signHash3;
|
|
150
|
+
//# sourceMappingURL=out.js.map
|
|
151
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hash/index.ts","../src/sign/constant.ts","../src/sign/ed25519.ts","../src/sign/internal.ts","../src/sign/secp256k1.ts","../src/sign/index.ts"],"names":["SignAlgorithm","getPublicKey","signHash"],"mappings":";;;;;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A;AAAA,YAAY,eAAe;AAU3B,eAAsBA,UAAS,MAAkB,YAA+D;AAC5G,MAAI,KAAK,WAAW,IAAI;AACpB,UAAM,IAAI,MAAM,4BAA4B;AAAA,EAChD;AAEA,eAAa,oBAAoB,UAAU;AAC3C,QAAM,SAAmB,uBAAa,YAAY,KAAK;AAGvD,QAAM,CAAC,WAAW,UAAU,IAAI,MAAgB,eAAK,MAAM,YAAY;AAAA;AAAA,IAEnE,WAAW;AAAA;AAAA,IAEX,WAAW;AAAA;AAAA,IAEX,KAAK;AAAA;AAAA,IAEL,cAAc;AAAA,EAClB,CAAC;AAED,QAAM,UAAoB,iBAAO,WAAW,MAAM,QAAQ,EAAE,QAAQ,KAAK,CAAC;AAC1E,MAAI,CAAC,SAAS;AACV,UAAM,IAAI,MAAM,mBAAmB;AAAA,EACvC;AAEA,SAAO,WAAW,KAAK,CAAC,GAAG,WAAW,UAAU,CAAC;AACrD;AAEA,eAAsBD,cAAa,YAA+D;AAC9F,eAAa,oBAAoB,UAAU;AAC3C,SAAO,QAAQ,QAAkB,uBAAa,YAAY,KAAK,CAAC;AACpE;;;ACnCA,eAAsBC,UAClB,MACA,YACA,WACmB;AACnB,MAAI,KAAK,WAAW,IAAI;AACpB,UAAM,IAAI,MAAM,4BAA4B;AAAA,EAChD;AAIA,QAAM,UACF,OAAO,eAAe,WAAW,WAAW,QAAQ,OAAO,EAAE,IAAI,OAAO,KAAK,UAAU,EAAE,SAAS,KAAK;AAC3G,UAAQ,WAAW;AAAA,IACf;AACI,aAAiBA,UAAS,MAAM,OAAO;AACvC;AAAA,IACJ;AACI,aAAe,SAAS,MAAM,OAAO;AACrC;AAAA,IACJ;AACI,YAAM,IAAI,MAAM,0BAA0B,SAAS,EAAE;AAAA,EAC7D;AACJ","sourcesContent":["import { blake2b as _blake2b } from '@noble/hashes/blake2b'\nimport { sha256 as _sha256 } from '@noble/hashes/sha256'\nimport { sha3_256 as _sha3_256 } from '@noble/hashes/sha3'\n\nexport function keccak_256(message: Uint8Array | string): Uint8Array {\n return keccak_256(message)\n}\n\nexport function blake2b(message: Uint8Array | string): Uint8Array {\n return _blake2b(message)\n}\n\nexport function sha3_256(message: Uint8Array | string): Uint8Array {\n return _sha3_256(message)\n}\n\nexport function sha2_256(message: Uint8Array | string): Uint8Array {\n return _sha256(message)\n}\n","export enum SignAlgorithm {\n NATIVE,\n SECP256K1,\n ED25519,\n}\n","import * as ed25519 from '@noble/ed25519'\n\nimport { normalizePrivateKey } from './internal'\n\n/**\n * Sign a hash using ed25519\n * @param hash - 32 bytes long\n * @param privateKey - private key in hex format\n * @returns\n */\nexport async function signHash(hash: Uint8Array, privateKey: string | Uint8Array | bigint): Promise<Uint8Array> {\n if (hash.length !== 32) {\n throw new Error('Hash must be 32 bytes long')\n }\n\n privateKey = normalizePrivateKey(privateKey)\n const pubKey = await ed25519.getPublicKey(privateKey)\n const signature = await ed25519.sign(hash, privateKey)\n const isValid = await ed25519.verify(signature, hash, pubKey)\n if (!isValid) {\n throw new Error('Invalid signature')\n }\n\n return signature\n}\n\nexport async function getPublicKey(privateKey: string | Uint8Array | bigint): Promise<Uint8Array> {\n privateKey = normalizePrivateKey(privateKey)\n return ed25519.getPublicKey(privateKey)\n}\n","/**\n * Normalize the private key to a string or Uint8Array\n * - getPublicKey accepts bigint but sign does not, in ed25519\n * - a string privateKey should not have a 0x prefix, in ed25519\n * @param privateKey\n */\nexport function normalizePrivateKey(privateKey: string | Uint8Array | bigint): string | Uint8Array {\n if (typeof privateKey === 'string') {\n return privateKey.replace(/^0x/, '')\n } else if (typeof privateKey === 'bigint') {\n return privateKey.toString(16).padStart(64, '0') // the private key is 32 bytes for ed25519 and secp256k1\n } else {\n return privateKey\n }\n}\n","import * as secp256k1 from '@noble/secp256k1'\n\nimport { normalizePrivateKey } from './internal'\n\n/**\n * Sign a hash using secp256k1\n * @param hash - 32 bytes long\n * @param privateKey - private key in hex format\n * @returns\n */\nexport async function signHash(hash: Uint8Array, privateKey: string | Uint8Array | bigint): Promise<Uint8Array> {\n if (hash.length !== 32) {\n throw new Error('Hash must be 32 bytes long')\n }\n\n privateKey = normalizePrivateKey(privateKey)\n const pubKey = secp256k1.getPublicKey(privateKey, false)\n\n // refer to: https://github.com/paulmillr/noble-secp256k1/tree/1.7.1#signmsghash-privatekey\n const [signature, recoveryId] = await secp256k1.sign(hash, privateKey, {\n // recovered - true indicates the recovered bit should be included in the result\n recovered: true,\n // canonical - true makes signatures compatible with libsecp256k1, false makes signatures compatible with openssl\n canonical: true,\n // der - whether the returned signature should be in DER format. If false, it would be in Compact format (32-byte r + 32-byte s)\n der: false,\n // extraEntropy - When true, it would automatically be filled with 32 bytes of cryptographically secure entropy. Strongly recommended to pass true to improve security:\n extraEntropy: undefined,\n })\n\n const isValid = secp256k1.verify(signature, hash, pubKey, { strict: true })\n if (!isValid) {\n throw new Error('Invalid signature')\n }\n\n return Uint8Array.from([...signature, recoveryId])\n}\n\nexport async function getPublicKey(privateKey: string | Uint8Array | bigint): Promise<Uint8Array> {\n privateKey = normalizePrivateKey(privateKey)\n return Promise.resolve(secp256k1.getPublicKey(privateKey, false))\n}\n","import { SignAlgorithm } from './constant'\nexport * from './keypair'\nexport * from './constant'\nimport * as ed25519 from './ed25519'\nimport * as secp256k1 from './secp256k1'\n\nexport async function signHash(\n hash: Uint8Array,\n privateKey: string | Uint8Array,\n algorithm: SignAlgorithm\n): Promise<Uint8Array> {\n if (hash.length !== 32) {\n throw new Error('Hash must be 32 bytes long')\n }\n\n // privKey should be a hex string without 0x prefix\n // refer to: https://github.com/paulmillr/noble-secp256k1/blob/5ed251242bf065aeb22a54bb42eee1f67e7c77ce/index.ts#L130\n const privKey =\n typeof privateKey === 'string' ? privateKey.replace(/^0x/, '') : Buffer.from(privateKey).toString('hex')\n switch (algorithm) {\n case SignAlgorithm.SECP256K1:\n return secp256k1.signHash(hash, privKey)\n break\n case SignAlgorithm.ED25519:\n return ed25519.signHash(hash, privKey)\n break\n default:\n throw new Error(`Unsupported algorithm: ${algorithm}`)\n }\n}\n\nexport { secp256k1, ed25519 }\n"]}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
declare function keccak_256(message: Uint8Array | string): Uint8Array;
|
|
2
|
+
declare function blake2b(message: Uint8Array | string): Uint8Array;
|
|
3
|
+
declare function sha3_256(message: Uint8Array | string): Uint8Array;
|
|
4
|
+
declare function sha2_256(message: Uint8Array | string): Uint8Array;
|
|
5
|
+
|
|
6
|
+
declare enum SignAlgorithm {
|
|
7
|
+
NATIVE = 0,
|
|
8
|
+
SECP256K1 = 1,
|
|
9
|
+
ED25519 = 2
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface KeyPair {
|
|
13
|
+
privateKey: Uint8Array;
|
|
14
|
+
publicKey: Uint8Array;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Sign a hash using ed25519
|
|
19
|
+
* @param hash - 32 bytes long
|
|
20
|
+
* @param privateKey - private key in hex format
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
declare function signHash$2(hash: Uint8Array, privateKey: string | Uint8Array | bigint): Promise<Uint8Array>;
|
|
24
|
+
declare function getPublicKey$1(privateKey: string | Uint8Array | bigint): Promise<Uint8Array>;
|
|
25
|
+
|
|
26
|
+
declare namespace ed25519 {
|
|
27
|
+
export { getPublicKey$1 as getPublicKey, signHash$2 as signHash };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Sign a hash using secp256k1
|
|
32
|
+
* @param hash - 32 bytes long
|
|
33
|
+
* @param privateKey - private key in hex format
|
|
34
|
+
* @returns
|
|
35
|
+
*/
|
|
36
|
+
declare function signHash$1(hash: Uint8Array, privateKey: string | Uint8Array | bigint): Promise<Uint8Array>;
|
|
37
|
+
declare function getPublicKey(privateKey: string | Uint8Array | bigint): Promise<Uint8Array>;
|
|
38
|
+
|
|
39
|
+
declare const secp256k1_getPublicKey: typeof getPublicKey;
|
|
40
|
+
declare namespace secp256k1 {
|
|
41
|
+
export { secp256k1_getPublicKey as getPublicKey, signHash$1 as signHash };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
declare function signHash(hash: Uint8Array, privateKey: string | Uint8Array, algorithm: SignAlgorithm): Promise<Uint8Array>;
|
|
45
|
+
|
|
46
|
+
export { type KeyPair, SignAlgorithm, blake2b, ed25519, keccak_256, secp256k1, sha2_256, sha3_256, signHash };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
declare function keccak_256(message: Uint8Array | string): Uint8Array;
|
|
2
|
+
declare function blake2b(message: Uint8Array | string): Uint8Array;
|
|
3
|
+
declare function sha3_256(message: Uint8Array | string): Uint8Array;
|
|
4
|
+
declare function sha2_256(message: Uint8Array | string): Uint8Array;
|
|
5
|
+
|
|
6
|
+
declare enum SignAlgorithm {
|
|
7
|
+
NATIVE = 0,
|
|
8
|
+
SECP256K1 = 1,
|
|
9
|
+
ED25519 = 2
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface KeyPair {
|
|
13
|
+
privateKey: Uint8Array;
|
|
14
|
+
publicKey: Uint8Array;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Sign a hash using ed25519
|
|
19
|
+
* @param hash - 32 bytes long
|
|
20
|
+
* @param privateKey - private key in hex format
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
declare function signHash$2(hash: Uint8Array, privateKey: string | Uint8Array | bigint): Promise<Uint8Array>;
|
|
24
|
+
declare function getPublicKey$1(privateKey: string | Uint8Array | bigint): Promise<Uint8Array>;
|
|
25
|
+
|
|
26
|
+
declare namespace ed25519 {
|
|
27
|
+
export { getPublicKey$1 as getPublicKey, signHash$2 as signHash };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Sign a hash using secp256k1
|
|
32
|
+
* @param hash - 32 bytes long
|
|
33
|
+
* @param privateKey - private key in hex format
|
|
34
|
+
* @returns
|
|
35
|
+
*/
|
|
36
|
+
declare function signHash$1(hash: Uint8Array, privateKey: string | Uint8Array | bigint): Promise<Uint8Array>;
|
|
37
|
+
declare function getPublicKey(privateKey: string | Uint8Array | bigint): Promise<Uint8Array>;
|
|
38
|
+
|
|
39
|
+
declare const secp256k1_getPublicKey: typeof getPublicKey;
|
|
40
|
+
declare namespace secp256k1 {
|
|
41
|
+
export { secp256k1_getPublicKey as getPublicKey, signHash$1 as signHash };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
declare function signHash(hash: Uint8Array, privateKey: string | Uint8Array, algorithm: SignAlgorithm): Promise<Uint8Array>;
|
|
45
|
+
|
|
46
|
+
export { type KeyPair, SignAlgorithm, blake2b, ed25519, keccak_256, secp256k1, sha2_256, sha3_256, signHash };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { blake2b as blake2b$1 } from '@noble/hashes/blake2b';
|
|
2
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
3
|
+
import { sha3_256 as sha3_256$1 } from '@noble/hashes/sha3';
|
|
4
|
+
import * as ed25519 from '@noble/ed25519';
|
|
5
|
+
import * as secp256k1 from '@noble/secp256k1';
|
|
6
|
+
|
|
7
|
+
var __defProp = Object.defineProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
function keccak_256(message) {
|
|
13
|
+
return keccak_256();
|
|
14
|
+
}
|
|
15
|
+
function blake2b(message) {
|
|
16
|
+
return blake2b$1(message);
|
|
17
|
+
}
|
|
18
|
+
function sha3_256(message) {
|
|
19
|
+
return sha3_256$1(message);
|
|
20
|
+
}
|
|
21
|
+
function sha2_256(message) {
|
|
22
|
+
return sha256(message);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/sign/constant.ts
|
|
26
|
+
var SignAlgorithm = /* @__PURE__ */ ((SignAlgorithm2) => {
|
|
27
|
+
SignAlgorithm2[SignAlgorithm2["NATIVE"] = 0] = "NATIVE";
|
|
28
|
+
SignAlgorithm2[SignAlgorithm2["SECP256K1"] = 1] = "SECP256K1";
|
|
29
|
+
SignAlgorithm2[SignAlgorithm2["ED25519"] = 2] = "ED25519";
|
|
30
|
+
return SignAlgorithm2;
|
|
31
|
+
})(SignAlgorithm || {});
|
|
32
|
+
|
|
33
|
+
// src/sign/ed25519.ts
|
|
34
|
+
var ed25519_exports = {};
|
|
35
|
+
__export(ed25519_exports, {
|
|
36
|
+
getPublicKey: () => getPublicKey2,
|
|
37
|
+
signHash: () => signHash
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// src/sign/internal.ts
|
|
41
|
+
function normalizePrivateKey(privateKey) {
|
|
42
|
+
if (typeof privateKey === "string") {
|
|
43
|
+
return privateKey.replace(/^0x/, "");
|
|
44
|
+
} else if (typeof privateKey === "bigint") {
|
|
45
|
+
return privateKey.toString(16).padStart(64, "0");
|
|
46
|
+
} else {
|
|
47
|
+
return privateKey;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/sign/ed25519.ts
|
|
52
|
+
async function signHash(hash, privateKey) {
|
|
53
|
+
if (hash.length !== 32) {
|
|
54
|
+
throw new Error("Hash must be 32 bytes long");
|
|
55
|
+
}
|
|
56
|
+
privateKey = normalizePrivateKey(privateKey);
|
|
57
|
+
const pubKey = await ed25519.getPublicKey(privateKey);
|
|
58
|
+
const signature = await ed25519.sign(hash, privateKey);
|
|
59
|
+
const isValid = await ed25519.verify(signature, hash, pubKey);
|
|
60
|
+
if (!isValid) {
|
|
61
|
+
throw new Error("Invalid signature");
|
|
62
|
+
}
|
|
63
|
+
return signature;
|
|
64
|
+
}
|
|
65
|
+
async function getPublicKey2(privateKey) {
|
|
66
|
+
privateKey = normalizePrivateKey(privateKey);
|
|
67
|
+
return ed25519.getPublicKey(privateKey);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/sign/secp256k1.ts
|
|
71
|
+
var secp256k1_exports = {};
|
|
72
|
+
__export(secp256k1_exports, {
|
|
73
|
+
getPublicKey: () => getPublicKey4,
|
|
74
|
+
signHash: () => signHash2
|
|
75
|
+
});
|
|
76
|
+
async function signHash2(hash, privateKey) {
|
|
77
|
+
if (hash.length !== 32) {
|
|
78
|
+
throw new Error("Hash must be 32 bytes long");
|
|
79
|
+
}
|
|
80
|
+
privateKey = normalizePrivateKey(privateKey);
|
|
81
|
+
const pubKey = secp256k1.getPublicKey(privateKey, false);
|
|
82
|
+
const [signature, recoveryId] = await secp256k1.sign(hash, privateKey, {
|
|
83
|
+
// recovered - true indicates the recovered bit should be included in the result
|
|
84
|
+
recovered: true,
|
|
85
|
+
// canonical - true makes signatures compatible with libsecp256k1, false makes signatures compatible with openssl
|
|
86
|
+
canonical: true,
|
|
87
|
+
// der - whether the returned signature should be in DER format. If false, it would be in Compact format (32-byte r + 32-byte s)
|
|
88
|
+
der: false,
|
|
89
|
+
// extraEntropy - When true, it would automatically be filled with 32 bytes of cryptographically secure entropy. Strongly recommended to pass true to improve security:
|
|
90
|
+
extraEntropy: void 0
|
|
91
|
+
});
|
|
92
|
+
const isValid = secp256k1.verify(signature, hash, pubKey, { strict: true });
|
|
93
|
+
if (!isValid) {
|
|
94
|
+
throw new Error("Invalid signature");
|
|
95
|
+
}
|
|
96
|
+
return Uint8Array.from([...signature, recoveryId]);
|
|
97
|
+
}
|
|
98
|
+
async function getPublicKey4(privateKey) {
|
|
99
|
+
privateKey = normalizePrivateKey(privateKey);
|
|
100
|
+
return Promise.resolve(secp256k1.getPublicKey(privateKey, false));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/sign/index.ts
|
|
104
|
+
async function signHash3(hash, privateKey, algorithm) {
|
|
105
|
+
if (hash.length !== 32) {
|
|
106
|
+
throw new Error("Hash must be 32 bytes long");
|
|
107
|
+
}
|
|
108
|
+
const privKey = typeof privateKey === "string" ? privateKey.replace(/^0x/, "") : Buffer.from(privateKey).toString("hex");
|
|
109
|
+
switch (algorithm) {
|
|
110
|
+
case 1 /* SECP256K1 */:
|
|
111
|
+
return signHash2(hash, privKey);
|
|
112
|
+
case 2 /* ED25519 */:
|
|
113
|
+
return signHash(hash, privKey);
|
|
114
|
+
default:
|
|
115
|
+
throw new Error(`Unsupported algorithm: ${algorithm}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export { SignAlgorithm, blake2b, ed25519_exports as ed25519, keccak_256, secp256k1_exports as secp256k1, sha2_256, sha3_256, signHash3 as signHash };
|
|
120
|
+
//# sourceMappingURL=out.js.map
|
|
121
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hash/index.ts","../src/sign/constant.ts","../src/sign/ed25519.ts","../src/sign/internal.ts","../src/sign/secp256k1.ts","../src/sign/index.ts"],"names":["SignAlgorithm","getPublicKey","signHash"],"mappings":";;;;;;;AAAA,SAAS,WAAW,gBAAgB;AACpC,SAAS,UAAU,eAAe;AAClC,SAAS,YAAY,iBAAiB;AAE/B,SAAS,WAAW,SAA0C;AACjE,SAAO,WAAW,OAAO;AAC7B;AAEO,SAAS,QAAQ,SAA0C;AAC9D,SAAO,SAAS,OAAO;AAC3B;AAEO,SAAS,SAAS,SAA0C;AAC/D,SAAO,UAAU,OAAO;AAC5B;AAEO,SAAS,SAAS,SAA0C;AAC/D,SAAO,QAAQ,OAAO;AAC1B;;;AClBO,IAAK,gBAAL,kBAAKA,mBAAL;AACH,EAAAA,8BAAA;AACA,EAAAA,8BAAA;AACA,EAAAA,8BAAA;AAHQ,SAAAA;AAAA,GAAA;;;ACAZ;AAAA;AAAA,sBAAAC;AAAA,EAAA;AAAA;AAAA,YAAY,aAAa;;;ACMlB,SAAS,oBAAoB,YAA+D;AAC/F,MAAI,OAAO,eAAe,UAAU;AAChC,WAA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D;AAEA,eAAsBD,cAAa,YAA+D;AAC9F,eAAa,oBAAoB,UAAU;AAC3C,SAAO,QAAQ,QAAkB,uBAAa,YAAY,KAAK,CAAC;AACpE;;;ACnCA,eAAsBC,UAClB,MACA,YACA,WACmB;AACnB,MAAI,KAAK,WAAW,IAAI;AACpB,UAAM,IAAI,MAAM,4BAA4B;AAAA,EAChD;AAIA,QAAM,UACF,OAAO,eAAe,WAAW,WAAW,QAAQ,OAAO,EAAE,IAAI,OAAO,KAAK,UAAU,EAAE,SAAS,KAAK;AAC3G,UAAQ,WAAW;AAAA,IACf;AACI,aAAiBA,UAAS,MAAM,OAAO;AACvC;AAAA,IACJ;AACI,aAAe,SAAS,MAAM,OAAO;AACrC;AAAA,IACJ;AACI,YAAM,IAAI,MAAM,0BAA0B,SAAS,EAAE;AAAA,EAC7D;AACJ","sourcesContent":["import { blake2b as _blake2b } from '@noble/hashes/blake2b'\nimport { sha256 as _sha256 } from '@noble/hashes/sha256'\nimport { sha3_256 as _sha3_256 } from '@noble/hashes/sha3'\n\nexport function keccak_256(message: Uint8Array | string): Uint8Array {\n return keccak_256(message)\n}\n\nexport function blake2b(message: Uint8Array | string): Uint8Array {\n return _blake2b(message)\n}\n\nexport function sha3_256(message: Uint8Array | string): Uint8Array {\n return _sha3_256(message)\n}\n\nexport function sha2_256(message: Uint8Array | string): Uint8Array {\n return _sha256(message)\n}\n","export enum SignAlgorithm {\n NATIVE,\n SECP256K1,\n ED25519,\n}\n","import * as ed25519 from '@noble/ed25519'\n\nimport { normalizePrivateKey } from './internal'\n\n/**\n * Sign a hash using ed25519\n * @param hash - 32 bytes long\n * @param privateKey - private key in hex format\n * @returns\n */\nexport async function signHash(hash: Uint8Array, privateKey: string | Uint8Array | bigint): Promise<Uint8Array> {\n if (hash.length !== 32) {\n throw new Error('Hash must be 32 bytes long')\n }\n\n privateKey = normalizePrivateKey(privateKey)\n const pubKey = await ed25519.getPublicKey(privateKey)\n const signature = await ed25519.sign(hash, privateKey)\n const isValid = await ed25519.verify(signature, hash, pubKey)\n if (!isValid) {\n throw new Error('Invalid signature')\n }\n\n return signature\n}\n\nexport async function getPublicKey(privateKey: string | Uint8Array | bigint): Promise<Uint8Array> {\n privateKey = normalizePrivateKey(privateKey)\n return ed25519.getPublicKey(privateKey)\n}\n","/**\n * Normalize the private key to a string or Uint8Array\n * - getPublicKey accepts bigint but sign does not, in ed25519\n * - a string privateKey should not have a 0x prefix, in ed25519\n * @param privateKey\n */\nexport function normalizePrivateKey(privateKey: string | Uint8Array | bigint): string | Uint8Array {\n if (typeof privateKey === 'string') {\n return privateKey.replace(/^0x/, '')\n } else if (typeof privateKey === 'bigint') {\n return privateKey.toString(16).padStart(64, '0') // the private key is 32 bytes for ed25519 and secp256k1\n } else {\n return privateKey\n }\n}\n","import * as secp256k1 from '@noble/secp256k1'\n\nimport { normalizePrivateKey } from './internal'\n\n/**\n * Sign a hash using secp256k1\n * @param hash - 32 bytes long\n * @param privateKey - private key in hex format\n * @returns\n */\nexport async function signHash(hash: Uint8Array, privateKey: string | Uint8Array | bigint): Promise<Uint8Array> {\n if (hash.length !== 32) {\n throw new Error('Hash must be 32 bytes long')\n }\n\n privateKey = normalizePrivateKey(privateKey)\n const pubKey = secp256k1.getPublicKey(privateKey, false)\n\n // refer to: https://github.com/paulmillr/noble-secp256k1/tree/1.7.1#signmsghash-privatekey\n const [signature, recoveryId] = await secp256k1.sign(hash, privateKey, {\n // recovered - true indicates the recovered bit should be included in the result\n recovered: true,\n // canonical - true makes signatures compatible with libsecp256k1, false makes signatures compatible with openssl\n canonical: true,\n // der - whether the returned signature should be in DER format. If false, it would be in Compact format (32-byte r + 32-byte s)\n der: false,\n // extraEntropy - When true, it would automatically be filled with 32 bytes of cryptographically secure entropy. Strongly recommended to pass true to improve security:\n extraEntropy: undefined,\n })\n\n const isValid = secp256k1.verify(signature, hash, pubKey, { strict: true })\n if (!isValid) {\n throw new Error('Invalid signature')\n }\n\n return Uint8Array.from([...signature, recoveryId])\n}\n\nexport async function getPublicKey(privateKey: string | Uint8Array | bigint): Promise<Uint8Array> {\n privateKey = normalizePrivateKey(privateKey)\n return Promise.resolve(secp256k1.getPublicKey(privateKey, false))\n}\n","import { SignAlgorithm } from './constant'\nexport * from './keypair'\nexport * from './constant'\nimport * as ed25519 from './ed25519'\nimport * as secp256k1 from './secp256k1'\n\nexport async function signHash(\n hash: Uint8Array,\n privateKey: string | Uint8Array,\n algorithm: SignAlgorithm\n): Promise<Uint8Array> {\n if (hash.length !== 32) {\n throw new Error('Hash must be 32 bytes long')\n }\n\n // privKey should be a hex string without 0x prefix\n // refer to: https://github.com/paulmillr/noble-secp256k1/blob/5ed251242bf065aeb22a54bb42eee1f67e7c77ce/index.ts#L130\n const privKey =\n typeof privateKey === 'string' ? privateKey.replace(/^0x/, '') : Buffer.from(privateKey).toString('hex')\n switch (algorithm) {\n case SignAlgorithm.SECP256K1:\n return secp256k1.signHash(hash, privKey)\n break\n case SignAlgorithm.ED25519:\n return ed25519.signHash(hash, privKey)\n break\n default:\n throw new Error(`Unsupported algorithm: ${algorithm}`)\n }\n}\n\nexport { secp256k1, ed25519 }\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@layerzerolabs/lz-foundation",
|
|
3
|
+
"version": "2.3.30-oft-rc.0",
|
|
4
|
+
"description": "LayerZero Core Library",
|
|
5
|
+
"license": "BUSL-1.1",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.mjs",
|
|
10
|
+
"require": "./dist/index.cjs"
|
|
11
|
+
},
|
|
12
|
+
"./package.json": "./package.json"
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.cjs",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/**/*"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "$npm_execpath clean-prebuild && $npm_execpath build-ts",
|
|
21
|
+
"build-ts": "$npm_execpath tsc --noEmit && $npm_execpath tsup",
|
|
22
|
+
"clean": "$npm_execpath clean-prebuild && rimraf .turbo",
|
|
23
|
+
"clean-prebuild": "rimraf dist"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@layerzerolabs/lz-definitions": "^2.3.45-oft-rc.0",
|
|
27
|
+
"@layerzerolabs/lz-utilities": "^2.3.45-oft-rc.0",
|
|
28
|
+
"@noble/ed25519": "^1.7.1",
|
|
29
|
+
"@noble/hashes": "^1.3.2",
|
|
30
|
+
"@noble/secp256k1": "^1.7.1"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@jest/globals": "^29.7.0",
|
|
34
|
+
"@layerzerolabs/tsup-config-next": "^2.3.45-oft-rc.0",
|
|
35
|
+
"@layerzerolabs/typescript-config-next": "^2.3.45-oft-rc.0",
|
|
36
|
+
"@types/jest": "^29.5.10",
|
|
37
|
+
"jest": "^29.7.0",
|
|
38
|
+
"rimraf": "^5.0.5",
|
|
39
|
+
"ts-jest": "^29.1.1",
|
|
40
|
+
"tsup": "^8.0.1",
|
|
41
|
+
"typescript": "~5.2.2"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
}
|
|
46
|
+
}
|