Skip to content

Commit

Permalink
simplify round3
Browse files Browse the repository at this point in the history
  • Loading branch information
patnir committed Sep 20, 2024
1 parent b3e1481 commit f40294e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 65 deletions.
113 changes: 50 additions & 63 deletions ironfish-cli/src/commands/wallet/multisig/dkg/round3.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { AccountFormat, encodeAccountImport } from '@ironfish/sdk'
import { AccountFormat, RpcClient, encodeAccountImport } from '@ironfish/sdk'
import { Flags } from '@oclif/core'
import { IronfishCommand } from '../../../../command'
import { RemoteFlags } from '../../../../flags'
Expand Down Expand Up @@ -108,55 +108,13 @@ export class DkgRound3Command extends IronfishCommand {
round2PublicPackages = round2PublicPackages.map((i) => i.trim())

if (flags.ledger) {
const identityResponse = await client.wallet.multisig.getIdentity({
name: participantName,
})
const identity = identityResponse.content.identity

const { dkgKeys, publicKeyPackage } = await this.performRound3WithLedger(
identity,
await this.performRound3WithLedger(
client,
participantName,
round1PublicPackages,
round2PublicPackages,
round2SecretPackage,
)

const accountImport = {
...dkgKeys,
multisigKeys: {
publicKeyPackage: publicKeyPackage.toString('hex'),
identity,
},
version: 4,
name: participantName,
spendingKey: null,
createdAt: null,
}

// Import multisig account
const response = await client.wallet.importAccount({
account: encodeAccountImport(accountImport, AccountFormat.Base64Json),
})

this.log()
this.log(
`Account ${response.content.name} imported with public address: ${dkgKeys.publicAddress}`,
)

this.log()
this.log('Creating an encrypted backup of multisig keys from your Ledger device...')
this.log()

const encryptedKeys = await this.backupKeysWithLedger()

this.log()
this.log('Encrypted Ledger Multisig Backup:')
this.log(encryptedKeys.toString('hex'))
this.log()
this.log('Please save the encrypted keys show above.')
this.log(
'Use `ironfish wallet:multisig:ledger:restore` if you need to restore the keys to your Ledger.',
)

return
}

Expand All @@ -174,33 +132,62 @@ export class DkgRound3Command extends IronfishCommand {
)
}

async backupKeysWithLedger(): Promise<Buffer> {
const ledger = await createLedgerInstance(true, this.logger)
return ledger.dkgBackupKeys()
}

async performRound3WithLedger(
identity: string,
client: RpcClient,
participantName: string,
round1PublicPackagesStr: string[],
round2PublicPackagesStr: string[],
round2SecretPackage: string,
): Promise<{
dkgKeys: {
publicAddress: string
viewKey: string
incomingViewKey: string
outgoingViewKey: string
proofAuthorizingKey: string
}
publicKeyPackage: Buffer
}> {
): Promise<void> {
const ledger = await createLedgerInstance(true, this.logger)

return ledger.dkgRound3(
const identityResponse = await client.wallet.multisig.getIdentity({ name: participantName })
const identity = identityResponse.content.identity

// Perform round3 with Ledger
const { dkgKeys, publicKeyPackage } = await ledger.dkgRound3(
0,
identity,
round1PublicPackagesStr,
round2PublicPackagesStr,
round2SecretPackage,
)

const accountImport = {
...dkgKeys,
multisigKeys: {
publicKeyPackage: publicKeyPackage.toString('hex'),
identity,
},
version: 4,
name: participantName,
spendingKey: null,
createdAt: null,
}

// Import multisig account
const response = await client.wallet.importAccount({
account: encodeAccountImport(accountImport, AccountFormat.Base64Json),
})

this.log()
this.log(
`Account ${response.content.name} imported with public address: ${dkgKeys.publicAddress}`,
)

this.log()
this.log('Creating an encrypted backup of multisig keys from your Ledger device...')
this.log()

const encryptedKeys = await ledger.dkgBackupKeys()

this.log()
this.log('Encrypted Ledger Multisig Backup:')
this.log(encryptedKeys.toString('hex'))
this.log()
this.log('Please save the encrypted keys show above.')
this.log(
'Use `ironfish wallet:multisig:ledger:restore` if you need to restore the keys to your Ledger.',
)
}
}
5 changes: 3 additions & 2 deletions ironfish-cli/src/utils/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
deserializePublicPackage,
deserializeRound2CombinedPublicPackage,
} from '@ironfish/rust-nodejs'
import { AccountImport, createRootLogger, Logger } from '@ironfish/sdk'
import { AccountImport, Logger, createRootLogger } from '@ironfish/sdk'
import TransportNodeHid from '@ledgerhq/hw-transport-node-hid'
import IronfishApp, {
IronfishKeys,
Expand Down Expand Up @@ -218,6 +218,7 @@ export class Ledger {
}

dkgRound3 = async (
index: number,
identity: string,
round1PublicPackagesStr: string[],
round2PublicPackagesStr: string[],
Expand Down Expand Up @@ -271,7 +272,7 @@ export class Ledger {

await this.tryInstruction(
this.app.dkgRound3Min(
0,
index,
participants,
round1FrostPackages,
round2FrostPackages,
Expand Down

0 comments on commit f40294e

Please sign in to comment.