-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds wallet/multisig/importParticipant RPC
provides an RPC route for importing an existing identity and optional secret into the walletdb useful for creating a record of the identity retrieved from a Ledger device participating in DKG to generate a multisig account throws errors if the identity already exists in the walletdb or if the name is already in use for another identity or account
- Loading branch information
Showing
6 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
ironfish/src/rpc/routes/wallet/multisig/importParticipant.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
secret: secret.serialize().toString('hex'), | ||
}), | ||
).rejects.toThrow( | ||
expect.objectContaining({ | ||
message: expect.stringContaining( | ||
`Multisig participant already exists for 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
70
ironfish/src/rpc/routes/wallet/multisig/importParticipant.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters