-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jellyfish-api-core): add verifyMessage and signMessageWithPrivKe…
…y RPCs (#1905) <!-- Thanks for sending a pull request! --> #### What this PR does / why we need it: Adds verifyMessage and signMessageWithPrivKey RPCs and tests. #### Which issue(s) does this PR fixes?: <!-- (Optional) Automatically closes linked issue when PR is merged. Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`. --> Fixes part of #48
- Loading branch information
Showing
4 changed files
with
149 additions
and
0 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
36 changes: 36 additions & 0 deletions
36
packages/jellyfish-api-core/__tests__/category/misc/signMessageWithPrivKey.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,36 @@ | ||
import { MasterNodeRegTestContainer } from '@defichain/testcontainers' | ||
import { ContainerAdapterClient } from '../../container_adapter_client' | ||
import { RpcApiError } from '@defichain/jellyfish-api-core' | ||
|
||
describe('Verify message', () => { | ||
const container = new MasterNodeRegTestContainer() | ||
const client = new ContainerAdapterClient(container) | ||
|
||
beforeAll(async () => { | ||
await container.start() | ||
}) | ||
|
||
afterAll(async () => { | ||
await container.stop() | ||
}) | ||
|
||
it('should throw error if invalid private key', async () => { | ||
const privkey = await client.wallet.dumpPrivKey(await client.wallet.getNewAddress()) | ||
const promise = client.misc.signMessageWithPrivKey(privkey.substr(1, 7), 'test') | ||
|
||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toMatchObject({ | ||
payload: { | ||
code: -5, | ||
message: 'Invalid private key', | ||
method: 'signmessagewithprivkey' | ||
} | ||
}) | ||
}) | ||
|
||
it('should sign with private key', async () => { | ||
const privkey = await client.wallet.dumpPrivKey(await client.wallet.getNewAddress()) | ||
const promise = client.misc.signMessageWithPrivKey(privkey, 'test') | ||
await expect(promise).toBeTruthy() | ||
}) | ||
}) |
70 changes: 70 additions & 0 deletions
70
packages/jellyfish-api-core/__tests__/category/misc/verifyMessage.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,70 @@ | ||
import { MasterNodeRegTestContainer } from '@defichain/testcontainers' | ||
import { ContainerAdapterClient } from '../../container_adapter_client' | ||
import { RegTestFoundationKeys } from '@defichain/jellyfish-network' | ||
import { RpcApiError } from '@defichain/jellyfish-api-core' | ||
|
||
describe('Verify message', () => { | ||
const container = new MasterNodeRegTestContainer() | ||
const client = new ContainerAdapterClient(container) | ||
const message = 'test' | ||
|
||
beforeAll(async () => { | ||
await container.start() | ||
}) | ||
|
||
afterAll(async () => { | ||
await container.stop() | ||
}) | ||
|
||
it('should throw error if invalid address', async () => { | ||
const promise = client.misc.verifyMessage('test', 'ICqlzHuredAz6XN7bVsB09/FGtGbRX+nUv+E9qz44rQ8DRi/zHpDGuMs2U6EtnGapv7r1V7cIdJ2ui9TMaaCNvA=', message) | ||
|
||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toMatchObject({ | ||
payload: { | ||
code: -3, | ||
message: 'Invalid address', | ||
method: 'verifymessage' | ||
} | ||
}) | ||
}) | ||
|
||
it('should throw error if invalid signature', async () => { | ||
const promise = client.misc.verifyMessage('mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU', 'ICqlzHuredAz6XN7bVsB09/FGtGbRX+nUv+E9qz44rQ8DRi/zHpDGuMs2U6EtnGapv7r1V7cIdJ2ui9TMaaCNvA', message) | ||
|
||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toMatchObject({ | ||
payload: { | ||
code: -5, | ||
message: 'Malformed base64 encoding', | ||
method: 'verifymessage' | ||
} | ||
}) | ||
}) | ||
|
||
it('should fail if incorrect keypair is used', async () => { | ||
const keyPair = RegTestFoundationKeys[0].owner | ||
const otherKeyPair = RegTestFoundationKeys[1].owner | ||
|
||
const signedString = await client.misc.signMessageWithPrivKey(keyPair.privKey, message) | ||
|
||
const isCorrect = await client.misc.verifyMessage(otherKeyPair.address, signedString, message) | ||
expect(isCorrect).toStrictEqual(false) | ||
}) | ||
|
||
it('should fail to verify message if incorrect message', async () => { | ||
const keyPair = RegTestFoundationKeys[0].owner | ||
const signedString = await client.misc.signMessageWithPrivKey(keyPair.privKey, message) | ||
|
||
const isCorrect = await client.misc.verifyMessage(keyPair.address, signedString, 'test1') | ||
expect(isCorrect).toStrictEqual(false) | ||
}) | ||
|
||
it('should verify message', async () => { | ||
const keyPair = RegTestFoundationKeys[0].owner | ||
const signedString = await client.misc.signMessageWithPrivKey(keyPair.privKey, message) | ||
|
||
const isCorrect = await client.misc.verifyMessage(keyPair.address, signedString, message) | ||
expect(isCorrect).toStrictEqual(true) | ||
}) | ||
}) |
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