Skip to content

Commit

Permalink
Fixing an issue with ethers library derivePath function that was brea…
Browse files Browse the repository at this point in the history
…king some issues with wallet generation

Signed-off-by: Kipkap <kipkap1001@gmail.com>
  • Loading branch information
himalayan-dev committed Feb 24, 2024
1 parent e75f278 commit c9631bf
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "git+https://github.com/hashgraph/hedera-metamask-snaps.git"
},
"source": {
"shasum": "czc6KHQMgLwju9hzMn/WG1RUh+ORbHoXV3Ig0eDum3I=",
"shasum": "WvE9FBVYPcmo7FhAFIYbkAWjXqm56h2T1FrEkmxiMFQ=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
80 changes: 76 additions & 4 deletions packages/hedera-wallet-snap/packages/snap/src/utils/CryptoUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,78 @@
*/

import { PublicKey } from '@hashgraph/sdk';
import { HDNodeWallet, Mnemonic, ethers } from 'ethers';
import { HDNodeWallet, Mnemonic, assertArgument, ethers } from 'ethers';
import { DEFAULTCOINTYPE } from '../types/constants';

export class CryptoUtils {
/**
* Derives a wallet from the provided node using the provided path.
* NOTE: This method is a copy of the 'derivePath' method from the 'ethers' library as that method
* changed in the recent version and the new method does not work as expected.
*
* @param node - The node to derive the wallet from.
* @param path - The path to use for derivation.
* @returns The derived HDNodeWallet.
*/
// eslint-disable-next-line no-restricted-syntax
private static derivePathForWallet(
node: HDNodeWallet,
path: string,
): HDNodeWallet {
const components = path.split('/');

assertArgument(
components.length > 0 && (components[0] === 'm' || node.depth > 0),
'invalid path',
'path',
path,
);

if (components[0] === 'm') {
components.shift();
}

let result: HDNodeWallet = node;

const HardenedBit = 0x80000000;
for (let i = 0; i < components.length; i++) {
const component = components[i];

if (component.match(/^[0-9]+'$/u)) {
const index = parseInt(
component.substring(0, component.length - 1),
10,
);

assertArgument(
index < HardenedBit,
'invalid path index',
`path[${i}]`,
component,
);
result = result.deriveChild(HardenedBit + index);
} else if (component.match(/^[0-9]+$/u)) {
const index = parseInt(component, 10);
assertArgument(
index < HardenedBit,
'invalid path index',
`path[${i}]`,
component,
);
result = result.deriveChild(index);
} else {
assertArgument(
false,
'invalid path component',
`path[${i}]`,
component,
);
}
}

return result;
}

/**
* Generates a wallet using the provided EVM address to generate entropy.
*
Expand All @@ -40,9 +108,13 @@ export class CryptoUtils {
},
});

const nodeWallet = HDNodeWallet.fromMnemonic(
Mnemonic.fromEntropy(entropy),
).derivePath(`m/44/${DEFAULTCOINTYPE}/0/0/0`);
console.log('entropy: ', entropy);

let nodeWallet = HDNodeWallet.fromMnemonic(Mnemonic.fromEntropy(entropy));
nodeWallet = CryptoUtils.derivePathForWallet(
nodeWallet,
`m/44/${DEFAULTCOINTYPE}/0/0/0`,
);

return nodeWallet;
}
Expand Down

0 comments on commit c9631bf

Please sign in to comment.