@deserialize/multi-vm-wallet 1.0.3 → 1.0.4

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/utils/test.ts ADDED
@@ -0,0 +1,49 @@
1
+ import { generateKey } from "crypto";
2
+
3
+ import { Keypair, PublicKey } from "@solana/web3.js";
4
+ import base58 from "bs58";
5
+ import { NATIVE_MINT } from "@solana/spl-token";
6
+ import { GenerateNewMnemonic } from "./bip32";
7
+ import { SVMChainWallet, SVMVM } from "./svm";
8
+ import { VM } from "./vm";
9
+ import { ChainWalletConfig } from "./types";
10
+ const mnemonic = GenerateNewMnemonic()
11
+
12
+
13
+ console.log('mnemonic: ', mnemonic);
14
+
15
+ const seed = VM.mnemonicToSeed(mnemonic)
16
+ const pKey = "4QxETeX9pndiF1XNghUiDTnZnHq3cfjmuPLBJysrgocsLq1yb8w96aPWALa8ZnRZWmDU4wM8Tg8d1ZRVVByj7uXE"
17
+
18
+ export const testUserKeyPair = Keypair.fromSecretKey(base58.decode(pKey));
19
+ const vm = new SVMVM(seed)
20
+ // const vmFromMnemonic = SVMVM.fromMnemonic(mnemonic)
21
+ // const keyFromMnemonic = vmFromMnemonic.generatePrivateKey(0)
22
+ // console.log('keyFromMnemonic: ', keyFromMnemonic.privateKey.publicKey);
23
+ const key = vm.generatePrivateKey(0)
24
+ console.log('key: ', key.privateKey.publicKey);
25
+ const chainConfig: ChainWalletConfig = {
26
+ chainId: "solana-mainnet",
27
+ name: "Solana",
28
+ rpcUrl: "https://solana-mainnet.g.alchemy.com/v2/vB5mKztdJeFdz9RkW99Qf",
29
+ explorerUrl: "https://explorer.solana.com",
30
+ nativeToken: { name: "Solana", symbol: "SOL", decimals: 9 },
31
+ confirmationNo: 1,
32
+
33
+ }
34
+
35
+
36
+ const wallet = new SVMChainWallet(chainConfig, testUserKeyPair, key.index)
37
+ const toBuy = new PublicKey("9BB6NFEcjBCtnNLFko2FqVQBq8HHM13kCyYcdQbgpump")
38
+ wallet.swap({
39
+ name: NATIVE_MINT.toBase58(),
40
+ address: NATIVE_MINT.toBase58(),
41
+ symbol: NATIVE_MINT.toBase58(),
42
+ decimals: 9
43
+ }, toBuy, 0.005,).then(res => console.log(res))
44
+
45
+
46
+ // console.log('wal: ', wal.address);
47
+ // wal.getNativeBalance().then(e => console.log(e))
48
+
49
+
package/utils/vm.ts CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  import { Balance, ChainWalletConfig, vmTypes } from "./types";
3
3
  import * as bip39 from "bip39";
4
-
4
+ import CryptoJS from "crypto-js";
5
5
  // Abstract Base Classes
6
6
  export abstract class VM<AddressType, PrivateKeyType, ConnectionType> {
7
7
  protected seed: string;
@@ -15,12 +15,61 @@ export abstract class VM<AddressType, PrivateKeyType, ConnectionType> {
15
15
  return bip39.mnemonicToSeedSync(mnemonic).toString("hex");
16
16
  }
17
17
 
18
+ static generateSalt(): string {
19
+ return CryptoJS.lib.WordArray.random(16).toString(); // 128-bit salt
20
+ }
21
+ static deriveKey(
22
+ password: string,
23
+ salt: string,
24
+ iterations = 10000,
25
+ keySize = 256 / 32
26
+ ) {
27
+ return CryptoJS.PBKDF2(password, CryptoJS.enc.Hex.parse(salt), {
28
+ keySize: keySize,
29
+ iterations: iterations,
30
+ }).toString();
31
+ }
32
+ static encryptSeedPhrase(seedPhrase: string, password: string) {
33
+ const salt = this.generateSalt(); // Generate a unique salt for this encryption
34
+ const key = this.deriveKey(password, salt); // Derive a key using PBKDF2
35
+
36
+ // Encrypt the seed phrase with AES using the derived key
37
+ const encrypted = CryptoJS.AES.encrypt(seedPhrase, key).toString();
38
+
39
+ // Return the encrypted data and the salt (needed for decryption)
40
+ return { encrypted, salt };
41
+ }
42
+
43
+ static decryptSeedPhrase(
44
+ encryptedSeedPhrase: string,
45
+ password: string,
46
+ salt: string
47
+ ) {
48
+ try {
49
+ const key = this.deriveKey(password, salt); // Derive the key using the same salt
50
+ const bytes = CryptoJS.AES.decrypt(encryptedSeedPhrase, key);
51
+ const seedPhrase = bytes.toString(CryptoJS.enc.Utf8);
52
+
53
+ // Check if decryption was successful
54
+ if (!seedPhrase) throw new Error("Decryption failed.");
55
+ return seedPhrase;
56
+ } catch (e: any) {
57
+ console.error("Invalid password or corrupted data:", e.message);
58
+ return null;
59
+ }
60
+ }
61
+ generateSalt = VM.generateSalt
62
+ deriveKey = VM.deriveKey
63
+ encryptSeedPhrase = VM.encryptSeedPhrase
64
+ decryptSeedPhrase = VM.decryptSeedPhrase
65
+
18
66
  abstract derivationPath: string
19
67
 
20
68
 
21
69
 
22
70
  abstract generatePrivateKey(index: number, mnemonic?: string, derivationPath?: string): { privateKey: PrivateKeyType, index: number };
23
71
 
72
+
24
73
  // abstract validateAddress(address: AddressType): boolean;
25
74
  // abstract getNativeBalance(address: AddressType, connection: ConnectionType): Promise<Balance>;
26
75
  }