@deserialize/multi-vm-wallet 1.0.372 → 1.1.1
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/bip32.d.ts +1 -3
- package/dist/bip32.js +103 -33
- package/dist/bip32.js.map +1 -1
- package/dist/bip32Old.d.ts +51 -0
- package/dist/bip32Old.js +856 -0
- package/dist/bip32Old.js.map +1 -0
- package/dist/{utils/bip32.d.ts → bip32Small.d.ts} +1 -3
- package/dist/bip32Small.js +115 -0
- package/dist/bip32Small.js.map +1 -0
- package/dist/bipTest.d.ts +0 -0
- package/dist/bipTest.js +363 -0
- package/dist/bipTest.js.map +1 -0
- package/dist/constant.d.ts +2 -0
- package/dist/constant.js +55 -0
- package/dist/constant.js.map +1 -0
- package/dist/evm/evm.d.ts +1 -0
- package/dist/evm/evm.js +1 -0
- package/dist/evm/evm.js.map +1 -1
- package/dist/evm/utils.d.ts +2 -1
- package/dist/evm/utils.js +24 -1
- package/dist/evm/utils.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/svm/svm.d.ts +1 -0
- package/dist/svm/svm.js +12 -8
- package/dist/svm/svm.js.map +1 -1
- package/dist/svm/transactionSender.js +6 -2
- package/dist/svm/transactionSender.js.map +1 -1
- package/dist/svm/utils.d.ts +1 -0
- package/dist/svm/utils.js +25 -33
- package/dist/svm/utils.js.map +1 -1
- package/dist/test.d.ts +2 -0
- package/dist/test.js +42 -0
- package/dist/test.js.map +1 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js.map +1 -1
- package/dist/vm.d.ts +2 -1
- package/dist/vm.js +2 -2
- package/dist/vm.js.map +1 -1
- package/package.json +2 -5
- package/utils/bip32.ts +124 -35
- package/utils/bip32Old.ts +1004 -0
- package/utils/bip32Small.ts +91 -0
- package/utils/bipTest.ts +442 -0
- package/utils/constant.ts +55 -0
- package/utils/evm/evm.ts +4 -3
- package/utils/evm/utils.ts +29 -1
- package/utils/index.ts +3 -2
- package/utils/svm/svm.ts +14 -12
- package/utils/svm/transactionSender.ts +7 -2
- package/utils/svm/utils.ts +33 -33
- package/utils/test.ts +49 -0
- package/utils/types.ts +2 -0
- package/utils/vm.ts +4 -3
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/utils/IChainWallet.d.ts +0 -17
- package/dist/utils/IChainWallet.js +0 -22
- package/dist/utils/bip32.js +0 -99
- package/dist/utils/evm/evm.d.ts +0 -39
- package/dist/utils/evm/evm.js +0 -233
- package/dist/utils/evm/index.d.ts +0 -2
- package/dist/utils/evm/index.js +0 -18
- package/dist/utils/evm/utils.d.ts +0 -207
- package/dist/utils/evm/utils.js +0 -537
- package/dist/utils/index.d.ts +0 -7
- package/dist/utils/index.js +0 -23
- package/dist/utils/svm/index.d.ts +0 -1
- package/dist/utils/svm/index.js +0 -17
- package/dist/utils/svm/svm.d.ts +0 -36
- package/dist/utils/svm/svm.js +0 -166
- package/dist/utils/svm/transactionSender.d.ts +0 -8
- package/dist/utils/svm/transactionSender.js +0 -83
- package/dist/utils/svm/utils.d.ts +0 -85
- package/dist/utils/svm/utils.js +0 -304
- package/dist/utils/types.d.ts +0 -44
- package/dist/utils/types.js +0 -9
- package/dist/utils/vm.d.ts +0 -12
- package/dist/utils/vm.js +0 -48
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { Keypair } from "@solana/web3.js";
|
|
4
|
+
import * as bip39 from "@scure/bip39";
|
|
5
|
+
import { HDKey } from "@scure/bip32"
|
|
6
|
+
import { Buffer } from "buffer"; // Import the polyfill
|
|
7
|
+
window.Buffer = Buffer; // Inject Buffer into the global scope
|
|
8
|
+
import { wordlist } from "@scure/bip39/wordlists/english";
|
|
9
|
+
import { hmac } from "../node_modules/@noble/hashes/hmac";
|
|
10
|
+
import { sha512 } from "../node_modules/@noble/hashes/sha512";
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
export function GenerateNewMnemonic() {
|
|
14
|
+
const mnemonic = bip39.generateMnemonic(wordlist);
|
|
15
|
+
return mnemonic;
|
|
16
|
+
}
|
|
17
|
+
export function ValidateMnemonic(mnemonic: string) {
|
|
18
|
+
const isValid = bip39.validateMnemonic(mnemonic, wordlist);
|
|
19
|
+
if (!isValid) {
|
|
20
|
+
throw new Error("Invalid mnemonic");
|
|
21
|
+
}
|
|
22
|
+
return isValid;
|
|
23
|
+
}
|
|
24
|
+
export function GenerateSeed(_mnemonic?: string) {
|
|
25
|
+
const mnemonic = _mnemonic || bip39.generateMnemonic(wordlist);
|
|
26
|
+
const seedString = bip39.mnemonicToSeedSync(mnemonic);
|
|
27
|
+
return seedString;
|
|
28
|
+
}
|
|
29
|
+
//EVM
|
|
30
|
+
export function EVMDeriveChildPrivateKey(seed: string, index: number, derivationPath: string) {
|
|
31
|
+
const path = `${derivationPath}${index}'`
|
|
32
|
+
const scureNode = HDKey.fromMasterSeed(Buffer.from(seed, "hex"))
|
|
33
|
+
const child = scureNode.derive(path);
|
|
34
|
+
const privateKey = Buffer.from(child.privateKey!).toString("hex");
|
|
35
|
+
const publicKey = Buffer.from(child.publicKey!).toString("hex");
|
|
36
|
+
return { privateKey, publicKey };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
//SVM
|
|
40
|
+
export function SVMDeriveChildPrivateKey(seed: string, index: number, derivationPath: string) {
|
|
41
|
+
const path = `${derivationPath}${index}'`;
|
|
42
|
+
// Derive a seed from the given path
|
|
43
|
+
const derivedSeed = derivePathEclipticCurve(path, Buffer.from(seed, "hex")).key;
|
|
44
|
+
const derivedKeyPair = Keypair.fromSeed(derivedSeed);
|
|
45
|
+
return derivedKeyPair;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function derivePathEclipticCurve(path: string, seed: Uint8Array): { key: Uint8Array, chainCode: Uint8Array } {
|
|
49
|
+
const segments = path
|
|
50
|
+
.split("/")
|
|
51
|
+
.slice(1)
|
|
52
|
+
.map((seg) => {
|
|
53
|
+
if (!seg.endsWith("'")) {
|
|
54
|
+
throw new Error("Only hardened derivation is supported");
|
|
55
|
+
}
|
|
56
|
+
return parseInt(seg.slice(0, -1), 10) + 0x80000000;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Initialize with master key derivation
|
|
60
|
+
let hmacResult = hmac(sha512, "ed25519 seed", seed);
|
|
61
|
+
let key = hmacResult.slice(0, 32);
|
|
62
|
+
let chainCode = hmacResult.slice(32, 64);
|
|
63
|
+
|
|
64
|
+
// Derive each path segment
|
|
65
|
+
for (const segment of segments) {
|
|
66
|
+
const result = hardenedDerivation(key, chainCode, segment);
|
|
67
|
+
key = Buffer.from(result.key);
|
|
68
|
+
chainCode = Buffer.from(result.chainCode);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return { key, chainCode };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function hardenedDerivation(
|
|
75
|
+
parentKey: Uint8Array,
|
|
76
|
+
parentChainCode: Uint8Array,
|
|
77
|
+
index: number
|
|
78
|
+
): { key: Uint8Array, chainCode: Uint8Array } {
|
|
79
|
+
const indexBuffer = new Uint8Array(4);
|
|
80
|
+
new DataView(indexBuffer.buffer).setUint32(0, index, false);
|
|
81
|
+
|
|
82
|
+
// Proper SLIP-0010 format: 0x00 + parent_key + index
|
|
83
|
+
const data = new Uint8Array([0x00, ...parentKey, ...indexBuffer]);
|
|
84
|
+
|
|
85
|
+
const hmacResult = hmac(sha512, parentChainCode, data);
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
key: hmacResult.slice(0, 32), // Left 32 bytes
|
|
89
|
+
chainCode: hmacResult.slice(32, 64) // Right 32 bytes
|
|
90
|
+
};
|
|
91
|
+
}
|
package/utils/bipTest.ts
ADDED
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
// import { mnemonicToSeedSync, generateMnemonic } from "@scure/bip39";
|
|
2
|
+
// import { wordlist } from "@scure/bip39/wordlists/english";
|
|
3
|
+
// import nacl from "tweetnacl";
|
|
4
|
+
// import { hmac } from "../node_modules/@noble/hashes/hmac";
|
|
5
|
+
// import { sha512 } from "../node_modules/@noble/hashes/sha512";
|
|
6
|
+
// import BIP32Factory from "bip32";
|
|
7
|
+
// import * as ecc from "tiny-secp256k1";
|
|
8
|
+
// import { BIP32Interface } from "bip32";
|
|
9
|
+
// import { derivePath as derivePathLib } from "ed25519-hd-key";
|
|
10
|
+
// import { HDKey } from "@scure/bip32";
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
// import * as bip39_impl1 from "bip39";
|
|
14
|
+
|
|
15
|
+
// import * as ed25519 from "ed25519-hd-key";
|
|
16
|
+
// import { Keypair } from "@solana/web3.js";
|
|
17
|
+
|
|
18
|
+
// // Implementation 2 - Using @scure libraries with custom Ed25519
|
|
19
|
+
// import * as bip39_impl2 from "@scure/bip39";
|
|
20
|
+
// // === your SLIP-0010 implementation here ===
|
|
21
|
+
// // (using your derivePath + hardenedDerivation methods)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
// function derivePath(path: string, seed: Uint8Array): { key: Uint8Array, chainCode: Uint8Array } {
|
|
25
|
+
// const segments = path
|
|
26
|
+
// .split("/")
|
|
27
|
+
// .slice(1)
|
|
28
|
+
// .map((seg) => {
|
|
29
|
+
// if (!seg.endsWith("'")) {
|
|
30
|
+
// throw new Error("Only hardened derivation is supported");
|
|
31
|
+
// }
|
|
32
|
+
// return parseInt(seg.slice(0, -1), 10) + 0x80000000;
|
|
33
|
+
// });
|
|
34
|
+
|
|
35
|
+
// // Initialize with master key derivation
|
|
36
|
+
// let hmacResult = hmac(sha512, "ed25519 seed", seed);
|
|
37
|
+
// let key = hmacResult.slice(0, 32);
|
|
38
|
+
// let chainCode = hmacResult.slice(32, 64);
|
|
39
|
+
|
|
40
|
+
// // Derive each path segment
|
|
41
|
+
// for (const segment of segments) {
|
|
42
|
+
// const result = hardenedDerivation(key, chainCode, segment);
|
|
43
|
+
// key = Buffer.from(result.key);
|
|
44
|
+
// chainCode = Buffer.from(result.chainCode);
|
|
45
|
+
// }
|
|
46
|
+
|
|
47
|
+
// return { key, chainCode };
|
|
48
|
+
// }
|
|
49
|
+
|
|
50
|
+
// function hardenedDerivation(
|
|
51
|
+
// parentKey: Uint8Array,
|
|
52
|
+
// parentChainCode: Uint8Array,
|
|
53
|
+
// index: number
|
|
54
|
+
// ): { key: Uint8Array, chainCode: Uint8Array } {
|
|
55
|
+
// const indexBuffer = new Uint8Array(4);
|
|
56
|
+
// new DataView(indexBuffer.buffer).setUint32(0, index, false);
|
|
57
|
+
|
|
58
|
+
// // Proper SLIP-0010 format: 0x00 + parent_key + index
|
|
59
|
+
// const data = new Uint8Array([0x00, ...parentKey, ...indexBuffer]);
|
|
60
|
+
|
|
61
|
+
// const hmacResult = hmac(sha512, parentChainCode, data);
|
|
62
|
+
|
|
63
|
+
// return {
|
|
64
|
+
// key: hmacResult.slice(0, 32), // Left 32 bytes
|
|
65
|
+
// chainCode: hmacResult.slice(32, 64) // Right 32 bytes
|
|
66
|
+
// };
|
|
67
|
+
// }
|
|
68
|
+
|
|
69
|
+
// function hex(buf: Uint8Array) {
|
|
70
|
+
// return Buffer.from(buf).toString("hex");
|
|
71
|
+
// }
|
|
72
|
+
|
|
73
|
+
// // --- Test Vector Check ---
|
|
74
|
+
// const vectorTest = () => {
|
|
75
|
+
// const seedHex = "000102030405060708090a0b0c0d0e0f";
|
|
76
|
+
// const seed = Buffer.from(seedHex, "hex");
|
|
77
|
+
// const { key, chainCode } = derivePath("m/0'", seed);
|
|
78
|
+
|
|
79
|
+
// console.log("SLIP-0010 Test Vector:");
|
|
80
|
+
// console.log("Derived key:", hex(key));
|
|
81
|
+
// console.log("Expected key: 68e0fe46dfb67e368c75379acec591dad19df3cde26e63b93a8e704f1dade7a3");
|
|
82
|
+
// console.log("Derived chainCode:", hex(chainCode));
|
|
83
|
+
// console.log("Expected chainCode: 8b59aa11380b624e81507a27fedda59fea6d0b779a778918a2fd3590e16e9c69");
|
|
84
|
+
// }
|
|
85
|
+
|
|
86
|
+
// // --- Solana Path Check ---
|
|
87
|
+
// const solanaPathCheck = () => {
|
|
88
|
+
// const mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
|
|
89
|
+
// const seed = mnemonicToSeedSync(mnemonic);
|
|
90
|
+
|
|
91
|
+
// const { key } = derivePath("m/44'/501'/0'/0'", seed);
|
|
92
|
+
// const keypair = nacl.sign.keyPair.fromSeed(key);
|
|
93
|
+
|
|
94
|
+
// console.log("\nSolana Path m/44'/501'/0'/0':");
|
|
95
|
+
// console.log("Private key seed:", hex(key));
|
|
96
|
+
// console.log("Public key:", hex(keypair.publicKey));
|
|
97
|
+
// }
|
|
98
|
+
|
|
99
|
+
// // === Test Harness ===
|
|
100
|
+
// const compareDerivation = () => {
|
|
101
|
+
// const mnemonic = generateMnemonic(wordlist);
|
|
102
|
+
// const seed = mnemonicToSeedSync(mnemonic);
|
|
103
|
+
|
|
104
|
+
// const path = "m/44'/501'/0'/0'";
|
|
105
|
+
|
|
106
|
+
// // Custom
|
|
107
|
+
// const custom = derivePath(path, seed);
|
|
108
|
+
// const customKeypair = nacl.sign.keyPair.fromSeed(custom.key);
|
|
109
|
+
|
|
110
|
+
// // Library
|
|
111
|
+
// const lib = derivePathLib(path, Buffer.from(seed).toString("hex"));
|
|
112
|
+
// const libKeypair = nacl.sign.keyPair.fromSeed(lib.key);
|
|
113
|
+
|
|
114
|
+
// console.log("=== Solana Derivation Test ===");
|
|
115
|
+
// console.log("Path:", path);
|
|
116
|
+
// console.log("\n-- Custom Implementation --");
|
|
117
|
+
// console.log("Private key seed:", hex(custom.key));
|
|
118
|
+
// console.log("Chain code:", hex(custom.chainCode));
|
|
119
|
+
// console.log("Public key:", hex(customKeypair.publicKey));
|
|
120
|
+
|
|
121
|
+
// console.log("\n-- ed25519-hd-key Library --");
|
|
122
|
+
// console.log("Private key seed:", hex(lib.key));
|
|
123
|
+
// console.log("Chain code:", hex(lib.chainCode));
|
|
124
|
+
// console.log("Public key:", hex(libKeypair.publicKey));
|
|
125
|
+
|
|
126
|
+
// console.log("\nMatch (private):", hex(custom.key) === hex(lib.key));
|
|
127
|
+
// console.log("Match (chainCode):", hex(custom.chainCode) === hex(lib.chainCode));
|
|
128
|
+
// console.log("Match (public):", hex(customKeypair.publicKey) === hex(libKeypair.publicKey));
|
|
129
|
+
// }
|
|
130
|
+
|
|
131
|
+
// const testBip32 = async () => {
|
|
132
|
+
// const mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
|
|
133
|
+
// const seed = mnemonicToSeedSync(mnemonic);
|
|
134
|
+
|
|
135
|
+
// const derivationPath = "m/44'/0'/0'/0"; // Bitcoin account
|
|
136
|
+
// const index = 0;
|
|
137
|
+
// const path = `${derivationPath}/${index}`;
|
|
138
|
+
|
|
139
|
+
// // --- @scure/bip32 ---
|
|
140
|
+
// const scureNode = HDKey.fromMasterSeed(seed);
|
|
141
|
+
// const scureChild = scureNode.derive(path);
|
|
142
|
+
|
|
143
|
+
// console.log("=== @scure/bip32 ===");
|
|
144
|
+
// console.log("Path:", path);
|
|
145
|
+
// console.log("Private key:", hex(scureChild.privateKey!));
|
|
146
|
+
// console.log("Public key:", hex(scureChild.publicKey!));
|
|
147
|
+
// console.log("Chain code:", hex(scureChild.chainCode!));
|
|
148
|
+
|
|
149
|
+
// // --- bip32 + tiny-secp256k1 ---
|
|
150
|
+
// const bip32 = BIP32Factory(ecc);
|
|
151
|
+
// const node: BIP32Interface = bip32.fromSeed(Buffer.from(seed));
|
|
152
|
+
// const child = node.derivePath(path);
|
|
153
|
+
|
|
154
|
+
// console.log("\n=== bip32 (tiny-secp256k1) ===");
|
|
155
|
+
// console.log("Path:", path);
|
|
156
|
+
// console.log("Private key:", hex(child.privateKey!));
|
|
157
|
+
// console.log("Public key:", hex(child.publicKey));
|
|
158
|
+
// console.log("Chain code:", hex(child.chainCode));
|
|
159
|
+
|
|
160
|
+
// // Compare
|
|
161
|
+
// console.log("\n=== Comparison ===");
|
|
162
|
+
// console.log("Private keys match:", hex(scureChild.privateKey!) === hex(child.privateKey!));
|
|
163
|
+
// console.log("Public keys match:", hex(scureChild.publicKey!) === hex(child.publicKey));
|
|
164
|
+
// console.log("Chain codes match:", hex(scureChild.chainCode!) === hex(child.chainCode));
|
|
165
|
+
// }
|
|
166
|
+
|
|
167
|
+
// // ===== IMPLEMENTATION 1 =====
|
|
168
|
+
// class WalletImpl1 {
|
|
169
|
+
// static GenerateNewMnemonic(): string {
|
|
170
|
+
// const mnemonic = bip39_impl1.generateMnemonic();
|
|
171
|
+
// return mnemonic;
|
|
172
|
+
// }
|
|
173
|
+
|
|
174
|
+
// static ValidateMnemonic(mnemonic: string): boolean {
|
|
175
|
+
// const isValid = bip39_impl1.validateMnemonic(mnemonic);
|
|
176
|
+
// if (!isValid) {
|
|
177
|
+
// throw new Error("Invalid mnemonic");
|
|
178
|
+
// }
|
|
179
|
+
// return isValid;
|
|
180
|
+
// }
|
|
181
|
+
|
|
182
|
+
// static GenerateSeed(_mnemonic: string): string {
|
|
183
|
+
// const mnemonic = _mnemonic || bip39_impl1.generateMnemonic();
|
|
184
|
+
// const seed = bip39_impl1.mnemonicToSeedSync(mnemonic);
|
|
185
|
+
// const seedString = seed.toString("hex");
|
|
186
|
+
// return seedString;
|
|
187
|
+
// }
|
|
188
|
+
|
|
189
|
+
// static getSeedNode(seed: string): BIP32Interface {
|
|
190
|
+
// const bip32 = BIP32Factory(ecc);
|
|
191
|
+
// const restoredSeedBuffer = Buffer.from(seed, "hex");
|
|
192
|
+
// const node = bip32.fromSeed(restoredSeedBuffer);
|
|
193
|
+
// return node;
|
|
194
|
+
// }
|
|
195
|
+
|
|
196
|
+
// static EVMDeriveChildPrivateKey(seed: string, index: number, derivationPath: string): { privateKey: string, publicKey: string } {
|
|
197
|
+
// const node = this.getSeedNode(seed);
|
|
198
|
+
// const child = node.derivePath(`${derivationPath}${index}'`);
|
|
199
|
+
// const privateKey = child.privateKey!.toString("hex");
|
|
200
|
+
// const publicKey = child.publicKey.toString("hex");
|
|
201
|
+
// return { privateKey, publicKey };
|
|
202
|
+
// }
|
|
203
|
+
|
|
204
|
+
// static SVMDeriveChildPrivateKey(seed: string, index: number, derivationPath: string): { privateKey: string, publicKey: string, keypair: Keypair } {
|
|
205
|
+
// const path = `${derivationPath}${index}'`;
|
|
206
|
+
// const derivedSeed = ed25519.derivePath(path, seed).key;
|
|
207
|
+
// const derivedKeyPair = Keypair.fromSeed(derivedSeed);
|
|
208
|
+
// return {
|
|
209
|
+
// privateKey: Buffer.from(derivedKeyPair.secretKey).toString("hex"),
|
|
210
|
+
// publicKey: derivedKeyPair.publicKey.toString(),
|
|
211
|
+
// keypair: derivedKeyPair
|
|
212
|
+
// };
|
|
213
|
+
// }
|
|
214
|
+
// }
|
|
215
|
+
|
|
216
|
+
// // ===== IMPLEMENTATION 2 =====
|
|
217
|
+
// class WalletImpl2 {
|
|
218
|
+
// static GenerateNewMnemonic(): string {
|
|
219
|
+
// const mnemonic = bip39_impl2.generateMnemonic(wordlist);
|
|
220
|
+
// return mnemonic;
|
|
221
|
+
// }
|
|
222
|
+
|
|
223
|
+
// static ValidateMnemonic(mnemonic: string): boolean {
|
|
224
|
+
// const isValid = bip39_impl2.validateMnemonic(mnemonic, wordlist);
|
|
225
|
+
// if (!isValid) {
|
|
226
|
+
// throw new Error("Invalid mnemonic");
|
|
227
|
+
// }
|
|
228
|
+
// return isValid;
|
|
229
|
+
// }
|
|
230
|
+
|
|
231
|
+
// static GenerateSeed(_mnemonic: string): Uint8Array {
|
|
232
|
+
// const mnemonic = _mnemonic || bip39_impl2.generateMnemonic(wordlist);
|
|
233
|
+
// const seedString = bip39_impl2.mnemonicToSeedSync(mnemonic);
|
|
234
|
+
// return seedString;
|
|
235
|
+
// }
|
|
236
|
+
|
|
237
|
+
// static EVMDeriveChildPrivateKey(seed: string, index: number, derivationPath: string): { privateKey: string, publicKey: string } {
|
|
238
|
+
// const path = `${derivationPath}${index}'`;
|
|
239
|
+
// const scureNode = HDKey.fromMasterSeed(Buffer.from(seed, "hex"));
|
|
240
|
+
// const child = scureNode.derive(path);
|
|
241
|
+
// const privateKey = Buffer.from(child.privateKey!).toString("hex");
|
|
242
|
+
// const publicKey = Buffer.from(child.publicKey!).toString("hex");
|
|
243
|
+
// return { privateKey, publicKey };
|
|
244
|
+
// }
|
|
245
|
+
|
|
246
|
+
// static SVMDeriveChildPrivateKey(seed: string, index: number, derivationPath: string): { privateKey: string, publicKey: string, keypair: Keypair } {
|
|
247
|
+
// const path = `${derivationPath}${index}'`;
|
|
248
|
+
// const derivedSeed = this.derivePathEclipticCurve(path, Buffer.from(seed, "hex")).key;
|
|
249
|
+
// const derivedKeyPair = Keypair.fromSeed(derivedSeed);
|
|
250
|
+
// return {
|
|
251
|
+
// privateKey: Buffer.from(derivedKeyPair.secretKey).toString("hex"),
|
|
252
|
+
// publicKey: derivedKeyPair.publicKey.toString(),
|
|
253
|
+
// keypair: derivedKeyPair
|
|
254
|
+
// };
|
|
255
|
+
// }
|
|
256
|
+
|
|
257
|
+
// static derivePathEclipticCurve(path: string, seed: Uint8Array): { key: Uint8Array, chainCode: Uint8Array } {
|
|
258
|
+
// const segments = path
|
|
259
|
+
// .split("/")
|
|
260
|
+
// .slice(1)
|
|
261
|
+
// .map((seg) => {
|
|
262
|
+
// if (!seg.endsWith("'")) {
|
|
263
|
+
// throw new Error("Only hardened derivation is supported");
|
|
264
|
+
// }
|
|
265
|
+
// return parseInt(seg.slice(0, -1), 10) + 0x80000000;
|
|
266
|
+
// });
|
|
267
|
+
|
|
268
|
+
// let hmacResult = hmac(sha512, "ed25519 seed", seed);
|
|
269
|
+
// let key = hmacResult.slice(0, 32);
|
|
270
|
+
// let chainCode = hmacResult.slice(32, 64);
|
|
271
|
+
|
|
272
|
+
// for (const segment of segments) {
|
|
273
|
+
// const result = this.hardenedDerivation(key, chainCode, segment);
|
|
274
|
+
// key = Buffer.from(result.key);
|
|
275
|
+
// chainCode = Buffer.from(result.chainCode);
|
|
276
|
+
// }
|
|
277
|
+
|
|
278
|
+
// return { key, chainCode };
|
|
279
|
+
// }
|
|
280
|
+
|
|
281
|
+
// static hardenedDerivation(
|
|
282
|
+
// parentKey: Uint8Array,
|
|
283
|
+
// parentChainCode: Uint8Array,
|
|
284
|
+
// index: number
|
|
285
|
+
// ): { key: Uint8Array, chainCode: Uint8Array } {
|
|
286
|
+
// const indexBuffer = new Uint8Array(4);
|
|
287
|
+
// new DataView(indexBuffer.buffer).setUint32(0, index, false);
|
|
288
|
+
|
|
289
|
+
// const data = new Uint8Array([0x00, ...parentKey, ...indexBuffer]);
|
|
290
|
+
// const hmacResult = hmac(sha512, parentChainCode, data);
|
|
291
|
+
|
|
292
|
+
// return {
|
|
293
|
+
// key: hmacResult.slice(0, 32),
|
|
294
|
+
// chainCode: hmacResult.slice(32, 64)
|
|
295
|
+
// };
|
|
296
|
+
// }
|
|
297
|
+
// }
|
|
298
|
+
|
|
299
|
+
// // ===== COMPARISON SCRIPT =====
|
|
300
|
+
// function compareImplementations() {
|
|
301
|
+
// console.log("🔍 Comparing Wallet Implementations\n");
|
|
302
|
+
// console.log("=".repeat(80));
|
|
303
|
+
|
|
304
|
+
// // Test data
|
|
305
|
+
// const testMnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
|
|
306
|
+
// const evmDerivationPath = "m/44'/60'/0'/0/";
|
|
307
|
+
// const svmDerivationPath = "m/44'/501'/";
|
|
308
|
+
// const testIndices = [0, 1, 2, 5, 10];
|
|
309
|
+
|
|
310
|
+
// let allTestsPassed = true;
|
|
311
|
+
|
|
312
|
+
// try {
|
|
313
|
+
// // Test 1: Mnemonic validation
|
|
314
|
+
// console.log("📝 Test 1: Mnemonic Validation");
|
|
315
|
+
// const valid1 = WalletImpl1.ValidateMnemonic(testMnemonic);
|
|
316
|
+
// const valid2 = WalletImpl2.ValidateMnemonic(testMnemonic);
|
|
317
|
+
|
|
318
|
+
// console.log(` Implementation 1: ${valid1}`);
|
|
319
|
+
// console.log(` Implementation 2: ${valid2}`);
|
|
320
|
+
// console.log(` ✅ Match: ${valid1 === valid2}\n`);
|
|
321
|
+
|
|
322
|
+
// if (valid1 !== valid2) allTestsPassed = false;
|
|
323
|
+
|
|
324
|
+
// // Test 2: Seed generation
|
|
325
|
+
// console.log("🌱 Test 2: Seed Generation");
|
|
326
|
+
// const seed1 = WalletImpl1.GenerateSeed(testMnemonic);
|
|
327
|
+
// const seed2 = Buffer.from(WalletImpl2.GenerateSeed(testMnemonic)).toString("hex");
|
|
328
|
+
|
|
329
|
+
// console.log(` Implementation 1: ${seed1.substring(0, 32)}...`);
|
|
330
|
+
// console.log(` Implementation 2: ${seed2.substring(0, 32)}...`);
|
|
331
|
+
// console.log(` ✅ Match: ${seed1 === seed2}\n`);
|
|
332
|
+
|
|
333
|
+
// if (seed1 !== seed2) allTestsPassed = false;
|
|
334
|
+
|
|
335
|
+
// // Test 3: EVM Key Derivation
|
|
336
|
+
// console.log("⚡ Test 3: EVM Key Derivation");
|
|
337
|
+
// console.log(" Index | Implementation 1 Private Key | Implementation 2 Private Key | Match");
|
|
338
|
+
// console.log(" ------|--------------------------------------------------|--------------------------------------------------|------");
|
|
339
|
+
|
|
340
|
+
// let evmMatches = true;
|
|
341
|
+
// for (const index of testIndices) {
|
|
342
|
+
// const evm1 = WalletImpl1.EVMDeriveChildPrivateKey(seed1, index, evmDerivationPath);
|
|
343
|
+
// const evm2 = WalletImpl2.EVMDeriveChildPrivateKey(seed1, index, evmDerivationPath);
|
|
344
|
+
|
|
345
|
+
// const privateKeyMatch = evm1.privateKey === evm2.privateKey;
|
|
346
|
+
// const publicKeyMatch = evm1.publicKey === evm2.publicKey;
|
|
347
|
+
// const match = privateKeyMatch && publicKeyMatch;
|
|
348
|
+
|
|
349
|
+
// console.log(` ${index.toString().padStart(5)} | ${evm1.privateKey.substring(0, 48)} | ${evm2.privateKey.substring(0, 48)} | ${match ? '✅' : '❌'}`);
|
|
350
|
+
|
|
351
|
+
// if (!match) {
|
|
352
|
+
// evmMatches = false;
|
|
353
|
+
// allTestsPassed = false;
|
|
354
|
+
// }
|
|
355
|
+
// }
|
|
356
|
+
// console.log(` Overall EVM Match: ${evmMatches ? '✅' : '❌'}\n`);
|
|
357
|
+
|
|
358
|
+
// // Test 4: SVM Key Derivation
|
|
359
|
+
// console.log("🌞 Test 4: SVM (Solana) Key Derivation");
|
|
360
|
+
// console.log(" Index | Implementation 1 Private Key | Implementation 2 Private Key | Match");
|
|
361
|
+
// console.log(" ------|--------------------------------------------------|--------------------------------------------------|------");
|
|
362
|
+
|
|
363
|
+
// let svmMatches = true;
|
|
364
|
+
// for (const index of testIndices) {
|
|
365
|
+
// const svm1 = WalletImpl1.SVMDeriveChildPrivateKey(seed1, index, svmDerivationPath);
|
|
366
|
+
// const svm2 = WalletImpl2.SVMDeriveChildPrivateKey(seed1, index, svmDerivationPath);
|
|
367
|
+
|
|
368
|
+
// const privateKeyMatch = svm1.privateKey === svm2.privateKey;
|
|
369
|
+
// const publicKeyMatch = svm1.publicKey === svm2.publicKey;
|
|
370
|
+
// const match = privateKeyMatch && publicKeyMatch;
|
|
371
|
+
|
|
372
|
+
// console.log(` ${index.toString().padStart(5)} | ${svm1.privateKey.substring(0, 48)} | ${svm2.privateKey.substring(0, 48)} | ${match ? '✅' : '❌'}`);
|
|
373
|
+
|
|
374
|
+
// if (!match) {
|
|
375
|
+
// svmMatches = false;
|
|
376
|
+
// allTestsPassed = false;
|
|
377
|
+
|
|
378
|
+
// // Show detailed comparison for mismatches
|
|
379
|
+
// console.log(` 🔍 Detailed comparison for index ${index}:`);
|
|
380
|
+
// console.log(` Impl1 Private: ${svm1.privateKey}`);
|
|
381
|
+
// console.log(` Impl2 Private: ${svm2.privateKey}`);
|
|
382
|
+
// console.log(` Impl1 Public: ${svm1.publicKey}`);
|
|
383
|
+
// console.log(` Impl2 Public: ${svm2.publicKey}`);
|
|
384
|
+
// }
|
|
385
|
+
// }
|
|
386
|
+
// console.log(` Overall SVM Match: ${svmMatches ? '✅' : '❌'}\n`);
|
|
387
|
+
|
|
388
|
+
// // Test 5: Performance comparison
|
|
389
|
+
// console.log("⏱️ Test 5: Performance Comparison");
|
|
390
|
+
|
|
391
|
+
// const iterations = 100;
|
|
392
|
+
|
|
393
|
+
// // EVM Performance
|
|
394
|
+
// console.time(" Implementation 1 EVM");
|
|
395
|
+
// for (let i = 0; i < iterations; i++) {
|
|
396
|
+
// WalletImpl1.EVMDeriveChildPrivateKey(seed1, i % 10, evmDerivationPath);
|
|
397
|
+
// }
|
|
398
|
+
// console.timeEnd(" Implementation 1 EVM");
|
|
399
|
+
|
|
400
|
+
// console.time(" Implementation 2 EVM");
|
|
401
|
+
// for (let i = 0; i < iterations; i++) {
|
|
402
|
+
// WalletImpl2.EVMDeriveChildPrivateKey(seed1, i % 10, evmDerivationPath);
|
|
403
|
+
// }
|
|
404
|
+
// console.timeEnd(" Implementation 2 EVM");
|
|
405
|
+
|
|
406
|
+
// // SVM Performance
|
|
407
|
+
// console.time(" Implementation 1 SVM");
|
|
408
|
+
// for (let i = 0; i < iterations; i++) {
|
|
409
|
+
// WalletImpl1.SVMDeriveChildPrivateKey(seed1, i % 10, svmDerivationPath);
|
|
410
|
+
// }
|
|
411
|
+
// console.timeEnd(" Implementation 1 SVM");
|
|
412
|
+
|
|
413
|
+
// console.time(" Implementation 2 SVM");
|
|
414
|
+
// for (let i = 0; i < iterations; i++) {
|
|
415
|
+
// WalletImpl2.SVMDeriveChildPrivateKey(seed1, i % 10, svmDerivationPath);
|
|
416
|
+
// }
|
|
417
|
+
// console.timeEnd(" Implementation 2 SVM");
|
|
418
|
+
|
|
419
|
+
// console.log("\n" + "=".repeat(80));
|
|
420
|
+
// console.log(`🎯 Overall Result: ${allTestsPassed ? '✅ ALL TESTS PASSED' : '❌ SOME TESTS FAILED'}`);
|
|
421
|
+
// console.log("=".repeat(80));
|
|
422
|
+
|
|
423
|
+
// return {
|
|
424
|
+
// success: allTestsPassed,
|
|
425
|
+
// details: {
|
|
426
|
+
// mnemonicValidation: valid1 === valid2,
|
|
427
|
+
// seedGeneration: seed1 === seed2,
|
|
428
|
+
// evmDerivation: evmMatches,
|
|
429
|
+
// svmDerivation: svmMatches
|
|
430
|
+
// }
|
|
431
|
+
// };
|
|
432
|
+
|
|
433
|
+
// } catch (error: any) {
|
|
434
|
+
// console.error("❌ Error during comparison:", error);
|
|
435
|
+
// return {
|
|
436
|
+
// success: false,
|
|
437
|
+
// error: error.message
|
|
438
|
+
// };
|
|
439
|
+
// }
|
|
440
|
+
// }
|
|
441
|
+
|
|
442
|
+
// compareImplementations()
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ChainWalletConfig } from "./types";
|
|
2
|
+
|
|
3
|
+
export const DefaultChains: ChainWalletConfig[] = [{
|
|
4
|
+
chainId: 501,
|
|
5
|
+
name: "Solana Mainnet",
|
|
6
|
+
rpcUrl: "https://api.mainnet-beta.solana.com",
|
|
7
|
+
explorerUrl: "https://explorer.solana.com", // e.g. Solana Explorer
|
|
8
|
+
nativeToken: {
|
|
9
|
+
name: "Solana",
|
|
10
|
+
symbol: "SOL",
|
|
11
|
+
decimals: 9,
|
|
12
|
+
},
|
|
13
|
+
testnet: false,
|
|
14
|
+
logoUrl: "https://cryptologos.cc/logos/solana-sol-logo.png?v=040"
|
|
15
|
+
}
|
|
16
|
+
, {
|
|
17
|
+
chainId: 1,
|
|
18
|
+
name: "Ethereum Mainnet",
|
|
19
|
+
rpcUrl: "https://cloudflare-eth.com",
|
|
20
|
+
explorerUrl: "https://etherscan.io",
|
|
21
|
+
nativeToken: {
|
|
22
|
+
name: "Ether",
|
|
23
|
+
symbol: "ETH",
|
|
24
|
+
decimals: 18,
|
|
25
|
+
},
|
|
26
|
+
testnet: false,
|
|
27
|
+
logoUrl: "https://cryptologos.cc/logos/ethereum-eth-logo.png?v=040"
|
|
28
|
+
}, {
|
|
29
|
+
chainId: 56,
|
|
30
|
+
name: "BNB Smart Chain Mainnet",
|
|
31
|
+
rpcUrl: "https://bsc-dataseed.binance.org/",
|
|
32
|
+
explorerUrl: "https://bscscan.com",
|
|
33
|
+
nativeToken: {
|
|
34
|
+
name: "Binance Coin",
|
|
35
|
+
symbol: "BNB",
|
|
36
|
+
decimals: 18,
|
|
37
|
+
},
|
|
38
|
+
testnet: false,
|
|
39
|
+
logoUrl: "https://cryptologos.cc/logos/bnb-bnb-logo.png?v=040"
|
|
40
|
+
}, {
|
|
41
|
+
chainId: 501,
|
|
42
|
+
name: "Eclipse Mainnet",
|
|
43
|
+
rpcUrl: "https://mainnetbeta-rpc.eclipse.xyz",
|
|
44
|
+
explorerUrl: "https://explorer.eclipse.xyz/", // e.g. Solana Explorer
|
|
45
|
+
nativeToken: {
|
|
46
|
+
name: "Eclipse",
|
|
47
|
+
symbol: "ETH",
|
|
48
|
+
decimals: 9,
|
|
49
|
+
},
|
|
50
|
+
testnet: false,
|
|
51
|
+
logoUrl: "https://raw.githubusercontent.com/hyperlane-xyz/hyperlane-registry/a86c3b432b6f9ad7272ae09859f20eb3ade3bd6e/deployments/warp_routes/ES/logo.svg"
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
]
|
package/utils/evm/evm.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* this is a class that will be responsible for creating several evm wallets code
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { EVMDeriveChildPrivateKey
|
|
7
|
+
import { EVMDeriveChildPrivateKey } from "../bip32";
|
|
8
8
|
import { ChainWallet } from "../IChainWallet";
|
|
9
9
|
import { Balance, ChainWalletConfig, NFTInfo, TokenInfo, TransactionResult } from "../types";
|
|
10
10
|
import { VM } from "../vm";
|
|
@@ -24,7 +24,8 @@ import {
|
|
|
24
24
|
isChainSupportedByKyber,
|
|
25
25
|
TransactionParams,
|
|
26
26
|
approveToken,
|
|
27
|
-
executeContractMethod
|
|
27
|
+
executeContractMethod,
|
|
28
|
+
getTokenInfo
|
|
28
29
|
} from "./utils";
|
|
29
30
|
|
|
30
31
|
export const createEvmVmPrivateKey = (phrase: string) => { }
|
|
@@ -36,7 +37,7 @@ export class EVMVM extends VM<string, string, JsonRpcProvider> {
|
|
|
36
37
|
super(seed, "EVM");
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
|
|
40
|
+
getTokenInfo = getTokenInfo
|
|
40
41
|
generatePrivateKey(index: number, seed?: string, mnemonic?: string, derivationPath = this.derivationPath) {
|
|
41
42
|
let _seed: string
|
|
42
43
|
|
package/utils/evm/utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import { Balance } from '../types'
|
|
2
|
+
import { Balance, TokenInfo } from '../types'
|
|
3
3
|
import { JsonRpcProvider, Contract, Wallet, TransactionRequest, TransactionResponse, TransactionReceipt } from 'ethers'
|
|
4
4
|
import BN from 'bn.js'
|
|
5
5
|
|
|
@@ -152,6 +152,34 @@ export const getNativeBalance = async (address: string, provider: JsonRpcProvide
|
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
+
export const getTokenInfo = async (
|
|
156
|
+
tokenAddress: string,
|
|
157
|
+
provider: JsonRpcProvider
|
|
158
|
+
): Promise<TokenInfo> => {
|
|
159
|
+
try {
|
|
160
|
+
// Create contract instance
|
|
161
|
+
const tokenContract = new Contract(tokenAddress, ERC20_ABI, provider)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
// Get decimals to format the balance properly
|
|
165
|
+
const [decimals, name, symbol] = await Promise.all([
|
|
166
|
+
await tokenContract.decimals(),
|
|
167
|
+
await tokenContract.name(),
|
|
168
|
+
await tokenContract.symbol(),
|
|
169
|
+
|
|
170
|
+
])
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
name: name,
|
|
174
|
+
symbol: symbol,
|
|
175
|
+
address: tokenAddress,
|
|
176
|
+
decimals: decimals
|
|
177
|
+
}
|
|
178
|
+
} catch (error) {
|
|
179
|
+
console.error('Error fetching token balance:', error)
|
|
180
|
+
throw error
|
|
181
|
+
}
|
|
182
|
+
}
|
|
155
183
|
export const getTokenBalance = async (
|
|
156
184
|
tokenAddress: string,
|
|
157
185
|
walletAddress: string,
|
package/utils/index.ts
CHANGED