Skip to content

Commit

Permalink
feat: display additional ledger device info
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Apr 24, 2023
1 parent 40e14e7 commit b74a92c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 deletions.
7 changes: 3 additions & 4 deletions src/chrome/dev-tools/components/LedgerSimulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const LedgerSimulator = () => {

const getDeviceId = (wallet: BaseHdWallet): string => {
const publicKey = wallet.derivePath(`365'`).publicKey
return blake2b(64).update(Buffer.from(publicKey, 'hex')).digest('hex')
return blake2b(32).update(Buffer.from(publicKey, 'hex')).digest('hex')
}

const sendDeviceIdResponse = async () => {
Expand Down Expand Up @@ -125,9 +125,8 @@ export const LedgerSimulator = () => {

const signTx = async () => {
const wallet = createRadixWallet({ seed, curve })
const privateKey = wallet.deriveFullPath(hdPath).privateKey
const publicKey = wallet.deriveFullPath(hdPath).publicKey
const hash = blake2b(64)
const { privateKey, publicKey } = wallet.deriveFullPath(hdPath)
const hash = blake2b(32)
.update(Buffer.from(txIntent, 'base64'))
.digest('hex')

Expand Down
16 changes: 16 additions & 0 deletions src/ledger/components/ledger-device-box.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Box, Header } from 'components'
import { LedgerDevice } from '../schemas'

const deviceName = {
nanoS: 'Nano S',
'nanoS+': 'Nano S+',
nanoX: 'Nano X',
} as const

export const LedgerDeviceBox = (params: LedgerDevice) => (
<Box textAlign="center" py="large">
<Header>
{params.name} ({deviceName[params.model]})
</Header>
</Box>
)
26 changes: 11 additions & 15 deletions src/ledger/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ const ledgerDiscriminator = union([
literal('importOlympiaDevice'),
])

export const LedgerDeviceSchema = object({
name: string().optional(),
model: ledgerDeviceModel,
id: string(),
})

export type LedgerDevice = z.infer<typeof LedgerDeviceSchema>

export const LedgerDeviceIdRequestSchema = object({
interactionId: string(),
discriminator: literal('getDeviceInfo'),
Expand All @@ -36,11 +44,7 @@ export const LedgerPublicKeyRequestSchema = object({
curve,
derivationPath: string(),
}),
ledgerDevice: object({
name: string().optional(),
model: ledgerDeviceModel,
id: string(),
}),
ledgerDevice: LedgerDeviceSchema,
})

export type LedgerPublicKeyRequest = z.infer<
Expand All @@ -54,11 +58,7 @@ export const LedgerSignTransactionRequestSchema = object({
curve,
derivationPath: string(),
}),
ledgerDevice: object({
name: string().optional(),
model: ledgerDeviceModel,
id: string(),
}),
ledgerDevice: LedgerDeviceSchema,
compiledTransactionIntent: string(),
mode: union([literal('verbose'), literal('summary')]),
})
Expand All @@ -74,11 +74,7 @@ export const LedgerSignChallengeRequestSchema = object({
curve,
derivationPath: string(),
}),
ledgerDevice: object({
name: string().optional(),
model: ledgerDeviceModel,
id: string(),
}),
ledgerDevice: LedgerDeviceSchema,
challenge: string(),
})

Expand Down
20 changes: 15 additions & 5 deletions src/ledger/views/apply-ledger-factor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Button, Header } from 'components'
import { Button } from 'components'
import { ErrorText } from '../components/error-text'
import { ledger } from 'ledger/ledger-wrapper'
import {
Expand All @@ -8,6 +8,12 @@ import {
} from 'ledger/schemas'
import { PairingHeader } from 'pairing/components/pairing-header'
import { useState } from 'react'
import { LedgerDeviceBox } from 'ledger/components/ledger-device-box'

const EntityType = {
Account: '525',
Identity: '618'
} as const

export const ApplyLedgerFactor = ({
message,
Expand All @@ -19,6 +25,12 @@ export const ApplyLedgerFactor = ({
const [error, setError] = useState<string | undefined>()
const [isLoading, setIsLoading] = useState<boolean>(false)

const header = message.keyParameters.derivationPath
.split('/')[4]
.includes(EntityType.Account)
? 'Apply Ledger Factor to Account Security'
: 'Apply Ledger Factor to Persona Security'

const getPublicKey = async () => {
setIsLoading(true)
const publicKey = await ledger.getPublicKey(message)
Expand All @@ -32,15 +44,13 @@ export const ApplyLedgerFactor = ({
}
return (
<>
<PairingHeader header="Apply Ledger Factor to Account Security">
<PairingHeader header={header}>
Please connect the following Ledger hardware wallet device to this
computer and click Continue:
</PairingHeader>

<ErrorText error={error} />
<Box textAlign="center" py="large">
<Header>{message.ledgerDevice.name}</Header>
</Box>
<LedgerDeviceBox {...message.ledgerDevice} />
<Button full mt="large" onClick={getPublicKey} disabled={isLoading}>
Continue
</Button>
Expand Down
7 changes: 3 additions & 4 deletions src/ledger/views/sign-challenge.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Box, Button, Header } from 'components'
import { Button } from 'components'
import { LedgerDeviceBox } from 'ledger/components/ledger-device-box'
import { LedgerResponse, LedgerSignChallengeRequest } from 'ledger/schemas'
import { PairingHeader } from 'pairing/components/pairing-header'

Expand All @@ -14,9 +15,7 @@ export const SignChallenge = ({
Please connect the following Ledger hardware wallet device to this
computer and click Continue:
</PairingHeader>
<Box textAlign="center" py="large">
<Header>{message.ledgerDevice.name}</Header>
</Box>
<LedgerDeviceBox {...message.ledgerDevice} />
<Button>Continue</Button>
</>
)
8 changes: 3 additions & 5 deletions src/ledger/views/sign-transaction.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Button, Header, Box } from 'components'
import { Button } from 'components'
import { ErrorText } from 'ledger/components/error-text'
import { LedgerDeviceBox } from 'ledger/components/ledger-device-box'
import { ledger } from 'ledger/ledger-wrapper'
import {
LedgerResponse,
Expand Down Expand Up @@ -39,10 +40,7 @@ export const SignTransaction = ({
</PairingHeader>
<ErrorText error={error} />

<Box textAlign="center" py="large">
<Header>{message.ledgerDevice.name}</Header>
</Box>

<LedgerDeviceBox {...message.ledgerDevice} />
<Button onClick={sign} disabled={isLoading}>
Continue
</Button>
Expand Down

0 comments on commit b74a92c

Please sign in to comment.