Skip to content

Commit

Permalink
refactor: dry out remaining describe path functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnerdhair committed Apr 7, 2022
1 parent 98eba36 commit 003f218
Show file tree
Hide file tree
Showing 19 changed files with 185 additions and 341 deletions.
31 changes: 9 additions & 22 deletions integration/src/thorchain/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,17 @@ export function thorchainTests(get: () => { wallet: core.HDWallet; info: core.HD
coin: "Thorchain",
});

// This is strange, and probably wrong, behavior... but it's what happens.
if (wallet.getVendor() === "KeepKey") {
// eslint-disable-next-line jest/no-conditional-expect
expect(out).toMatchInlineSnapshot(`
// eslint-disable-next-line jest/no-conditional-expect
expect(out).toMatchInlineSnapshot(`
Object {
"coin": "Thorchain",
"isKnown": false,
"scriptType": undefined,
"verbose": "m/44'/931'/0'/0/0",
"accountIdx": 0,
"coin": "Rune",
"isKnown": true,
"isPrefork": false,
"verbose": "THORChain Account #0",
"wholeAccount": true,
}
`);
} else {
// eslint-disable-next-line jest/no-conditional-expect
expect(out).toMatchInlineSnapshot(`
Object {
"accountIdx": 0,
"coin": "Thorchain",
"isKnown": true,
"isPrefork": false,
"verbose": "Thorchain Account #0",
"wholeAccount": true,
}
`);
}
`);
},
TIMEOUT
);
Expand Down
2 changes: 1 addition & 1 deletion packages/hdwallet-core/src/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ export function unknownUTXOPath(path: BIP32Path, coin: Coin, scriptType?: BTCInp
};
}

export function describeUTXOPath(path: BIP32Path, coin: Coin, scriptType?: BTCInputScriptType): PathDescription {
export function btcDescribePath(path: BIP32Path, coin: Coin, scriptType?: BTCInputScriptType): PathDescription {
const unknown = unknownUTXOPath(path, coin, scriptType);

if (path.length !== 3 && path.length !== 5) return unknown;
Expand Down
40 changes: 40 additions & 0 deletions packages/hdwallet-core/src/eos.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { addressNListToBIP32, PathDescription, slip44ByCoin } from ".";
import { BIP32Path, HDWallet, HDWalletInfo } from "./wallet";

export interface EosGetPublicKey {
Expand Down Expand Up @@ -93,3 +94,42 @@ export interface EosWallet extends EosWalletInfo, HDWallet {
eosGetPublicKey(msg: EosGetPublicKey): Promise<string | null>;
eosSignTx(msg: EosToSignTx): Promise<EosTxSigned | null>;
}

export function eosDescribePath(path: BIP32Path): PathDescription {
const pathStr = addressNListToBIP32(path);
const unknown: PathDescription = {
verbose: pathStr,
coin: "Eos",
isKnown: false,
};

if (path.length != 5) {
return unknown;
}

if (path[0] != 0x80000000 + 44) {
return unknown;
}

if (path[1] != 0x80000000 + slip44ByCoin("Eos")) {
return unknown;
}

if ((path[2] & 0x80000000) >>> 0 !== 0x80000000) {
return unknown;
}

if (path[3] !== 0 || path[4] !== 0) {
return unknown;
}

const index = path[2] & 0x7fffffff;
return {
verbose: `Eos Account #${index}`,
accountIdx: index,
wholeAccount: true,
coin: "Eos",
isKnown: true,
isPrefork: false,
};
}
12 changes: 6 additions & 6 deletions packages/hdwallet-core/src/ethereum.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { bip32ToAddressNList } from ".";
import { describeETHPath, ETHAddressDerivationScheme } from "./ethereum";
import { ETHAddressDerivationScheme, ethDescribePath } from "./ethereum";

describe("describeETHPath", () => {
describe("ethDescribePath", () => {
it("works with BIP44 derivation", async () => {
const describePath = (x: string) =>
describeETHPath(bip32ToAddressNList(x), ETHAddressDerivationScheme.BIP44).verbose;
ethDescribePath(bip32ToAddressNList(x), ETHAddressDerivationScheme.BIP44).verbose;

expect(describePath("m/1/2/3")).toMatchInlineSnapshot(`"m/1/2/3"`);
expect(describePath("m/44'/123")).toMatchInlineSnapshot(`"m/44'/123"`);
Expand Down Expand Up @@ -36,7 +36,7 @@ describe("describeETHPath", () => {

it("works with Metamask derivation", async () => {
const describePath = (x: string) =>
describeETHPath(bip32ToAddressNList(x), ETHAddressDerivationScheme.Metamask).verbose;
ethDescribePath(bip32ToAddressNList(x), ETHAddressDerivationScheme.Metamask).verbose;

expect(describePath("m/1/2/3")).toMatchInlineSnapshot(`"m/1/2/3"`);
expect(describePath("m/44'/123")).toMatchInlineSnapshot(`"m/44'/123"`);
Expand Down Expand Up @@ -68,7 +68,7 @@ describe("describeETHPath", () => {

it("works with OldLedger derivation", async () => {
const describePath = (x: string) =>
describeETHPath(bip32ToAddressNList(x), ETHAddressDerivationScheme.OldLedger).verbose;
ethDescribePath(bip32ToAddressNList(x), ETHAddressDerivationScheme.OldLedger).verbose;

expect(describePath("m/1/2/3")).toMatchInlineSnapshot(`"m/1/2/3"`);
expect(describePath("m/44'/123")).toMatchInlineSnapshot(`"m/44'/123"`);
Expand Down Expand Up @@ -100,7 +100,7 @@ describe("describeETHPath", () => {

it("works with Ledger derivation", async () => {
const describePath = (x: string) =>
describeETHPath(bip32ToAddressNList(x), ETHAddressDerivationScheme.Ledger).verbose;
ethDescribePath(bip32ToAddressNList(x), ETHAddressDerivationScheme.Ledger).verbose;

expect(describePath("m/1/2/3")).toMatchInlineSnapshot(`"m/1/2/3"`);
expect(describePath("m/44'/123")).toMatchInlineSnapshot(`"m/44'/123"`);
Expand Down
2 changes: 1 addition & 1 deletion packages/hdwallet-core/src/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export enum ETHAddressDerivationScheme {
Ledger = "ledger",
}

export function describeETHPath(
export function ethDescribePath(
path: BIP32Path,
addressDerivationScheme = ETHAddressDerivationScheme.BIP44
): PathDescription {
Expand Down
40 changes: 40 additions & 0 deletions packages/hdwallet-core/src/ripple.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { addressNListToBIP32, PathDescription, slip44ByCoin } from ".";
import { BIP32Path, HDWallet, HDWalletInfo } from "./wallet";

export interface RippleGetAddress {
Expand Down Expand Up @@ -97,3 +98,42 @@ export interface RippleWallet extends RippleWalletInfo, HDWallet {
rippleGetAddress(msg: RippleGetAddress): Promise<string | null>;
rippleSignTx(msg: RippleSignTx): Promise<RippleSignedTx | null>;
}

export function rippleDescribePath(path: BIP32Path): PathDescription {
const pathStr = addressNListToBIP32(path);
const unknown: PathDescription = {
verbose: pathStr,
coin: "Ripple",
isKnown: false,
};

if (path.length != 5) {
return unknown;
}

if (path[0] != 0x80000000 + 44) {
return unknown;
}

if (path[1] != 0x80000000 + slip44ByCoin("Ripple")) {
return unknown;
}

if ((path[2] & 0x80000000) >>> 0 !== 0x80000000) {
return unknown;
}

if (path[3] !== 0 || path[4] !== 0) {
return unknown;
}

const index = path[2] & 0x7fffffff;
return {
verbose: `Ripple Account #${index}`,
accountIdx: index,
wholeAccount: true,
coin: "Ripple",
isKnown: true,
isPrefork: false,
};
}
4 changes: 2 additions & 2 deletions packages/hdwallet-core/src/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ export function thorchainDescribePath(path: BIP32Path): PathDescription {

const index = path[2] & 0x7fffffff;
return {
verbose: `Thorchain Account #${index}`,
verbose: `THORChain Account #${index}`,
accountIdx: index,
wholeAccount: true,
coin: "Thorchain",
coin: "Rune",
isKnown: true,
isPrefork: false,
};
Expand Down
55 changes: 55 additions & 0 deletions packages/hdwallet-core/src/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import _ from "lodash";

import {
binanceDescribePath,
btcDescribePath,
cosmosDescribePath,
eosDescribePath,
ETHAddressDerivationScheme,
ethDescribePath,
fioDescribePath,
kavaDescribePath,
osmosisDescribePath,
rippleDescribePath,
secretDescribePath,
terraDescribePath,
thorchainDescribePath,
} from ".";
import { BinanceWallet, BinanceWalletInfo } from "./binance";
import { BTCInputScriptType, BTCWallet, BTCWalletInfo } from "./bitcoin";
import { CosmosWallet, CosmosWalletInfo } from "./cosmos";
Expand Down Expand Up @@ -385,3 +400,43 @@ export interface HDWallet extends HDWalletInfo {
*/
disconnect(): Promise<void>;
}

export function describePath(
msg: DescribePath,
ethAddressDerivationScheme?: ETHAddressDerivationScheme
): PathDescription {
switch (msg.coin.toLowerCase()) {
case "ethereum":
return ethDescribePath(msg.path, ethAddressDerivationScheme);
case "atom":
return cosmosDescribePath(msg.path);
case "rune":
case "trune":
case "thorchain":
return thorchainDescribePath(msg.path);
case "secret":
case "scrt":
case "tscrt":
return secretDescribePath(msg.path);
case "luna":
case "terra":
case "tluna":
return terraDescribePath(msg.path);
case "kava":
case "tkava":
return kavaDescribePath(msg.path);
case "binance":
return binanceDescribePath(msg.path);
case "osmosis":
case "osmo":
return osmosisDescribePath(msg.path);
case "fio":
return fioDescribePath(msg.path);
case "ripple":
return rippleDescribePath(msg.path);
case "eos":
return eosDescribePath(msg.path);
default:
return btcDescribePath(msg.path, msg.coin, msg.scriptType);
}
}
Loading

0 comments on commit 003f218

Please sign in to comment.