-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
wallet/multisig/dkg/round1
RPC
- Loading branch information
Showing
7 changed files
with
284 additions
and
3 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* 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/. */ | ||
export * from './round1' |
114 changes: 114 additions & 0 deletions
114
ironfish/src/rpc/routes/wallet/multisig/dkg/round1.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,114 @@ | ||
/* 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 { createRouteTest } from '../../../../../testUtilities/routeTest' | ||
|
||
describe('Route multisig/dkg/round1', () => { | ||
const routeTest = createRouteTest() | ||
|
||
it('should create round 1 packages', async () => { | ||
const secretName = 'name' | ||
await routeTest.client.wallet.multisig.createParticipant({ name: secretName }) | ||
|
||
const identity = (await routeTest.client.wallet.multisig.getIdentity({ name: secretName })) | ||
.content.identity | ||
const otherParticipants = Array.from({ length: 2 }, () => ({ | ||
identity: multisig.ParticipantSecret.random().toIdentity().serialize().toString('hex'), | ||
})) | ||
const participants = [{ identity }, ...otherParticipants] | ||
|
||
const request = { secretName, minSigners: 2, participants } | ||
const response = await routeTest.client.wallet.multisig.dkg.round1(request) | ||
|
||
expect(response.content).toMatchObject({ | ||
encryptedSecretPackage: expect.any(String), | ||
publicPackage: expect.any(String), | ||
}) | ||
}) | ||
|
||
it('should fail if the named secret does not exist', async () => { | ||
const secretName = 'name' | ||
await routeTest.client.wallet.multisig.createParticipant({ name: secretName }) | ||
|
||
const identity = (await routeTest.client.wallet.multisig.getIdentity({ name: secretName })) | ||
.content.identity | ||
const otherParticipants = Array.from({ length: 2 }, () => ({ | ||
identity: multisig.ParticipantSecret.random().toIdentity().serialize().toString('hex'), | ||
})) | ||
const participants = [{ identity }, ...otherParticipants] | ||
|
||
const request = { secretName: 'otherName', minSigners: 2, participants } | ||
|
||
await expect(routeTest.client.wallet.multisig.dkg.round1(request)).rejects.toThrow( | ||
expect.objectContaining({ | ||
message: expect.stringContaining("Multisig secret with name 'otherName' not found"), | ||
status: 400, | ||
}), | ||
) | ||
}) | ||
|
||
it('should fail if the named identity is not part of the participants', async () => { | ||
const secretName = 'name' | ||
await routeTest.client.wallet.multisig.createParticipant({ name: secretName }) | ||
|
||
const participants = Array.from({ length: 3 }, () => ({ | ||
identity: multisig.ParticipantSecret.random().toIdentity().serialize().toString('hex'), | ||
})) | ||
|
||
const request = { secretName, minSigners: 2, participants } | ||
|
||
await expect(routeTest.client.wallet.multisig.dkg.round1(request)).rejects.toThrow( | ||
expect.objectContaining({ | ||
message: expect.stringContaining( | ||
"List of participant identities must include the identity for 'name'", | ||
), | ||
status: 400, | ||
}), | ||
) | ||
}) | ||
|
||
it('should fail if minSigners is too low', async () => { | ||
const secretName = 'name' | ||
await routeTest.client.wallet.multisig.createParticipant({ name: secretName }) | ||
|
||
const identity = (await routeTest.client.wallet.multisig.getIdentity({ name: secretName })) | ||
.content.identity | ||
const otherParticipants = Array.from({ length: 2 }, () => ({ | ||
identity: multisig.ParticipantSecret.random().toIdentity().serialize().toString('hex'), | ||
})) | ||
const participants = [{ identity }, ...otherParticipants] | ||
|
||
const request = { secretName, minSigners: 1, participants } | ||
|
||
await expect(routeTest.client.wallet.multisig.dkg.round1(request)).rejects.toThrow( | ||
expect.objectContaining({ | ||
message: expect.stringContaining('minSigners must be 2 or greater, got 1'), | ||
status: 400, | ||
}), | ||
) | ||
}) | ||
|
||
it('should fail if minSigners exceeds the number of participants', async () => { | ||
const secretName = 'name' | ||
await routeTest.client.wallet.multisig.createParticipant({ name: secretName }) | ||
|
||
const identity = (await routeTest.client.wallet.multisig.getIdentity({ name: secretName })) | ||
.content.identity | ||
const otherParticipants = Array.from({ length: 2 }, () => ({ | ||
identity: multisig.ParticipantSecret.random().toIdentity().serialize().toString('hex'), | ||
})) | ||
const participants = [{ identity }, ...otherParticipants] | ||
|
||
const request = { secretName, minSigners: 4, participants } | ||
|
||
await expect(routeTest.client.wallet.multisig.dkg.round1(request)).rejects.toThrow( | ||
expect.objectContaining({ | ||
message: expect.stringContaining( | ||
'minSigners (4) exceeds the number of participants (3)', | ||
), | ||
status: 400, | ||
}), | ||
) | ||
}) | ||
}) |
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,91 @@ | ||
/* 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 * as yup from 'yup' | ||
import { RPC_ERROR_CODES, RpcValidationError } from '../../../../adapters' | ||
import { ApiNamespace } from '../../../namespaces' | ||
import { routes } from '../../../router' | ||
import { AssertHasRpcContext } from '../../../rpcContext' | ||
|
||
export type DkgRound1Request = { | ||
secretName: string | ||
minSigners: number | ||
participants: Array<{ identity: string }> | ||
} | ||
|
||
export type DkgRound1Response = { | ||
encryptedSecretPackage: string | ||
publicPackage: string | ||
} | ||
|
||
export const DkgRound1RequestSchema: yup.ObjectSchema<DkgRound1Request> = yup | ||
.object({ | ||
secretName: yup.string().defined(), | ||
minSigners: yup.number().defined(), | ||
participants: yup | ||
.array() | ||
.of(yup.object({ identity: yup.string().defined() }).defined()) | ||
.defined(), | ||
}) | ||
.defined() | ||
|
||
export const DkgRound1ResponseSchema: yup.ObjectSchema<DkgRound1Response> = yup | ||
.object({ | ||
encryptedSecretPackage: yup.string().defined(), | ||
publicPackage: yup.string().defined(), | ||
}) | ||
.defined() | ||
|
||
routes.register<typeof DkgRound1RequestSchema, DkgRound1Response>( | ||
`${ApiNamespace.wallet}/multisig/dkg/round1`, | ||
DkgRound1RequestSchema, | ||
async (request, node): Promise<void> => { | ||
AssertHasRpcContext(request, node, 'wallet') | ||
|
||
const { secretName, minSigners, participants } = request.data | ||
const multisigSecret = await node.wallet.walletDb.getMultisigSecretByName(secretName) | ||
|
||
if (!multisigSecret) { | ||
throw new RpcValidationError( | ||
`Multisig secret with name '${secretName}' not found`, | ||
400, | ||
RPC_ERROR_CODES.MULTISIG_SECRET_NOT_FOUND, | ||
) | ||
} | ||
|
||
const participantIdentities = participants.map((p) => p.identity) | ||
const selfIdentity = new multisig.ParticipantSecret(multisigSecret.secret) | ||
.toIdentity() | ||
.serialize() | ||
.toString('hex') | ||
|
||
if (!participantIdentities.includes(selfIdentity)) { | ||
throw new RpcValidationError( | ||
`List of participant identities must include the identity for '${secretName}' (${selfIdentity})`, | ||
400, | ||
RPC_ERROR_CODES.VALIDATION, | ||
) | ||
} | ||
|
||
if (minSigners < 2) { | ||
throw new RpcValidationError( | ||
`minSigners must be 2 or greater, got ${minSigners}`, | ||
400, | ||
RPC_ERROR_CODES.VALIDATION, | ||
) | ||
} | ||
|
||
if (minSigners > participantIdentities.length) { | ||
throw new RpcValidationError( | ||
`minSigners (${minSigners}) exceeds the number of participants (${participantIdentities.length})`, | ||
400, | ||
RPC_ERROR_CODES.VALIDATION, | ||
) | ||
} | ||
|
||
const packages = multisig.dkgRound1(selfIdentity, minSigners, participantIdentities) | ||
|
||
request.end(packages) | ||
}, | ||
) |
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