@layerzerolabs/onesig-core 0.0.5 → 0.0.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.
- package/dist/index.d.ts +7 -3
- package/dist/index.js +49 -15
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { TypedDataSigner } from '@ethersproject/abstract-signer';
|
|
|
5
5
|
|
|
6
6
|
interface BaseLeafData<TargetAddressType = unknown, CallData = unknown> {
|
|
7
7
|
nonce: bigint;
|
|
8
|
-
|
|
8
|
+
oneSigId: bigint;
|
|
9
9
|
targetOneSigAddress: TargetAddressType;
|
|
10
10
|
calls: CallData[];
|
|
11
11
|
}
|
|
@@ -14,9 +14,12 @@ interface SigningOptions {
|
|
|
14
14
|
expiry: number | string | BigNumber;
|
|
15
15
|
}
|
|
16
16
|
interface GenerateLeafsResult<Leaf extends BaseLeafData = BaseLeafData<any, any>> {
|
|
17
|
-
|
|
17
|
+
encodeCalls: (calls: Leaf['calls']) => Buffer;
|
|
18
|
+
encodeAddress: (address: Leaf['targetOneSigAddress']) => Buffer;
|
|
18
19
|
leafs: Leaf[];
|
|
19
20
|
}
|
|
21
|
+
declare function encodeLeafHeader({ targetOneSigAddress, oneSigId, nonce }: Omit<BaseLeafData<Buffer>, 'calls'>): Buffer;
|
|
22
|
+
declare function encodeLeaf(gen: GenerateLeafsResult, index: number): string;
|
|
20
23
|
declare function makeOneSigTree(input: GenerateLeafsResult[]): MerkleTree;
|
|
21
24
|
declare function compareAddresses(a: string, b: string): number;
|
|
22
25
|
type HexStringLike = `0x${string}`;
|
|
@@ -40,8 +43,9 @@ declare class Signature {
|
|
|
40
43
|
*/
|
|
41
44
|
static concatenateSignatures(input: SignatureLike[], digest: Buffer | string): Signature;
|
|
42
45
|
}
|
|
46
|
+
declare function getSigningData(tree: MerkleTree, { seed, expiry }: SigningOptions): Parameters<TypedDataSigner['_signTypedData']>;
|
|
43
47
|
declare function getDigestToSign(tree: MerkleTree, options: SigningOptions): string;
|
|
44
48
|
declare function signOneSigTree(tree: MerkleTree, signers: TypedDataSigner[], options: SigningOptions, enc?: 'string'): Promise<string>;
|
|
45
49
|
declare function signOneSigTree(tree: MerkleTree, signers: TypedDataSigner[], options: SigningOptions, enc: 'signature'): Promise<Signature>;
|
|
46
50
|
|
|
47
|
-
export { type BaseLeafData, type GenerateLeafsResult, Signature, type SigningOptions, compareAddresses, getDigestToSign, makeOneSigTree, signOneSigTree };
|
|
51
|
+
export { type BaseLeafData, type GenerateLeafsResult, Signature, type SigningOptions, compareAddresses, encodeLeaf, encodeLeafHeader, getDigestToSign, getSigningData, makeOneSigTree, signOneSigTree };
|
package/dist/index.js
CHANGED
|
@@ -29,7 +29,10 @@ __export(src_exports, {
|
|
|
29
29
|
MerkleTree: () => import_merkletreejs.MerkleTree,
|
|
30
30
|
Signature: () => Signature,
|
|
31
31
|
compareAddresses: () => compareAddresses,
|
|
32
|
+
encodeLeaf: () => encodeLeaf,
|
|
33
|
+
encodeLeafHeader: () => encodeLeafHeader,
|
|
32
34
|
getDigestToSign: () => getDigestToSign,
|
|
35
|
+
getSigningData: () => getSigningData,
|
|
33
36
|
makeOneSigTree: () => makeOneSigTree,
|
|
34
37
|
signOneSigTree: () => signOneSigTree
|
|
35
38
|
});
|
|
@@ -63,28 +66,59 @@ _code = new WeakMap();
|
|
|
63
66
|
var OneSigCoreError = _OneSigCoreError;
|
|
64
67
|
|
|
65
68
|
// src/index.ts
|
|
69
|
+
function readByteFromHex(input, byteOffset) {
|
|
70
|
+
const charOffset = byteOffset * 2;
|
|
71
|
+
const sub = input.substring(charOffset, charOffset + 2);
|
|
72
|
+
return parseInt(sub, 16);
|
|
73
|
+
}
|
|
74
|
+
function encodeLeafHeader({ targetOneSigAddress, oneSigId, nonce }) {
|
|
75
|
+
if (targetOneSigAddress.byteLength !== 32) {
|
|
76
|
+
throw new Error("Contract address must be 32 bytes");
|
|
77
|
+
}
|
|
78
|
+
const storage = Buffer.alloc(97);
|
|
79
|
+
storage[0] = 1;
|
|
80
|
+
const idStr = oneSigId.toString(16).padStart(64, "0");
|
|
81
|
+
const nonceStr = nonce.toString(16).padStart(64, "0");
|
|
82
|
+
for (let i = 0; i < 32; i++) {
|
|
83
|
+
storage[i + 1] = readByteFromHex(idStr, i);
|
|
84
|
+
storage[i + 33] = targetOneSigAddress[i];
|
|
85
|
+
storage[i + 65] = readByteFromHex(nonceStr, i);
|
|
86
|
+
}
|
|
87
|
+
return storage;
|
|
88
|
+
}
|
|
89
|
+
function encodeLeaf(gen, index) {
|
|
90
|
+
const leaf = gen.leafs[index];
|
|
91
|
+
if (!leaf) {
|
|
92
|
+
throw new Error("Leaf does not exist");
|
|
93
|
+
}
|
|
94
|
+
const leafData = Buffer.concat([
|
|
95
|
+
encodeLeafHeader({
|
|
96
|
+
nonce: leaf.nonce,
|
|
97
|
+
oneSigId: leaf.oneSigId,
|
|
98
|
+
targetOneSigAddress: gen.encodeAddress(leaf.targetOneSigAddress)
|
|
99
|
+
}),
|
|
100
|
+
gen.encodeCalls(leaf.calls)
|
|
101
|
+
]);
|
|
102
|
+
return import_ethers.ethers.utils.keccak256(leafData);
|
|
103
|
+
}
|
|
66
104
|
function makeOneSigTree(input) {
|
|
67
|
-
const encodedLeafs =
|
|
68
|
-
const
|
|
69
|
-
for (const
|
|
70
|
-
for (
|
|
71
|
-
const
|
|
72
|
-
|
|
105
|
+
const encodedLeafs = [];
|
|
106
|
+
const seenNonceIds = /* @__PURE__ */ new Set();
|
|
107
|
+
for (const gen of input) {
|
|
108
|
+
for (let i = 0; i < gen.leafs.length; i++) {
|
|
109
|
+
const leaf = gen.leafs[i];
|
|
110
|
+
const nonceIdCombo = `${leaf.nonce}.${leaf.oneSigId}`;
|
|
111
|
+
if (seenNonceIds.has(nonceIdCombo)) {
|
|
73
112
|
throw new OneSigCoreError(
|
|
74
|
-
"
|
|
113
|
+
"NONCE_ID_SEEN_TWICE",
|
|
75
114
|
"Two calls should not be made for the same chain/nonce twice"
|
|
76
115
|
);
|
|
77
116
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (encodedLeafs.has(encoded)) {
|
|
81
|
-
throw new OneSigCoreError("LEAF_SEEN_TWICE", "Encoded leaf cannot be seen twice");
|
|
82
|
-
}
|
|
83
|
-
encodedLeafs.add(encoded);
|
|
117
|
+
seenNonceIds.add(nonceIdCombo);
|
|
118
|
+
encodedLeafs.push(encodeLeaf(gen, i));
|
|
84
119
|
}
|
|
85
120
|
}
|
|
86
|
-
const
|
|
87
|
-
const tree = new import_merkletreejs.MerkleTree(asArray, import_ethers.ethers.utils.keccak256, { sortPairs: true });
|
|
121
|
+
const tree = new import_merkletreejs.MerkleTree(encodedLeafs, import_ethers.ethers.utils.keccak256, { sortPairs: true });
|
|
88
122
|
return tree;
|
|
89
123
|
}
|
|
90
124
|
function compareAddresses(a, b) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@layerzerolabs/onesig-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@babel/core": "^7.23.9",
|
|
33
33
|
"@jest/globals": "^29.7.0",
|
|
34
|
-
"@layerzerolabs/eslint-config-next": "^3.0.
|
|
35
|
-
"@layerzerolabs/prettier-config-next": "^3.0.
|
|
34
|
+
"@layerzerolabs/eslint-config-next": "^3.0.75",
|
|
35
|
+
"@layerzerolabs/prettier-config-next": "^3.0.75",
|
|
36
36
|
"@openzeppelin/contracts": "5.0.2",
|
|
37
37
|
"@openzeppelin/contracts-upgradeable": "5.0.2",
|
|
38
38
|
"@rushstack/eslint-patch": "^1.7.0",
|