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

adds wallet/multisig/importParticipant RPC #5383

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions ironfish/src/rpc/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ import type {
GetWorkersStatusRequest,
GetWorkersStatusResponse,
ImportAccountRequest,
ImportParticipantRequest,
ImportParticipantResponse,
ImportResponse,
IsValidPublicAddressRequest,
IsValidPublicAddressResponse,
Expand Down Expand Up @@ -275,6 +277,15 @@ export abstract class RpcClient {
).waitForEnd()
},

importParticipant: (
params: ImportParticipantRequest,
): Promise<RpcResponseEnded<ImportParticipantResponse>> => {
return this.request<ImportParticipantResponse>(
`${ApiNamespace.wallet}/multisig/importParticipant`,
params,
).waitForEnd()
},

getIdentity: (
params: GetIdentityRequest,
): Promise<RpcResponseEnded<GetIdentityResponse>> => {
Expand Down
108 changes: 108 additions & 0 deletions ironfish/src/rpc/routes/wallet/multisig/importParticipant.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* 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 { multisig } from '@ironfish/rust-nodejs'
import { Assert } from '../../../../assert'
import { createRouteTest } from '../../../../testUtilities/routeTest'

describe('Route wallet/multisig/importParticipant', () => {
const routeTest = createRouteTest()

it('should fail for an identity that exists', async () => {
const name = 'name'

const secret = multisig.ParticipantSecret.random()
const identity = secret.toIdentity()

await routeTest.wallet.walletDb.putMultisigIdentity(identity.serialize(), {
name,
secret: undefined,
})

await expect(
routeTest.client.wallet.multisig.importParticipant({
identity: identity.serialize().toString('hex'),
name: 'new-name',
secret: secret.serialize().toString('hex'),
}),
).rejects.toThrow(
expect.objectContaining({
message: expect.stringContaining(
`Multisig participant already exists for the identity ${identity
.serialize()
.toString('hex')}`,
),
status: 400,
}),
)
})
it('should fail for a participant name that exists', async () => {
const name = 'name'

const secret = multisig.ParticipantSecret.random()
const identity = secret.toIdentity()

await routeTest.wallet.walletDb.putMultisigIdentity(identity.serialize(), {
name,
secret: undefined,
})

const newSecret = multisig.ParticipantSecret.random()
const newIdentity = newSecret.toIdentity()

await expect(
routeTest.client.wallet.multisig.importParticipant({
identity: newIdentity.serialize().toString('hex'),
name,
secret: newSecret.serialize().toString('hex'),
}),
).rejects.toThrow(
expect.objectContaining({
message: expect.stringContaining(
`Multisig identity already exists with the name ${name}`,
),
status: 400,
}),
)
})

it('should fail for an account name that exists', async () => {
const name = 'existing-account'
await routeTest.client.wallet.createAccount({ name })

const secret = multisig.ParticipantSecret.random()
const identity = secret.toIdentity()

await expect(
routeTest.client.wallet.multisig.importParticipant({
identity: identity.serialize().toString('hex'),
name,
secret: secret.serialize().toString('hex'),
}),
).rejects.toThrow(
expect.objectContaining({
message: expect.stringContaining(`Account already exists with the name ${name}`),
status: 400,
}),
)
})

it('should import a new identity', async () => {
const name = 'identity'

const secret = multisig.ParticipantSecret.random()
const identity = secret.toIdentity()

await routeTest.client.wallet.multisig.importParticipant({
identity: identity.serialize().toString('hex'),
name,
secret: secret.serialize().toString('hex'),
})

const secretValue = await routeTest.node.wallet.walletDb.getMultisigIdentity(
identity.serialize(),
)
Assert.isNotUndefined(secretValue)
expect(secretValue.name).toEqual(name)
})
})
70 changes: 70 additions & 0 deletions ironfish/src/rpc/routes/wallet/multisig/importParticipant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* 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 * as yup from 'yup'
import {
DuplicateAccountNameError,
DuplicateIdentityError,
DuplicateIdentityNameError,
} from '../../../../wallet/errors'
import { ApiNamespace } from '../../namespaces'
import { routes } from '../../router'
import { AssertHasRpcContext } from '../../rpcContext'

export type ImportParticipantRequest = {
name: string
identity: string
secret?: string
}

export type ImportParticipantResponse = {
identity: string
}

export const ImportParticipantRequestSchema: yup.ObjectSchema<ImportParticipantRequest> = yup
.object({
name: yup.string().defined(),
identity: yup.string().defined(),
secret: yup.string().optional(),
})
.defined()

export const ImportParticipantResponseSchema: yup.ObjectSchema<ImportParticipantResponse> = yup
.object({
identity: yup.string().defined(),
})
.defined()

routes.register<typeof ImportParticipantRequestSchema, ImportParticipantResponse>(
`${ApiNamespace.wallet}/multisig/importParticipant`,
ImportParticipantRequestSchema,
async (request, context): Promise<void> => {
AssertHasRpcContext(request, context, 'wallet')

if (await context.wallet.walletDb.hasMultisigSecretName(request.data.name)) {
throw new DuplicateIdentityNameError(request.data.name)
}

if (
await context.wallet.walletDb.getMultisigIdentity(
Buffer.from(request.data.identity, 'hex'),
)
) {
throw new DuplicateIdentityError(request.data.identity)
}

if (context.wallet.getAccountByName(request.data.name)) {
throw new DuplicateAccountNameError(request.data.name)
}

await context.wallet.walletDb.putMultisigIdentity(
Buffer.from(request.data.identity, 'hex'),
{
name: request.data.name,
secret: request.data.secret ? Buffer.from(request.data.secret, 'hex') : undefined,
},
)

request.end({ identity: request.data.identity })
},
)
1 change: 1 addition & 0 deletions ironfish/src/rpc/routes/wallet/multisig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export * from './createParticipant'
export * from './getIdentity'
export * from './getIdentities'
export * from './getAccountIdentities'
export * from './importParticipant'
export * from './dkg'
9 changes: 9 additions & 0 deletions ironfish/src/wallet/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ export class DuplicateIdentityNameError extends Error {
}
}

export class DuplicateIdentityError extends Error {
name = this.constructor.name

constructor(identity: string) {
super()
this.message = `Multisig participant already exists for the identity ${identity}`
}
}

export class DuplicateSpendingKeyError extends Error {
name = this.constructor.name

Expand Down
15 changes: 14 additions & 1 deletion ironfish/src/wallet/walletdb/walletdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,19 @@ export class WalletDB {
await this.multisigIdentities.del(identity, tx)
}

async getMultisigIdentityByName(
name: string,
tx?: IDatabaseTransaction,
): Promise<Buffer | undefined> {
for await (const [identity, value] of this.multisigIdentities.getAllIter(tx)) {
if (value.name === name) {
return identity
}
}

return undefined
}

async getMultisigSecretByName(
name: string,
tx?: IDatabaseTransaction,
Expand All @@ -1470,7 +1483,7 @@ export class WalletDB {
}

async hasMultisigSecretName(name: string, tx?: IDatabaseTransaction): Promise<boolean> {
return (await this.getMultisigSecretByName(name, tx)) !== undefined
return (await this.getMultisigIdentityByName(name, tx)) !== undefined
}

async *getMultisigIdentities(
Expand Down