Skip to content
Snippets Groups Projects
Verified Commit 2725f3eb authored by Mateusz Tyszczak's avatar Mateusz Tyszczak :scroll:
Browse files

Use BIP44 entropy instead of BIP32

parent 0cd657e3
No related branches found
No related tags found
No related merge requests found
Pipeline #118995 passed
{ {
"name": "@hiveio/metamask-snap", "name": "@hiveio/metamask-snap",
"version": "1.3.3", "version": "1.4.0",
"description": "Hive wallet extension allowing you to sign transactions using keys derived from your Metamask wallet", "description": "Hive wallet extension allowing you to sign transactions using keys derived from your Metamask wallet",
"main": "./dist/bundle.js", "main": "./dist/bundle.js",
"files": [ "files": [
......
{ {
"version": "1.3.3", "version": "1.4.0",
"description": "Hive wallet extension allowing you to sign transactions using keys derived from your Metamask wallet", "description": "Hive wallet extension allowing you to sign transactions using keys derived from your Metamask wallet",
"proposedName": "Hive Wallet", "proposedName": "Hive Wallet",
"repository": { "repository": {
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
"url": "git+https://gitlab.syncad.com/hive/metamask-snap.git" "url": "git+https://gitlab.syncad.com/hive/metamask-snap.git"
}, },
"source": { "source": {
"shasum": "O7LMRoprQOJvI9FF1oXBJX0UdRamQM8jXstqhRspMUQ=", "shasum": "QnK+W9d9Xy2Iv5TIZRhULm6OdccrCPxDGiRtY7kZtcU=",
"location": { "location": {
"npm": { "npm": {
"filePath": "dist/bundle.js", "filePath": "dist/bundle.js",
...@@ -24,16 +24,9 @@ ...@@ -24,16 +24,9 @@
"snaps": false "snaps": false
}, },
"endowment:webassembly": {}, "endowment:webassembly": {},
"snap_getBip32Entropy": [ "snap_getBip44Entropy": [
{ {
"path": ["m", "48'", "13'"], "coinType": 3054
"curve": "secp256k1"
}
],
"snap_getBip32PublicKey": [
{
"path": ["m", "48'", "13'"],
"curve": "secp256k1"
} }
] ]
}, },
......
...@@ -2,7 +2,7 @@ import type { TPublicKey, TRole } from "@hiveio/wax"; ...@@ -2,7 +2,7 @@ import type { TPublicKey, TRole } from "@hiveio/wax";
import type { KeyIndex } from "../rpc"; import type { KeyIndex } from "../rpc";
import { getWax } from "../hive/wax"; import { getWax } from "../hive/wax";
import { remove0x } from "@metamask/utils"; import { remove0x } from "@metamask/utils";
import { SLIP10Node } from "@metamask/key-tree"; import { type BIP44Node, getBIP44AddressKeyDeriver } from "@metamask/key-tree";
import type { IBeekeeperUnlockedWallet } from "@hiveio/beekeeper"; import type { IBeekeeperUnlockedWallet } from "@hiveio/beekeeper";
const KeyIndexToPathMap = { const KeyIndexToPathMap = {
...@@ -12,27 +12,32 @@ const KeyIndexToPathMap = { ...@@ -12,27 +12,32 @@ const KeyIndexToPathMap = {
posting: 4 posting: 4
} as const satisfies Record<TRole, number>; } as const satisfies Record<TRole, number>;
export const keyIndexToPath = (keyIndex: KeyIndex): string[] => { const CoinType = 0xBEE;
export const keyIndexToBip44Node = async(keyIndex: KeyIndex): Promise<BIP44Node> => {
const bip44 = await snap.request({
method: 'snap_getBip44Entropy',
params: {
coinType: CoinType
}
});
// Create a function that takes an index and returns an extended private key for m/44'/3054'/accountIndex'/0/address_index
const deriveHiveAddress = await getBIP44AddressKeyDeriver(bip44, { account: keyIndex.accountIndex ?? 0, change: 0 });
const keyIndexType = KeyIndexToPathMap[keyIndex.role]; const keyIndexType = KeyIndexToPathMap[keyIndex.role];
if (keyIndexType === undefined) if (keyIndexType === undefined)
throw new Error(`Invalid key index type: ${keyIndex.role}`); throw new Error(`Invalid key index type: ${keyIndex.role}`);
return ["m", "48'", "13'", `${keyIndexType}'`, `${keyIndex.accountIndex ?? 0}'`, "0'"]; // Derive the second Hive address, which has a proper index.
return await deriveHiveAddress(keyIndexType);
}; };
export const getPublicKeyWifFromKeyIndex = async (keyIndex: KeyIndex): Promise<TPublicKey> => { export const getPublicKeyWifFromKeyIndex = async (keyIndex: KeyIndex): Promise<TPublicKey> => {
const wax = await getWax(); const wax = await getWax();
const bip32 = await snap.request({ const bip44Node = await keyIndexToBip44Node(keyIndex);
method: 'snap_getBip32PublicKey',
params: {
curve: "secp256k1",
path: keyIndexToPath(keyIndex),
compressed: true
}
});
const publicKey = wax.convertRawPublicKeyToWif(remove0x(bip32)); const publicKey = wax.convertRawPublicKeyToWif(remove0x(bip44Node.compressedPublicKey));
return publicKey; return publicKey;
}; };
...@@ -40,20 +45,12 @@ export const getPublicKeyWifFromKeyIndex = async (keyIndex: KeyIndex): Promise<T ...@@ -40,20 +45,12 @@ export const getPublicKeyWifFromKeyIndex = async (keyIndex: KeyIndex): Promise<T
export const getPrivateKeyWifFromKeyIndex = async (keyIndex: KeyIndex): Promise<string> => { export const getPrivateKeyWifFromKeyIndex = async (keyIndex: KeyIndex): Promise<string> => {
const wax = await getWax(); const wax = await getWax();
const snapResponse = await snap.request({ const bip44Node = await keyIndexToBip44Node(keyIndex);
method: 'snap_getBip32Entropy',
params: {
curve: "secp256k1",
path: keyIndexToPath(keyIndex)
}
});
const node = await SLIP10Node.fromJSON(snapResponse);
if (!node.privateKey) if (!bip44Node.privateKey)
throw new Error('No private key found'); throw new Error('No private key found');
const wif = wax.convertRawPrivateKeyToWif(remove0x(node.privateKey)); const wif = wax.convertRawPrivateKeyToWif(remove0x(bip44Node.privateKey));
return wif; return wif;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment