Skip to content

Commit

Permalink
dry out remaining describe path functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnerdhair committed Feb 12, 2022
1 parent a7636de commit 7114493
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 207 deletions.
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 @@ -91,3 +92,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 {
let pathStr = addressNListToBIP32(path);
let 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;
}

let index = path[2] & 0x7fffffff;
return {
verbose: `Eos Account #${index}`,
accountIdx: index,
wholeAccount: true,
coin: "Eos",
isKnown: true,
isPrefork: false,
};
}
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 @@ -92,3 +93,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 {
let pathStr = addressNListToBIP32(path);
let 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;
}

let 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 @@ -124,10 +124,10 @@ export function thorchainDescribePath(path: BIP32Path): PathDescription {

let 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
213 changes: 9 additions & 204 deletions packages/hdwallet-keepkey/src/keepkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,201 +19,6 @@ export function isKeepKey(wallet: core.HDWallet): wallet is KeepKeyHDWallet {
return _.isObject(wallet) && (wallet as any)._isKeepKey;
}

function describeCosmosPath(path: core.BIP32Path): core.PathDescription {
let pathStr = core.addressNListToBIP32(path);
let unknown: core.PathDescription = {
verbose: pathStr,
coin: "Atom",
isKnown: false,
};

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

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

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

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

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

let index = path[2] & 0x7fffffff;
return {
verbose: `Cosmos Account #${index}`,
accountIdx: index,
wholeAccount: true,
coin: "Atom",
isKnown: true,
isPrefork: false,
};
}

function describeThorchainPath(path: core.BIP32Path): core.PathDescription {
let pathStr = core.addressNListToBIP32(path);
let unknown: core.PathDescription = {
verbose: pathStr,
coin: "Rune",
isKnown: false,
};

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

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

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

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

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

let index = path[2] & 0x7fffffff;
return {
verbose: `THORChain Account #${index}`,
accountIdx: index,
wholeAccount: true,
coin: "Rune",
isKnown: true,
isPrefork: false,
};
}

function describeEosPath(path: core.BIP32Path): core.PathDescription {
let pathStr = core.addressNListToBIP32(path);
let unknown: core.PathDescription = {
verbose: pathStr,
coin: "Eos",
isKnown: false,
};

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

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

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

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

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

let index = path[2] & 0x7fffffff;
return {
verbose: `Eos Account #${index}`,
accountIdx: index,
wholeAccount: true,
coin: "Eos",
isKnown: true,
isPrefork: false,
};
}

function describeRipplePath(path: core.BIP32Path): core.PathDescription {
let pathStr = core.addressNListToBIP32(path);
let unknown: core.PathDescription = {
verbose: pathStr,
coin: "Ripple",
isKnown: false,
};

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

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

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

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

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

let index = path[2] & 0x7fffffff;
return {
verbose: `Ripple Account #${index}`,
accountIdx: index,
wholeAccount: true,
coin: "Ripple",
isKnown: true,
isPrefork: false,
};
}

function describeBinancePath(path: core.BIP32Path): core.PathDescription {
let pathStr = core.addressNListToBIP32(path);
let unknown: core.PathDescription = {
verbose: pathStr,
coin: "Binance",
isKnown: false,
};

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

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

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

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

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

let index = path[2] & 0x7fffffff;
return {
verbose: `Binance Account #${index}`,
accountIdx: index,
wholeAccount: true,
coin: "Binance",
isKnown: true,
isPrefork: false,
};
}

export class KeepKeyHDWalletInfo
implements
core.HDWalletInfo,
Expand Down Expand Up @@ -334,13 +139,13 @@ export class KeepKeyHDWalletInfo
case "Ethereum":
return core.describeETHPath(msg.path);
case "Atom":
return describeCosmosPath(msg.path);
return core.cosmosDescribePath(msg.path);
case "Binance":
return describeBinancePath(msg.path);
return core.binanceDescribePath(msg.path);
case "Ripple":
return describeRipplePath(msg.path);
return core.rippleDescribePath(msg.path);
case "Eos":
return describeEosPath(msg.path);
return core.eosDescribePath(msg.path);
default:
return core.describeUTXOPath(msg.path, msg.coin, msg.scriptType);
}
Expand Down Expand Up @@ -390,7 +195,7 @@ export class KeepKeyHDWalletInfo
}

public cosmosNextAccountPath(msg: core.CosmosAccountPath): core.CosmosAccountPath | undefined {
let description = describeCosmosPath(msg.addressNList);
let description = core.cosmosDescribePath(msg.addressNList);
if (!description.isKnown) {
return undefined;
}
Expand All @@ -405,7 +210,7 @@ export class KeepKeyHDWalletInfo
}

public thorchainNextAccountPath(msg: core.ThorchainAccountPath): core.ThorchainAccountPath | undefined {
let description = describeThorchainPath(msg.addressNList);
let description = core.thorchainDescribePath(msg.addressNList);
if (!description.isKnown) {
return undefined;
}
Expand All @@ -420,7 +225,7 @@ export class KeepKeyHDWalletInfo
}

public rippleNextAccountPath(msg: core.RippleAccountPath): core.RippleAccountPath | undefined {
let description = describeRipplePath(msg.addressNList);
let description = core.rippleDescribePath(msg.addressNList);
if (!description.isKnown) {
return undefined;
}
Expand All @@ -434,7 +239,7 @@ export class KeepKeyHDWalletInfo
}

public binanceNextAccountPath(msg: core.BinanceAccountPath): core.BinanceAccountPath | undefined {
let description = describeBinancePath(msg.addressNList);
let description = core.binanceDescribePath(msg.addressNList);
if (!description.isKnown) {
return undefined;
}
Expand All @@ -449,7 +254,7 @@ export class KeepKeyHDWalletInfo
}

public eosNextAccountPath(msg: core.EosAccountPath): core.EosAccountPath | undefined {
let description = describeEosPath(msg.addressNList);
let description = core.eosDescribePath(msg.addressNList);
if (!description.isKnown) {
return undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/hdwallet-native/src/native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe("NativeHDWalletInfo", () => {
},
{
msg: { coin: "rune", path: [44 + 0x80000000, 931 + 0x80000000, 0 + 0x80000000, 0, 0] },
out: { coin: "Thorchain", verbose: "Thorchain Account #0", isKnown: true },
out: { coin: "Rune", verbose: "THORChain Account #0", isKnown: true },
},
{
msg: { coin: "secret", path: [44 + 0x80000000, 529 + 0x80000000, 0 + 0x80000000, 0, 0] },
Expand Down

0 comments on commit 7114493

Please sign in to comment.