@octaflowlabs/onchain-sdk 1.3.0 → 1.3.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.
@@ -20,6 +20,10 @@ export declare class EvmWalletService {
20
20
  generateMultipleWallets(mnemonic: string, count: number, startIndex?: number): EvmDerivedWallet[];
21
21
  deriveWalletFromMnemonic(mnemonic: string, accountIndex?: number, customPath?: string): EvmDerivedWallet;
22
22
  deriveFromPrivateKey(privateKey: string): Omit<EvmDerivedWallet, 'path'>;
23
+ getFirstAvailableIndexForMnemonicImport(mnemonic: string, existingAddresses: string[], maxIndicesToCheck?: number): {
24
+ index: number;
25
+ wallet: EvmDerivedWallet;
26
+ } | null;
23
27
  signMessage(privateKey: string, message: string): Promise<string>;
24
28
  isValidMnemonic(mnemonic: string): boolean;
25
29
  private validateMnemonic;
@@ -58,6 +58,18 @@ class EvmWalletService {
58
58
  publicKey: wallet.signingKey.publicKey,
59
59
  };
60
60
  }
61
+ getFirstAvailableIndexForMnemonicImport(mnemonic, existingAddresses, maxIndicesToCheck = 20) {
62
+ this.validateMnemonic(mnemonic);
63
+ const existingSet = new Set(existingAddresses.map((addr) => (0, ethers_1.getAddress)(addr)));
64
+ for (let i = 0; i < maxIndicesToCheck; i++) {
65
+ const wallet = this.deriveWalletFromMnemonic(mnemonic, i);
66
+ const normalizedAddress = (0, ethers_1.getAddress)(wallet.address);
67
+ if (!existingSet.has(normalizedAddress)) {
68
+ return { index: i, wallet };
69
+ }
70
+ }
71
+ return null;
72
+ }
61
73
  async signMessage(privateKey, message) {
62
74
  const wallet = new ethers_1.Wallet(privateKey);
63
75
  return await wallet.signMessage(message);
@@ -72,9 +84,8 @@ class EvmWalletService {
72
84
  }
73
85
  }
74
86
  validateMnemonic(mnemonic) {
75
- if (!this.isValidMnemonic(mnemonic)) {
87
+ if (!this.isValidMnemonic(mnemonic))
76
88
  throw new Error('Invalid mnemonic phrase');
77
- }
78
89
  }
79
90
  getEntropyForWordCount(wordCount) {
80
91
  const entropyMap = {
@@ -20,6 +20,10 @@ export declare class EvmWalletService {
20
20
  generateMultipleWallets(mnemonic: string, count: number, startIndex?: number): EvmDerivedWallet[];
21
21
  deriveWalletFromMnemonic(mnemonic: string, accountIndex?: number, customPath?: string): EvmDerivedWallet;
22
22
  deriveFromPrivateKey(privateKey: string): Omit<EvmDerivedWallet, 'path'>;
23
+ getFirstAvailableIndexForMnemonicImport(mnemonic: string, existingAddresses: string[], maxIndicesToCheck?: number): {
24
+ index: number;
25
+ wallet: EvmDerivedWallet;
26
+ } | null;
23
27
  signMessage(privateKey: string, message: string): Promise<string>;
24
28
  isValidMnemonic(mnemonic: string): boolean;
25
29
  private validateMnemonic;
@@ -1,5 +1,5 @@
1
1
  /** npm imports */
2
- import { Wallet, HDNodeWallet, Mnemonic } from 'ethers';
2
+ import { Wallet, HDNodeWallet, Mnemonic, getAddress } from 'ethers';
3
3
  export class EvmWalletService {
4
4
  constructor(entropy) {
5
5
  this.entropy = entropy;
@@ -55,6 +55,18 @@ export class EvmWalletService {
55
55
  publicKey: wallet.signingKey.publicKey,
56
56
  };
57
57
  }
58
+ getFirstAvailableIndexForMnemonicImport(mnemonic, existingAddresses, maxIndicesToCheck = 20) {
59
+ this.validateMnemonic(mnemonic);
60
+ const existingSet = new Set(existingAddresses.map((addr) => getAddress(addr)));
61
+ for (let i = 0; i < maxIndicesToCheck; i++) {
62
+ const wallet = this.deriveWalletFromMnemonic(mnemonic, i);
63
+ const normalizedAddress = getAddress(wallet.address);
64
+ if (!existingSet.has(normalizedAddress)) {
65
+ return { index: i, wallet };
66
+ }
67
+ }
68
+ return null;
69
+ }
58
70
  async signMessage(privateKey, message) {
59
71
  const wallet = new Wallet(privateKey);
60
72
  return await wallet.signMessage(message);
@@ -69,9 +81,8 @@ export class EvmWalletService {
69
81
  }
70
82
  }
71
83
  validateMnemonic(mnemonic) {
72
- if (!this.isValidMnemonic(mnemonic)) {
84
+ if (!this.isValidMnemonic(mnemonic))
73
85
  throw new Error('Invalid mnemonic phrase');
74
- }
75
86
  }
76
87
  getEntropyForWordCount(wordCount) {
77
88
  const entropyMap = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octaflowlabs/onchain-sdk",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "onchain methods for web3",
5
5
  "repository": "https://github.com/crisramb665/onchain-sdk.git",
6
6
  "license": "MIT",