Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/27989 Ledger tx signature #85

Merged
merged 22 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
87934ce
27989: LedgerKey class extending base Key
sdrug Feb 7, 2022
801d34b
27989: adjusting withWallet middleware to sign tx with ledger if requ…
sdrug Feb 7, 2022
48671a7
27989: Adding await to invokeMiddlewares
sdrug Feb 7, 2022
cf71bd4
27989: Making LedgerKey constructor private
sdrug Feb 7, 2022
bb8bc89
27989: prevent LedgerKey from keeping connection to the ledger open
sdrug Feb 8, 2022
1f127ea
27989: adding secp256k1 to parse the signature
sdrug Feb 8, 2022
5c2a28e
27989: redefining createSignature in LedgerKey to comply with amino e…
sdrug Feb 8, 2022
905e5bd
27979: LedgerKey centralised error handling
sdrug Feb 8, 2022
56b9f9e
27989: LedgerKey signing in legacy amino signing mode
sdrug Feb 9, 2022
8c05d70
27989: signatureImport input data changed straight to buffer
sdrug Feb 9, 2022
96b66f4
27989: Passing signMode straight into createAndSignTx
sdrug Feb 9, 2022
c8242bc
27989: formatting
sdrug Feb 9, 2022
2909c0e
regenerate yarn lock
RodrigoAD Feb 9, 2022
a5ce10b
fix integer validation on set billing
RodrigoAD Feb 9, 2022
b982df7
updated deps
RodrigoAD Feb 9, 2022
b2805af
27989: BIP44 compliant path parsing
sdrug Feb 9, 2022
ab31607
27989: checkForErrors -> assertNoErrors
sdrug Feb 9, 2022
e3c1039
add libusb1 to nix to solve the libudev issue
tateexon Feb 9, 2022
40d7219
27989: Formatting
sdrug Feb 10, 2022
ac1b164
Merge branch 'main' into feature/27989-ledger-support
sdrug Feb 10, 2022
ee092e7
Fix upload test to use yarn instead of executable
tateexon Feb 10, 2022
e1b8903
27989: resolving conflicts in packages versions
sdrug Feb 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 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 @@ -16,14 +18,14 @@ export class LedgerKey extends Key {
const { ledgerConnector, terminateConnection } = await this.connectToLedger()

const response = await ledgerConnector.getPublicKey(this.path)
this.checkForErrors(response)
this.assertNoErrors(response)

this.publicKey = new SimplePublicKey(Buffer.from(response.compressed_pk.data).toString('base64'))
await terminateConnection()
}

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 All @@ -35,7 +37,7 @@ export class LedgerKey extends Key {
try {
logger.info('Approve tx on your Ledger device.')
const response = await ledgerConnector.sign(this.path, payload)
this.checkForErrors(response)
this.assertNoErrors(response)

const signature = signatureImport(Buffer.from(response.signature.data))
return Buffer.from(signature)
Expand All @@ -51,19 +53,23 @@ export class LedgerKey extends Key {
const transport = await TransportNodeHid.create()
const ledgerConnector = new LedgerTerraConnector(transport)
const response = await ledgerConnector.initialize()
this.checkForErrors(response)
this.assertNoErrors(response)

return {
ledgerConnector,
terminateConnection: transport.close.bind(transport),
}
}

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

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

private checkForErrors(response: CommonResponse) {
private assertNoErrors(response: CommonResponse) {
if (!response) return

const { error_message: ledgerError, return_code: returnCode, device_locked: isLocked } = response
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"