-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wallet/adSignature: RPC to add signature to a transaction. (#5097)
This RPC is used to add signature to a transaction. It takes a transaction and a signature as input and returns a signed transaction.
- Loading branch information
Showing
5 changed files
with
134 additions
and
2 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
32 changes: 32 additions & 0 deletions
32
ironfish/src/rpc/routes/wallet/__fixtures__/addSignature.test.ts.fixture
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,32 @@ | ||
{ | ||
"Route wallet/addSignature should return error if signature is not a valid hex": [ | ||
{ | ||
"value": { | ||
"version": 4, | ||
"id": "0e9f1f07-ee9d-4746-a4e8-131d89d71345", | ||
"name": "addSignatureAccount2", | ||
"spendingKey": "21557df84c37fa55363c77ffe99a6d01a1c1938f06b805b34a2d2f3442016d3c", | ||
"viewKey": "4d8f669c3667195906dcc4e636983d653e6042267fffb3b7ed1a00e8aeacf7011382ac8e43e6f446f3751dca9640eb7d684b88c181feec8f1399863b0e389c95", | ||
"incomingViewKey": "9f13a57239aba9daade572166257ae494807e6436a9eaaf5bf312855e225d906", | ||
"outgoingViewKey": "5c156963119e3fbdccd84d08622eae4dbbc36ada56e30682188a338fe9be0ed6", | ||
"publicAddress": "608569ba934ef7bb6c6bc99e653b7ab18eb17bf02602c8fc489c3286d1065f2c", | ||
"createdAt": { | ||
"hash": { | ||
"type": "Buffer", | ||
"data": "base64:R5HXrp+X3xAO8VWOhHctagm0N2I4goP3XG8goyqIqoY=" | ||
}, | ||
"sequence": 1 | ||
}, | ||
"scanningEnabled": true, | ||
"proofAuthorizingKey": "0b3bcdc835ee3709c9a30cad80c36d0d4f5d5712349d6bbfd34da9ed405bbc02" | ||
}, | ||
"head": { | ||
"hash": { | ||
"type": "Buffer", | ||
"data": "base64:R5HXrp+X3xAO8VWOhHctagm0N2I4goP3XG8goyqIqoY=" | ||
}, | ||
"sequence": 1 | ||
} | ||
} | ||
] | ||
} |
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,36 @@ | ||
/* 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 { RawTransactionSerde } from '../../../primitives' | ||
import { useAccountFixture } from '../../../testUtilities' | ||
import { createRawTransaction } from '../../../testUtilities/helpers/transaction' | ||
import { createRouteTest } from '../../../testUtilities/routeTest' | ||
|
||
describe('Route wallet/addSignature', () => { | ||
const routeTest = createRouteTest(true) | ||
|
||
it('should return error if signature is not a valid hex', async () => { | ||
const account = await useAccountFixture(routeTest.node.wallet, 'addSignatureAccount') | ||
const rawTransaction = await createRawTransaction({ | ||
wallet: routeTest.node.wallet, | ||
from: account, | ||
}) | ||
|
||
const response = await routeTest.client.wallet.buildTransaction({ | ||
rawTransaction: RawTransactionSerde.serialize(rawTransaction).toString('hex'), | ||
account: account.name, | ||
}) | ||
|
||
expect(response.status).toBe(200) | ||
expect(response.content.unsignedTransaction).toBeDefined() | ||
|
||
const invalidSignature = 'invalid' | ||
|
||
await expect( | ||
routeTest.client.wallet.addSignature({ | ||
unsignedTransaction: response.content.unsignedTransaction, | ||
signature: invalidSignature, | ||
}), | ||
).rejects.toThrow('Invalid signature length') | ||
}) | ||
}) |
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,53 @@ | ||
/* 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 { UnsignedTransaction } from '@ironfish/rust-nodejs' | ||
import * as yup from 'yup' | ||
import { ApiNamespace } from '../namespaces' | ||
import { routes } from '../router' | ||
import { AssertHasRpcContext } from '../rpcContext' | ||
|
||
export type AddSignatureRequest = { | ||
unsignedTransaction: string | ||
signature: string | ||
} | ||
|
||
export type AddSignatureResponse = { | ||
transaction: string | ||
} | ||
|
||
export const AddSignatureRequestSchema: yup.ObjectSchema<AddSignatureRequest> = yup | ||
.object({ | ||
unsignedTransaction: yup.string().defined(), | ||
signature: yup.string().defined(), | ||
}) | ||
.defined() | ||
|
||
export const AddSignatureResponseSchema: yup.ObjectSchema<AddSignatureResponse> = yup | ||
.object({ | ||
transaction: yup.string().defined(), | ||
}) | ||
.defined() | ||
|
||
routes.register<typeof AddSignatureRequestSchema, AddSignatureResponse>( | ||
`${ApiNamespace.wallet}/addSignature`, | ||
AddSignatureRequestSchema, | ||
(request, node): void => { | ||
AssertHasRpcContext(request, node, 'wallet') | ||
const unsignedTransaction = new UnsignedTransaction( | ||
Buffer.from(request.data.unsignedTransaction, 'hex'), | ||
) | ||
|
||
const buffer = Buffer.from(request.data.signature, 'hex') | ||
|
||
if (buffer.length !== 64) { | ||
throw new Error('Invalid signature length') | ||
} | ||
|
||
const serialized = unsignedTransaction.addSignature(buffer) | ||
|
||
request.end({ | ||
transaction: serialized.toString('hex'), | ||
}) | ||
}, | ||
) |
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