Skip to content

Commit

Permalink
Prefer ironfish_rust::participant::Identity over frost::Identifier
Browse files Browse the repository at this point in the history
`Identity` holds more information than `Identifier` and hence should be
the preferred data structure. An `Identity` can always be converted to
`Identifier` but the other way around is not possible.
  • Loading branch information
andreacorbellini committed Feb 7, 2024
1 parent ddf1e95 commit 062269e
Show file tree
Hide file tree
Showing 31 changed files with 164 additions and 165 deletions.
26 changes: 13 additions & 13 deletions ironfish-cli/src/commands/wallet/multisig/account/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IronfishCommand } from '../../../../command'
import { RemoteFlags } from '../../../../flags'

export class MultisigCreate extends IronfishCommand {
static description = `Create a set of multisig accounts from identifiers`
static description = `Create a set of multisig accounts from identities`
static hidden = true

static flags = {
Expand All @@ -16,9 +16,9 @@ export class MultisigCreate extends IronfishCommand {
char: 'n',
description: 'Name to use for the coordinator',
}),
identifier: Flags.string({
identity: Flags.string({
char: 'i',
description: 'Identifier of a participant',
description: 'Identity of a participant',
multiple: true,
}),
minSigners: Flags.integer({
Expand All @@ -35,18 +35,18 @@ export class MultisigCreate extends IronfishCommand {
async start(): Promise<void> {
const { flags } = await this.parse(MultisigCreate)

let identifiers = flags.identifier
if (!identifiers || identifiers.length < 2) {
const input = await CliUx.ux.prompt('Enter the identifiers separated by commas', {
let identities = flags.identity
if (!identities || identities.length < 2) {
const input = await CliUx.ux.prompt('Enter the identities separated by commas', {
required: true,
})
identifiers = input.split(',')
identities = input.split(',')

if (identifiers.length < 2) {
this.error('Minimum number of identifiers must be at least 2')
if (identities.length < 2) {
this.error('Minimum number of identities must be at least 2')
}
}
identifiers = identifiers.map((i) => i.trim())
identities = identities.map((i) => i.trim())

let minSigners = flags.minSigners
if (!minSigners) {
Expand All @@ -67,7 +67,7 @@ export class MultisigCreate extends IronfishCommand {

const response = await client.wallet.multisig.createTrustedDealerKeyPackage({
minSigners,
participants: identifiers.map((identifier) => ({ identifier })),
participants: identities.map((identity) => ({ identity })),
})

const chainResponse = await client.chain.getChainInfo()
Expand Down Expand Up @@ -110,7 +110,7 @@ export class MultisigCreate extends IronfishCommand {
for (const [i, keyPackage] of response.content.keyPackages.entries()) {
this.log('\n')
this.log(`Account ${i + 1}`)
this.log(`Identifier ${keyPackage.identifier}`)
this.log(`Identifier ${keyPackage.identity}`)
this.log('----------------')
const accountStr = encoder.encode({
name: `${name}-0`,
Expand All @@ -123,7 +123,7 @@ export class MultisigCreate extends IronfishCommand {
publicAddress: response.content.publicAddress,
proofAuthorizingKey: response.content.proofAuthorizingKey,
multisigKeys: {
identifier: keyPackage.identifier,
identity: keyPackage.identity,
keyPackage: keyPackage.keyPackage,
publicKeyPackage: response.content.publicKeyPackage,
},
Expand Down
7 changes: 0 additions & 7 deletions ironfish-cli/src/commands/wallet/multisig/identity/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,5 @@ export class MultisigIdentityCreate extends IronfishCommand {
const identity = secret.toIdentity()
this.log('Identity:')
this.log(identity.serialize().toString('hex'))

this.log()

// TODO: remove when other multisig CLI commands use identity as inputs
const identifier = identity.toFrostIdentifier()
this.log('Identifier:')
this.log(identifier)
}
}
11 changes: 5 additions & 6 deletions ironfish-rust-nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
/* auto-generated by NAPI-RS */

export function createSigningCommitment(keyPackage: string, seed: number): string
export function createSignatureShare(signingPackage: string, identifier: string, keyPackage: string, publicKeyRandomness: string, seed: number): string
export function splitSecret(coordinatorSaplingKey: string, minSigners: number, identifiers: Array<string>): TrustedDealerKeyPackages
export function createSignatureShare(signingPackage: string, identity: string, keyPackage: string, publicKeyRandomness: string, seed: number): string
export function splitSecret(coordinatorSaplingKey: string, minSigners: number, identities: Array<string>): TrustedDealerKeyPackages
export function contribute(inputPath: string, outputPath: string, seed?: string | undefined | null): Promise<string>
export function verifyTransform(paramsPath: string, newParamsPath: string): Promise<string>
export const KEY_LENGTH: number
Expand Down Expand Up @@ -52,8 +52,8 @@ export const TRANSACTION_EXPIRATION_LENGTH: number
export const TRANSACTION_FEE_LENGTH: number
export const LATEST_TRANSACTION_VERSION: number
export function verifyTransactions(serializedTransactions: Array<Buffer>): boolean
export interface IdentiferKeyPackage {
identifier: string
export interface IdentityKeyPackage {
identity: string
keyPackage: string
}
export interface TrustedDealerKeyPackages {
Expand All @@ -63,7 +63,7 @@ export interface TrustedDealerKeyPackages {
incomingViewKey: string
outgoingViewKey: string
publicAddress: string
keyPackages: Array<IdentiferKeyPackage>
keyPackages: Array<IdentityKeyPackage>
publicKeyPackage: string
}
export const enum LanguageCode {
Expand Down Expand Up @@ -104,7 +104,6 @@ export class ParticipantSecret {
export class ParticipantIdentity {
constructor(jsBytes: Buffer)
serialize(): Buffer
toFrostIdentifier(): string
}
export class BoxKeyPair {
constructor()
Expand Down
39 changes: 17 additions & 22 deletions ironfish-rust-nodejs/src/frost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::{
structs::{IdentiferKeyPackage, TrustedDealerKeyPackages},
structs::{IdentityKeyPackage, TrustedDealerKeyPackages},
to_napi_err,
};
use ironfish::{
frost::{keys::KeyPackage, round2::Randomizer, Identifier, SigningPackage},
frost::{keys::KeyPackage, round2::Randomizer, SigningPackage},
frost_utils::{
signature_share::create_signature_share as create_signature_share_rust,
signing_commitment::{
Expand Down Expand Up @@ -42,13 +42,14 @@ pub fn create_signing_commitment(key_package: String, seed: u32) -> Result<Strin
#[napi]
pub fn create_signature_share(
signing_package: String,
identifier: String,
identity: String,
key_package: String,
public_key_randomness: String,
seed: u32,
) -> Result<String> {
let identifier = Identifier::deserialize(&hex_to_bytes(&identifier).map_err(to_napi_err)?)
.map_err(to_napi_err)?;
let identity =
Identity::deserialize_from(&hex_to_vec_bytes(&identity).map_err(to_napi_err)?[..])
.map_err(to_napi_err)?;
let key_package =
KeyPackage::deserialize(&hex_to_vec_bytes(&key_package).map_err(to_napi_err)?[..])
.map_err(to_napi_err)?;
Expand All @@ -61,7 +62,7 @@ pub fn create_signature_share(

let signature_share = create_signature_share_rust(
signing_package,
identifier,
&identity,
key_package,
randomizer,
seed as u64,
Expand Down Expand Up @@ -135,39 +136,33 @@ impl ParticipantIdentity {

Ok(Buffer::from(vec))
}

#[napi]
pub fn to_frost_identifier(&self) -> String {
let identifier: Identifier = self.identity.to_frost_identifier();

bytes_to_hex(&identifier.serialize())
}
}

#[napi]
pub fn split_secret(
coordinator_sapling_key: String,
min_signers: u16,
identifiers: Vec<String>,
identities: Vec<String>,
) -> Result<TrustedDealerKeyPackages> {
let coordinator_key =
SaplingKey::new(hex_to_bytes(&coordinator_sapling_key).map_err(to_napi_err)?)
.map_err(to_napi_err)?;

let mut converted = Vec::new();
let mut deserialized_identities = Vec::new();

for identifier in &identifiers {
let bytes = hex_to_bytes(identifier).map_err(to_napi_err)?;
let deserialized = Identifier::deserialize(&bytes).map_err(to_napi_err)?;
converted.push(deserialized);
for identity in &identities {
let bytes = hex_to_vec_bytes(identity).map_err(to_napi_err)?;
let frost_id = Identity::deserialize_from(&bytes[..]).map_err(to_napi_err)?;
deserialized_identities.push(frost_id);
}

let t = split_spender_key(&coordinator_key, min_signers, converted).map_err(to_napi_err)?;
let t = split_spender_key(&coordinator_key, min_signers, deserialized_identities)
.map_err(to_napi_err)?;

let mut key_packages_serialized = Vec::new();
for (k, v) in t.key_packages.iter() {
key_packages_serialized.push(IdentiferKeyPackage {
identifier: bytes_to_hex(&k.serialize()),
key_packages_serialized.push(IdentityKeyPackage {
identity: bytes_to_hex(&k.serialize()),
key_package: bytes_to_hex(&v.serialize().map_err(to_napi_err)?),
});
}
Expand Down
6 changes: 3 additions & 3 deletions ironfish-rust-nodejs/src/structs/key_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use napi_derive::napi;

#[napi(object)]
pub struct IdentiferKeyPackage {
pub identifier: String,
pub struct IdentityKeyPackage {
pub identity: String,
pub key_package: String,
}
#[napi(object)]
Expand All @@ -18,6 +18,6 @@ pub struct TrustedDealerKeyPackages {
pub incoming_view_key: String,
pub outgoing_view_key: String,
pub public_address: String,
pub key_packages: Vec<IdentiferKeyPackage>,
pub key_packages: Vec<IdentityKeyPackage>,
pub public_key_package: String,
}
4 changes: 3 additions & 1 deletion ironfish-rust/src/frost_utils/signature_share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use ironfish_frost::frost::{
round2::{Randomizer, SignatureShare as FrostSignatureShare},
Identifier, SigningPackage,
};
use ironfish_frost::participant::Identity;
use rand::{rngs::StdRng, SeedableRng};

use crate::errors::{IronfishError, IronfishErrorKind};
Expand Down Expand Up @@ -50,7 +51,7 @@ impl SignatureShare {
// Wrapper around frost::round2::sign that provides a seedable rng from u64
pub fn create_signature_share(
signing_package: SigningPackage,
identifier: Identifier,
identity: &Identity,
key_package: KeyPackage,
randomizer: Randomizer,
seed: u64,
Expand All @@ -60,6 +61,7 @@ pub fn create_signature_share(
let signature_share =
frost::round2::sign(&signing_package, &signer_nonces, &key_package, randomizer)
.map_err(|_| IronfishError::new(IronfishErrorKind::RoundTwoSigningFailure))?;
let identifier = identity.to_frost_identifier();
Ok(SignatureShare {
identifier,
signature_share,
Expand Down
6 changes: 3 additions & 3 deletions ironfish-rust/src/frost_utils/signing_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn create_signing_commitment(
#[cfg(test)]
mod test {
use crate::frost_utils::split_secret::{split_secret, SecretShareConfig};
use crate::test_util::create_identifiers;
use crate::test_util::create_multisig_identities;
use ff::Field;
use jubjub::Fr;
use rand::thread_rng;
Expand All @@ -83,11 +83,11 @@ mod test {
let seed = 100;
let key = Fr::random(&mut rand::thread_rng());

let identifiers = create_identifiers(10);
let identities = create_multisig_identities(10);

let key_packages = split_secret(
&SecretShareConfig {
identifiers,
identities,
min_signers: 2,
secret: key.to_bytes().to_vec(),
},
Expand Down
Loading

0 comments on commit 062269e

Please sign in to comment.