Skip to content

Commit

Permalink
27989: BIP44 compliant path parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sdrug committed Feb 9, 2022
1 parent b982df7 commit b2805af
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
12 changes: 9 additions & 3 deletions packages-ts/gauntlet-terra/src/commands/ledgerKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import TransportNodeHid from '@ledgerhq/hw-transport-node-hid'
import { logger } from '@chainlink/gauntlet-core/dist/utils'
import { signatureImport } from 'secp256k1'

const BIP44_REGEX = /^(44)\'\s*\/\s*(\d+)\'\s*\/\s*([0,1]+)\'\s*\/\s*(\d+)\s*\/\s*(\d+)$/

export class LedgerKey extends Key {
private path: Array<number>

Expand All @@ -23,7 +25,7 @@ export class LedgerKey extends Key {
}

public static async create(path: string): Promise<LedgerKey> {
const pathArr = this.pathStringToArray(path)
const pathArr = this.bip44PathtoArray(path)
const ledgerKey = new LedgerKey(pathArr)
await ledgerKey.initialize()

Expand Down Expand Up @@ -59,8 +61,12 @@ export class LedgerKey extends Key {
}
}

private static pathStringToArray(path: string): Array<number> {
return path.split("'/").map((item) => parseInt(item))
private static bip44PathtoArray(path: string): Array<number> {
const match = BIP44_REGEX.exec(path)
if (!match)
throw new Error('Invalid BIP44 path!')

return match.slice(1).map(Number)
}

private checkForErrors(response: CommonResponse) {
Expand Down
4 changes: 2 additions & 2 deletions packages-ts/gauntlet-terra/src/commands/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { assertions, io, logger } from '@chainlink/gauntlet-core/dist/utils'
import TerraCommand from './internal/terra'
import path from 'path'
import { existsSync } from 'fs'
import { LEDGER_ULUNA_PATH } from '../lib/constants'
import { BIP44_LUNA_PATH } from '../lib/constants'

const isValidURL = (a) => true
export const withProvider: Middleware = (c: TerraCommand, next: Next) => {
Expand All @@ -25,7 +25,7 @@ export const withProvider: Middleware = (c: TerraCommand, next: Next) => {
export const withWallet: Middleware = async (c: TerraCommand, next: Next) => {
let key: Key
if (c.flags.withLedger || !!process.env.WITH_LEDGER) {
const path = c.flags.ledgerPath || LEDGER_ULUNA_PATH
const path = c.flags.ledgerPath || BIP44_LUNA_PATH
key = await LedgerKey.create(path)
} else {
const mnemonic = process.env.MNEMONIC
Expand Down
2 changes: 1 addition & 1 deletion packages-ts/gauntlet-terra/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const ADDRESS_ZERO = 'terra000000000000000000000000000000000000000'
export const LEDGER_ULUNA_PATH = "44'/330'/0'/0'/0"
export const BIP44_LUNA_PATH = "44'/330'/0'/0/0"

0 comments on commit b2805af

Please sign in to comment.