Skip to content

Commit

Permalink
fix: add more tests and fix state block link field if this is an address
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinroger committed May 6, 2018
1 parent 6416e8a commit 3d557b7
Show file tree
Hide file tree
Showing 11 changed files with 605 additions and 661 deletions.
922 changes: 432 additions & 490 deletions __tests__/block.js

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions __tests__/browser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-env jest */
/* global NanoCurrency */

const fs = require('fs')
const {promisify} = require('util')
const { promisify } = require('util')
const puppeteer = require('puppeteer')

const readFile = promisify(fs.readFile)
Expand All @@ -12,7 +13,7 @@ let umdScript = null
beforeAll(async () => {
browser = await puppeteer.launch()
page = await browser.newPage()
umdScript = await readFile('./dist/nanocurrency.umd.js' ,'utf8')
umdScript = await readFile('./dist/nanocurrency.umd.js', 'utf8')
})

afterAll(() => browser.close())
Expand All @@ -36,9 +37,13 @@ describe('browser', () => {

// webassembly test
result = await page.evaluate(async function () {
const publicKey = NanoCurrency.derivePublicKey('23B5E95B4C4325ED5AF109BFE4ACDE782DBAB0163591D9052963723AE8E72A09')
const publicKey = NanoCurrency.derivePublicKey(
'23B5E95B4C4325ED5AF109BFE4ACDE782DBAB0163591D9052963723AE8E72A09'
)
return publicKey
})
expect(result).toBe('4D312F604F638ADF19AFAC6308ECBBC5881E1B6CD6F53D382775C686BCA7535B')
expect(result).toBe(
'4D312F604F638ADF19AFAC6308ECBBC5881E1B6CD6F53D382775C686BCA7535B'
)
})
})
File renamed without changes.
1 change: 1 addition & 0 deletions __tests__/data/valid_blocks.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions __tests__/data/valid_keys.json

Large diffs are not rendered by default.

78 changes: 29 additions & 49 deletions __tests__/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
INVALID_ADDRESSES,
INVALID_HASHES_AND_ADDRESSES,
INVALID_AMOUNTS
} = require('./common/data')
} = require('./data/invalid')

const VALID_SEND_BLOCK = {
previous: 'A286FD300598BF0C8CCC1196943B9CEB94F268CC89F2010B7F7EE4055CC6AB8C',
Expand Down Expand Up @@ -37,28 +37,8 @@ const VALID_RECEIVE_BLOCK = {
hash: 'AD93AE771E883680C18502A7AEBA7B63465F2FB3830F0833DD49A54A5AE133BB'
}

const VALID_STATE_BLOCKS = [
{
account: 'xrb_3igf8hd4sjshoibbbkeitmgkp1o6ug4xads43j6e4gqkj5xk5o83j8ja9php',
previous:
'FC5A7FB777110A858052468D448B2DF22B648943C097C0608D1E2341007438B0',
representative:
'xrb_3p1asma84n8k84joneka776q4egm5wwru3suho9wjsfyuem8j95b3c78nw8j',
balance: '5000000000000000000000000000001',
link: 'B2EC73C1F503F47E051AD72ECB512C63BA8E1A0ACC2CEE4EA9A22FE1CBDB693F', // block hash
hash: '597395E83BD04DF8EF30AF04234EAAFE0606A883CF4AEAD2DB8196AAF5C4444F'
},
{
account: 'xrb_3igf8hd4sjshoibbbkeitmgkp1o6ug4xads43j6e4gqkj5xk5o83j8ja9php',
previous:
'597395E83BD04DF8EF30AF04234EAAFE0606A883CF4AEAD2DB8196AAF5C4444F',
representative:
'xrb_3p1asma84n8k84joneka776q4egm5wwru3suho9wjsfyuem8j95b3c78nw8j',
balance: '3000000000000000000000000000001',
link: 'xrb_1q3hqecaw15cjt7thbtxu3pbzr1eihtzzpzxguoc37bj1wc5ffoh7w74gi6p', // address
hash: '128106287002E595F479ACD615C818117FCB3860EC112670557A2467386249D4'
}
]
const VALID_STATE_BLOCKS = require('./data/valid_blocks')
const RANDOM_VALID_STATE_BLOCK = VALID_STATE_BLOCKS[0]

describe('send', () => {
test('creates correct send hash', () => {
Expand Down Expand Up @@ -226,13 +206,13 @@ describe('state', () => {
for (let validStateBlock of VALID_STATE_BLOCKS) {
expect(
nano.hashStateBlock(
validStateBlock.account,
validStateBlock.previous,
validStateBlock.representative,
validStateBlock.balance,
validStateBlock.link
validStateBlock.block.data.account,
validStateBlock.block.data.previous,
validStateBlock.block.data.representative,
validStateBlock.block.data.balance,
validStateBlock.originalLink
)
).toBe(validStateBlock.hash)
).toBe(validStateBlock.block.hash)
}
})

Expand All @@ -242,10 +222,10 @@ describe('state', () => {
expect(() =>
nano.hashStateBlock(
invalidAddress,
VALID_STATE_BLOCKS[0].previous,
VALID_STATE_BLOCKS[0].representative,
VALID_STATE_BLOCKS[0].balance,
VALID_STATE_BLOCKS[0].link
RANDOM_VALID_STATE_BLOCK.block.data.previous,
RANDOM_VALID_STATE_BLOCK.block.data.representative,
RANDOM_VALID_STATE_BLOCK.block.data.balance,
RANDOM_VALID_STATE_BLOCK.originalLink
)
).toThrowError('Account is not valid')
}
Expand All @@ -256,11 +236,11 @@ describe('state', () => {
for (let invalidHash of INVALID_HASHES) {
expect(() =>
nano.hashStateBlock(
VALID_STATE_BLOCKS[0].account,
RANDOM_VALID_STATE_BLOCK.block.data.account,
invalidHash,
VALID_STATE_BLOCKS[0].representative,
VALID_STATE_BLOCKS[0].balance,
VALID_STATE_BLOCKS[0].link
RANDOM_VALID_STATE_BLOCK.block.data.representative,
RANDOM_VALID_STATE_BLOCK.block.data.balance,
RANDOM_VALID_STATE_BLOCK.originalLink
)
).toThrowError('Previous is not valid')
}
Expand All @@ -271,11 +251,11 @@ describe('state', () => {
for (let invalidAddress of INVALID_ADDRESSES) {
expect(() =>
nano.hashStateBlock(
VALID_STATE_BLOCKS[0].account,
VALID_STATE_BLOCKS[0].previous,
RANDOM_VALID_STATE_BLOCK.block.data.account,
RANDOM_VALID_STATE_BLOCK.block.data.previous,
invalidAddress,
VALID_STATE_BLOCKS[0].balance,
VALID_STATE_BLOCKS[0].link
RANDOM_VALID_STATE_BLOCK.block.data.balance,
RANDOM_VALID_STATE_BLOCK.originalLink
)
).toThrowError('Representative is not valid')
}
Expand All @@ -286,11 +266,11 @@ describe('state', () => {
for (let invalidAmount of INVALID_AMOUNTS) {
expect(() =>
nano.hashStateBlock(
VALID_STATE_BLOCKS[0].account,
VALID_STATE_BLOCKS[0].previous,
VALID_STATE_BLOCKS[0].representative,
RANDOM_VALID_STATE_BLOCK.block.data.account,
RANDOM_VALID_STATE_BLOCK.block.data.previous,
RANDOM_VALID_STATE_BLOCK.block.data.representative,
invalidAmount,
VALID_STATE_BLOCKS[0].link
RANDOM_VALID_STATE_BLOCK.originalLink
)
).toThrowError('Balance is not valid')
}
Expand All @@ -301,10 +281,10 @@ describe('state', () => {
for (let invalidLink of INVALID_HASHES_AND_ADDRESSES) {
expect(() =>
nano.hashStateBlock(
VALID_STATE_BLOCKS[0].account,
VALID_STATE_BLOCKS[0].previous,
VALID_STATE_BLOCKS[0].representative,
VALID_STATE_BLOCKS[0].balance,
RANDOM_VALID_STATE_BLOCK.block.data.account,
RANDOM_VALID_STATE_BLOCK.block.data.previous,
RANDOM_VALID_STATE_BLOCK.block.data.representative,
RANDOM_VALID_STATE_BLOCK.block.data.balance,
invalidLink
)
).toThrowError('Link is not valid')
Expand Down
69 changes: 28 additions & 41 deletions __tests__/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,10 @@ const {
INVALID_SECRET_KEYS,
INVALID_ADDRESSES,
INVALID_PUBLIC_KEYS
} = require('./common/data')
} = require('./data/invalid')

const SEED = 'b947ee0115014a4d49a804e7fc7248e31690b80033ce7a6e3a07bdf93b2584ff'
const KEYS = [
{
index: 0,
secretKey: '23B5E95B4C4325ED5AF109BFE4ACDE782DBAB0163591D9052963723AE8E72A09',
publicKey: '4D312F604F638ADF19AFAC6308ECBBC5881E1B6CD6F53D382775C686BCA7535B',
address: 'xrb_1mbj7xi6yrwcuwetzd5535pdqjea5rfpsoqo9nw4gxg8itycgntucp49i1nz'
},
{
index: 1,
secretKey: 'FEB6959738D43F9B0E6CDCC778BCD2F3384E6F1255510DB6F48E2B99BA059F0F',
publicKey: 'FFFADBB9E15D553101E1DB0C67F20F6984836DC1BB513FF1D0E4A0AF89CE5291',
address: 'xrb_3zztugwy4qco861y5preezs1yte6ifpw5gtj9zrx3s71oy6wwnnj5en5oo5a'
}
]
const VALID_KEYS = require('./data/valid_keys')
const RANDOM_VALID_KEY = VALID_KEYS[0]

describe('seeds', () => {
test('generates different seeds', async () => {
Expand All @@ -39,79 +26,79 @@ describe('seeds', () => {

describe('secret keys', () => {
test('creates correct secret keys', () => {
expect.assertions(KEYS.length)
for (let key of KEYS) {
expect(nano.deriveSecretKey(SEED, key.index)).toBe(key.secretKey)
expect.assertions(VALID_KEYS.length)
for (let key of VALID_KEYS) {
expect(nano.deriveSecretKey(key.seed, key.index)).toBe(key.secretKey)
}
})

test('throws with invalid seeds', () => {
expect.assertions(INVALID_SEEDS.length)
for (let invalidSeed of INVALID_SEEDS) {
expect(
() => nano.deriveSecretKey(invalidSeed, 0)
).toThrowError('Seed is not valid')
expect(() => nano.deriveSecretKey(invalidSeed, 0)).toThrowError(
'Seed is not valid'
)
}
})

test('throws with invalid indexes', () => {
expect.assertions(INVALID_INDEXES.length)
for (let invalidIndex of INVALID_INDEXES) {
expect(
() => nano.deriveSecretKey(SEED, invalidIndex)
expect(() =>
nano.deriveSecretKey(RANDOM_VALID_KEY.seed, invalidIndex)
).toThrowError('Index is not valid')
}
})
})

describe('public keys', () => {
test('creates correct public keys from secret keys', () => {
expect.assertions(KEYS.length)
for (let key of KEYS) {
expect.assertions(VALID_KEYS.length)
for (let key of VALID_KEYS) {
expect(nano.derivePublicKey(key.secretKey)).toBe(key.publicKey)
}
})

test('creates correct public keys from addresses', () => {
expect.assertions(KEYS.length)
for (let key of KEYS) {
expect(nano.derivePublicKey(key.address)).toBe(key.publicKey)
expect.assertions(VALID_KEYS.length)
for (let key of VALID_KEYS) {
expect(nano.derivePublicKey(key.account)).toBe(key.publicKey)
}
})

test('throws with invalid secret keys', () => {
expect.assertions(INVALID_SECRET_KEYS.length)
for (let invalidSecretKey of INVALID_SECRET_KEYS) {
expect(
() => nano.derivePublicKey(invalidSecretKey)
).toThrowError('Secret key or address is not valid')
expect(() => nano.derivePublicKey(invalidSecretKey)).toThrowError(
'Secret key or address is not valid'
)
}
})

test('throws with invalid addresses', () => {
expect.assertions(INVALID_ADDRESSES.length)
for (let invalidAddress of INVALID_ADDRESSES) {
expect(
() => nano.derivePublicKey(invalidAddress)
).toThrowError('Secret key or address is not valid')
expect(() => nano.derivePublicKey(invalidAddress)).toThrowError(
'Secret key or address is not valid'
)
}
})
})

describe('addresses', () => {
test('creates correct addresses', () => {
expect.assertions(KEYS.length)
for (let key of KEYS) {
expect(nano.deriveAddress(key.publicKey)).toBe(key.address)
expect.assertions(VALID_KEYS.length)
for (let key of VALID_KEYS) {
expect(nano.deriveAddress(key.publicKey)).toBe(key.account)
}
})

test('throws with invalid public keys', () => {
expect.assertions(INVALID_PUBLIC_KEYS.length)
for (let invalidPublicKey of INVALID_PUBLIC_KEYS) {
expect(
() => nano.deriveAddress(invalidPublicKey)
).toThrowError('Public key is not valid')
expect(() => nano.deriveAddress(invalidPublicKey)).toThrowError(
'Public key is not valid'
)
}
})
})
Loading

0 comments on commit 3d557b7

Please sign in to comment.