-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from casper-ecosystem/sign-msg
SignMessage implemented
- Loading branch information
Showing
5 changed files
with
87 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
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,47 @@ | ||
import * as nacl from 'tweetnacl-ts'; | ||
import * as secp256k1 from 'ethereum-cryptography/secp256k1'; | ||
import { sha256 } from 'ethereum-cryptography/sha256'; | ||
|
||
import { CLPublicKey } from './CLValue/'; | ||
import { AsymmetricKey } from './Keys'; | ||
|
||
/** | ||
* Method for signing string message. | ||
* @param key AsymmetricKey used to sign the message | ||
* @param message Message that will be signed | ||
* @return Uint8Array Signature in byte format | ||
*/ | ||
export const signMessage = ( | ||
key: AsymmetricKey, | ||
message: string | ||
): Uint8Array => { | ||
const messageWithHeader = Buffer.from(`Casper Message:\n${message}`); | ||
return key.sign(messageWithHeader); | ||
}; | ||
|
||
/** | ||
* Method to verify signature | ||
* @param key Public key of private key used to signed. | ||
* @param message Message that was signed | ||
* @param signature Signature in byte format | ||
* @return boolean Verification result | ||
*/ | ||
export const verifyMessageSignature = ( | ||
key: CLPublicKey, | ||
message: string, | ||
signature: Uint8Array | ||
): boolean => { | ||
const messageWithHeader = Buffer.from(`Casper Message:\n${message}`); | ||
if (key.isEd25519()) { | ||
return nacl.sign_detached_verify(messageWithHeader, signature, key.value()); | ||
} | ||
if (key.isSecp256K1()) { | ||
return secp256k1.ecdsaVerify( | ||
signature, | ||
sha256(messageWithHeader), | ||
key.value() | ||
); | ||
} | ||
|
||
throw new Error('Unsupported algorithm.'); | ||
}; |
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,31 @@ | ||
import { expect } from 'chai'; | ||
import { Ed25519, Secp256K1 } from '../../src/lib/Keys'; | ||
import { signMessage, verifyMessageSignature } from '../../src/index'; | ||
|
||
describe('SignedMessage', () => { | ||
it('Should generate proper signed message and validate it (Ed25519)', () => { | ||
const signKeyPair = Ed25519.new(); | ||
const exampleMessage = "Hello World!"; | ||
const wrongMessage = "!Hello World"; | ||
|
||
const signature = signMessage(signKeyPair, exampleMessage); | ||
const valid = verifyMessageSignature(signKeyPair.publicKey, exampleMessage, signature); | ||
const invalid = verifyMessageSignature(signKeyPair.publicKey, wrongMessage, signature); | ||
|
||
expect(valid).to.be.eq(true); | ||
expect(invalid).to.be.eq(false); | ||
}); | ||
|
||
it('Should generate proper signed message and validate it (Secp256K1)', () => { | ||
const signKeyPair = Secp256K1.new(); | ||
const exampleMessage = "Hello World!"; | ||
const wrongMessage = "!Hello World"; | ||
|
||
const signature = signMessage(signKeyPair, exampleMessage); | ||
const valid = verifyMessageSignature(signKeyPair.publicKey, exampleMessage, signature); | ||
const invalid = verifyMessageSignature(signKeyPair.publicKey, wrongMessage, signature); | ||
|
||
expect(valid).to.be.eq(true); | ||
expect(invalid).to.be.eq(false); | ||
}); | ||
}); |