-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sign, verify and block creation functions
- Loading branch information
1 parent
44fa7c8
commit ff4a71f
Showing
12 changed files
with
448 additions
and
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
### App ### | ||
|
||
/dist/ | ||
/native.js | ||
/tmp/* | ||
!/tmp/.gitkeep | ||
|
||
### Node ### | ||
|
||
|
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 |
---|---|---|
@@ -1,20 +1,95 @@ | ||
/* eslint-env jest */ | ||
|
||
const nano = require('../dist/nanocurrency.cjs') | ||
const { | ||
INVALID_HASHES, | ||
INVALID_SECRET_KEYS, | ||
INVALID_PUBLIC_KEYS, | ||
INVALID_SIGNATURES | ||
} = require('./common/data') | ||
|
||
const VALID_HASH = '7f7122e843b27524f4f1d6bd14aefd1c8f01d36ae8653d37417533c0d4bc2be6' | ||
const VALID_SECRET_KEY = '23b5e95b4c4325ed5af109bfe4acde782dbab0163591d9052963723ae8e72a09' | ||
const VALID_PUBLIC_KEY = '4d312f604f638adf19afac6308ecbbc5881e1b6cd6f53d382775c686bca7535b' | ||
const VALID_SIGNATURE = '4ae39add5ee6d53c81ab8c0281787d6f81b620acca37c24dd4bfdcc85df45db65e86234b2367155382951d52b57d7dc97284b1fc9914db07d5735bd307435300' | ||
const SECRET_KEY = '0000000000000000000000000000000000000000000000000000000000000001' | ||
const PUBLIC_KEY = 'c969ec348895a49e21824e10e6b829edea50ccc26a83ce8986a3b95d12576058' | ||
const HASH = 'f47b23107e5f34b2ce06f562b5c435df72a533251cb414c51b2b62a8f63a00e4' | ||
const SIGNATURE = '5974324f8cc42da56f62fc212a17886bdcb18de363d04da84eedc99cb4a33919d14a2cf9de9d534faa6d0b91d01f0622205d898293525e692586c84f2dcf9208' | ||
const INVALID_SIGNATURE = '8029fcd2f48c685296e525392898d5022260f10d19b0d6aaf435d9ed9fc2a41d91933a4bc99cdee48ad40d363ed81bcdb68871a212cf26f65ad24cc8f4234795' | ||
|
||
beforeAll(nano.init) | ||
|
||
test('signs correctly', () => { | ||
expect( | ||
nano.computeSignature( | ||
VALID_HASH, | ||
VALID_SECRET_KEY, | ||
VALID_PUBLIC_KEY | ||
) | ||
).toBe(VALID_SIGNATURE) | ||
describe('sign', () => { | ||
test('signs correctly', () => { | ||
expect( | ||
nano.signBlock( | ||
HASH, | ||
SECRET_KEY | ||
) | ||
).toBe(SIGNATURE) | ||
}) | ||
|
||
test('throws with invalid hashes', () => { | ||
expect.assertions(INVALID_HASHES.length) | ||
for (let invalidHash of INVALID_HASHES) { | ||
expect( | ||
() => nano.signBlock(invalidHash, SECRET_KEY) | ||
).toThrowError('Hash is not valid') | ||
} | ||
}) | ||
|
||
test('throws with invalid secret keys', () => { | ||
expect.assertions(INVALID_SECRET_KEYS.length) | ||
for (let invalidSecretKey of INVALID_SECRET_KEYS) { | ||
expect( | ||
() => nano.signBlock(HASH, invalidSecretKey) | ||
).toThrowError('Secret key is not valid') | ||
} | ||
}) | ||
}) | ||
|
||
describe('verify', () => { | ||
test('validates correct signature', () => { | ||
expect( | ||
nano.verifyBlock( | ||
HASH, | ||
SIGNATURE, | ||
PUBLIC_KEY | ||
) | ||
).toBe(true) | ||
}) | ||
|
||
test('does not validate incorrect signature', () => { | ||
expect( | ||
nano.verifyBlock( | ||
HASH, | ||
INVALID_SIGNATURE, | ||
PUBLIC_KEY | ||
) | ||
).toBe(false) | ||
}) | ||
|
||
test('throws with invalid hashes', () => { | ||
expect.assertions(INVALID_HASHES.length) | ||
for (let invalidHash of INVALID_HASHES) { | ||
expect( | ||
() => nano.verifyBlock(invalidHash, SIGNATURE, PUBLIC_KEY) | ||
).toThrowError('Hash is not valid') | ||
} | ||
}) | ||
|
||
test('throws with invalid signatures', () => { | ||
expect.assertions(INVALID_SIGNATURES.length) | ||
for (let invalidSignature of INVALID_SIGNATURES) { | ||
expect( | ||
() => nano.verifyBlock(HASH, invalidSignature, PUBLIC_KEY) | ||
).toThrowError('Signature is not valid') | ||
} | ||
}) | ||
|
||
test('throws with invalid public keys', () => { | ||
expect.assertions(INVALID_PUBLIC_KEYS.length) | ||
for (let invalidPublicKey of INVALID_PUBLIC_KEYS) { | ||
expect( | ||
() => nano.verifyBlock(HASH, SIGNATURE, invalidPublicKey) | ||
).toThrowError('Public key is not valid') | ||
} | ||
}) | ||
}) |
Oops, something went wrong.