Align NaCl functions with Rust tests (#72)
* Align nacl functions with Rust tests * Match tests from Rust
This commit is contained in:
@@ -30,7 +30,7 @@ naclKeypair() // => { secretKey: [...], publicKey: [...] }
|
||||
Creates a new public/secret keypair from a secret.
|
||||
|
||||
```js
|
||||
naclKeypairFromSecret (secretKey: Uint8Array): { secretKey: Uint8Array, publicKey: Uint8Array }
|
||||
naclKeypairFromSecret (secret: Uint8Array | string): { secretKey: Uint8Array, publicKey: Uint8Array }
|
||||
```
|
||||
|
||||
|
||||
@@ -47,11 +47,11 @@ naclKeypairFromSecret(...) // => { secretKey: [...], publicKey: [...] }
|
||||
Creates a new public/secret keypair from a seed.
|
||||
|
||||
```js
|
||||
naclKeypairFromSeed (seed: Uint8Array): { secretKey: Uint8Array, publicKey: Uint8Array }
|
||||
naclKeypairFromSeed (seed: Uint8Array | string): { secretKey: Uint8Array, publicKey: Uint8Array }
|
||||
```
|
||||
|
||||
|
||||
Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
|
||||
Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed. Seed can be a `Uint8Array`, a hex string or a string of bytes.
|
||||
|
||||
```js
|
||||
import { naclKeypairFromSeed } from '@polkadot/util-crypto';
|
||||
|
||||
@@ -5,11 +5,12 @@
|
||||
|
||||
import type { KeypairType } from '../types';
|
||||
|
||||
const nacl = require('tweetnacl');
|
||||
// const nacl = require('tweetnacl');
|
||||
const naclKeypairFromSeed = require('./keypairFromSeed');
|
||||
|
||||
/**
|
||||
@name naclKeypairFromSecret
|
||||
@signature naclKeypairFromSecret (secretKey: Uint8Array): { secretKey: Uint8Array, publicKey: Uint8Array }
|
||||
@signature naclKeypairFromSecret (secret: Uint8Array | string): { secretKey: Uint8Array, publicKey: Uint8Array }
|
||||
@summary Creates a new public/secret keypair from a secret.
|
||||
@description
|
||||
Returns a object containing a `publicKey` & `secretKey` generated from the supplied secret.
|
||||
@@ -18,6 +19,8 @@ const nacl = require('tweetnacl');
|
||||
|
||||
naclKeypairFromSecret(...) // => { secretKey: [...], publicKey: [...] }
|
||||
*/
|
||||
module.exports = function naclKeypairFromSecret (secretKey: Uint8Array): KeypairType {
|
||||
return nacl.sign.keyPair.fromSecretKey(secretKey);
|
||||
module.exports = function naclKeypairFromSecret (secret: Uint8Array | string): KeypairType {
|
||||
// NOTE: According to the Rust implementation, this is equivalent to the seed version
|
||||
return naclKeypairFromSeed(secret);
|
||||
// return nacl.sign.keyPair.fromSecretKey(secretKey);
|
||||
};
|
||||
|
||||
@@ -2,31 +2,63 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const u8aFromHex = require('@polkadot/util/u8a/fromHex');
|
||||
|
||||
const { naclKeypairFromSecret } = require('./index');
|
||||
|
||||
describe('naclKeypairFromSecret', () => {
|
||||
const secretKey = new Uint8Array([
|
||||
18, 52, 86, 120, 144, 18, 52, 86,
|
||||
120, 144, 18, 52, 86, 120, 144, 18,
|
||||
18, 52, 86, 120, 144, 18, 52, 86,
|
||||
120, 144, 18, 52, 86, 120, 144, 18,
|
||||
180, 114, 93, 155, 165, 255, 217, 82,
|
||||
16, 250, 209, 11, 193, 10, 88, 218,
|
||||
190, 190, 41, 193, 236, 252, 1, 152,
|
||||
216, 214, 0, 41, 45, 138, 13, 53
|
||||
]);
|
||||
|
||||
it('generates a valid publicKey/secretKey pair', () => {
|
||||
// NOTE: From Rust "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60"
|
||||
expect(
|
||||
naclKeypairFromSecret(secretKey)
|
||||
naclKeypairFromSecret(
|
||||
u8aFromHex(
|
||||
'0x9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60'
|
||||
)
|
||||
)
|
||||
).toEqual({
|
||||
// Note: From Rust "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"
|
||||
publicKey: new Uint8Array([
|
||||
180, 114, 93, 155, 165, 255, 217, 82,
|
||||
16, 250, 209, 11, 193, 10, 88, 218,
|
||||
190, 190, 41, 193, 236, 252, 1, 152,
|
||||
216, 214, 0, 41, 45, 138, 13, 53
|
||||
0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7,
|
||||
0xd5, 0x4b, 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a,
|
||||
0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, 0x23, 0x25,
|
||||
0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a
|
||||
]),
|
||||
secretKey
|
||||
secretKey: new Uint8Array([
|
||||
157, 97, 177, 157, 239, 253, 90, 96, 186, 132, 74, 244, 146, 236, 44, 196, 68, 73, 197, 105, 123, 50, 105, 25, 112, 59, 172, 3, 28, 174, 127, 96,
|
||||
// public part
|
||||
0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7,
|
||||
0xd5, 0x4b, 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a,
|
||||
0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, 0x23, 0x25,
|
||||
0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a
|
||||
])
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// NOTE: Original, non-fixed-to-Rust version
|
||||
// describe('naclKeypairFromSecret', () => {
|
||||
// const secretKey = new Uint8Array([
|
||||
// 18, 52, 86, 120, 144, 18, 52, 86,
|
||||
// 120, 144, 18, 52, 86, 120, 144, 18,
|
||||
// 18, 52, 86, 120, 144, 18, 52, 86,
|
||||
// 120, 144, 18, 52, 86, 120, 144, 18,
|
||||
// 180, 114, 93, 155, 165, 255, 217, 82,
|
||||
// 16, 250, 209, 11, 193, 10, 88, 218,
|
||||
// 190, 190, 41, 193, 236, 252, 1, 152,
|
||||
// 216, 214, 0, 41, 45, 138, 13, 53
|
||||
// ]);
|
||||
//
|
||||
// it('generates a valid publicKey/secretKey pair', () => {
|
||||
// expect(
|
||||
// naclKeypairFromSecret(secretKey)
|
||||
// ).toEqual({
|
||||
// publicKey: new Uint8Array([
|
||||
// 180, 114, 93, 155, 165, 255, 217, 82,
|
||||
// 16, 250, 209, 11, 193, 10, 88, 218,
|
||||
// 190, 190, 41, 193, 236, 252, 1, 152,
|
||||
// 216, 214, 0, 41, 45, 138, 13, 53
|
||||
// ]),
|
||||
// secretKey
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
|
||||
@@ -6,18 +6,38 @@
|
||||
import type { KeypairType } from '../types';
|
||||
|
||||
const nacl = require('tweetnacl');
|
||||
const hexToU8a = require('@polkadot/util/hex/toU8a');
|
||||
const isHex = require('@polkadot/util/is/hex');
|
||||
const isUint8Array = require('@polkadot/util/is/uint8Array');
|
||||
const u8aFromString = require('@polkadot/util/u8a/fromString');
|
||||
|
||||
/**
|
||||
@name naclKeypairFromSeed
|
||||
@signature naclKeypairFromSeed (seed: Uint8Array): { secretKey: Uint8Array, publicKey: Uint8Array }
|
||||
@signature naclKeypairFromSeed (seed: Uint8Array | string): { secretKey: Uint8Array, publicKey: Uint8Array }
|
||||
@summary Creates a new public/secret keypair from a seed.
|
||||
@description
|
||||
Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
|
||||
Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed. Seed can be a `Uint8Array`, a hex string or a string of bytes.
|
||||
@example
|
||||
import { naclKeypairFromSeed } from '@polkadot/util-crypto';
|
||||
|
||||
naclKeypairFromSeed(...) // => { secretKey: [...], publicKey: [...] }
|
||||
*/
|
||||
module.exports = function naclKeypairFromSeed (seed: Uint8Array): KeypairType {
|
||||
return nacl.sign.keyPair.fromSeed(seed);
|
||||
module.exports = function naclKeypairFromSeed (seed: Uint8Array | string): KeypairType {
|
||||
if (isUint8Array(seed)) {
|
||||
// $FlowFixMe type has been determined
|
||||
return nacl.sign.keyPair.fromSeed(seed);
|
||||
}
|
||||
|
||||
// $FlowFixMe type has been determined
|
||||
if (isHex(seed)) {
|
||||
return nacl.sign.keyPair.fromSeed(
|
||||
// $FlowFixMe type has been determined
|
||||
hexToU8a(seed)
|
||||
);
|
||||
}
|
||||
|
||||
return nacl.sign.keyPair.fromSeed(
|
||||
// $FlowFixMe type has been determined
|
||||
u8aFromString(seed)
|
||||
);
|
||||
};
|
||||
|
||||
@@ -2,35 +2,31 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const u8aFromString = require('@polkadot/util/u8a/fromString');
|
||||
|
||||
const { naclKeypairFromSeed } = require('./index');
|
||||
|
||||
describe('naclKeypairFromSeed', () => {
|
||||
// NOTE: Aligned with Rust test, b"12345678901234567890123456789012"
|
||||
it('generates a valid publicKey/secretKey pair', () => {
|
||||
expect(
|
||||
naclKeypairFromSeed(
|
||||
new Uint8Array([
|
||||
0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56,
|
||||
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12,
|
||||
0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56,
|
||||
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12
|
||||
])
|
||||
u8aFromString('12345678901234567890123456789012')
|
||||
)
|
||||
).toEqual({
|
||||
publicKey: new Uint8Array([
|
||||
180, 114, 93, 155, 165, 255, 217, 82,
|
||||
16, 250, 209, 11, 193, 10, 88, 218,
|
||||
190, 190, 41, 193, 236, 252, 1, 152,
|
||||
216, 214, 0, 41, 45, 138, 13, 53
|
||||
0x2f, 0x8c, 0x61, 0x29, 0xd8, 0x16, 0xcf, 0x51,
|
||||
0xc3, 0x74, 0xbc, 0x7f, 0x08, 0xc3, 0xe6, 0x3e,
|
||||
0xd1, 0x56, 0xcf, 0x78, 0xae, 0xfb, 0x4a, 0x65,
|
||||
0x50, 0xd9, 0x7b, 0x87, 0x99, 0x79, 0x77, 0xee
|
||||
]),
|
||||
secretKey: new Uint8Array([
|
||||
18, 52, 86, 120, 144, 18, 52, 86,
|
||||
120, 144, 18, 52, 86, 120, 144, 18,
|
||||
18, 52, 86, 120, 144, 18, 52, 86,
|
||||
120, 144, 18, 52, 86, 120, 144, 18,
|
||||
180, 114, 93, 155, 165, 255, 217, 82,
|
||||
16, 250, 209, 11, 193, 10, 88, 218,
|
||||
190, 190, 41, 193, 236, 252, 1, 152,
|
||||
216, 214, 0, 41, 45, 138, 13, 53
|
||||
49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50,
|
||||
// public part
|
||||
0x2f, 0x8c, 0x61, 0x29, 0xd8, 0x16, 0xcf, 0x51,
|
||||
0xc3, 0x74, 0xbc, 0x7f, 0x08, 0xc3, 0xe6, 0x3e,
|
||||
0xd1, 0x56, 0xcf, 0x78, 0xae, 0xfb, 0x4a, 0x65,
|
||||
0x50, 0xd9, 0x7b, 0x87, 0x99, 0x79, 0x77, 0xee
|
||||
])
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ Utility methods to convert to and from `Uint8Array` objects
|
||||
|
||||
- [u8aFromBuffer](#u8afrombuffer) Creates a Uint8Array value from a Buffer object.
|
||||
- [u8aFromHex](#u8afromhex) Creates a Uint8Array value from a hex string.
|
||||
- [u8aFromString](#u8afromstring) Creates a Uint8Array object from a string.
|
||||
- [u8aFromUtf8](#u8afromutf8) Creates a Uint8Array object from a utf-8 string.
|
||||
- [u8aToBuffer](#u8atobuffer) Creates a Buffer object from a hex string.
|
||||
- [u8aToUtf8](#u8atoutf8) Creates a utf-8 string from a Uint8Array object.
|
||||
@@ -32,6 +33,23 @@ u8aFromHex (value?: string): Uint8Array
|
||||
|
||||
|
||||
|
||||
## u8aFromString
|
||||
|
||||
Creates a Uint8Array object from a string.
|
||||
|
||||
```js
|
||||
u8aFromString (value: string): UInt8Array
|
||||
```
|
||||
|
||||
|
||||
String input values return the actual encoded `UInt8Array`.
|
||||
|
||||
```js
|
||||
import { u8aFromString } from '@polkadot/util';
|
||||
|
||||
u8aFromString('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
|
||||
```
|
||||
|
||||
## u8aFromUtf8
|
||||
|
||||
Creates a Uint8Array object from a utf-8 string.
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name u8aFromString
|
||||
@signature u8aFromString (value: string): UInt8Array
|
||||
@summary Creates a Uint8Array object from a string.
|
||||
@description
|
||||
String input values return the actual encoded `UInt8Array`.
|
||||
@example
|
||||
import { u8aFromString } from '@polkadot/util';
|
||||
|
||||
u8aFromString('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
|
||||
*/
|
||||
module.exports = function u8aFromString (value: string): Uint8Array {
|
||||
const stringArray = [];
|
||||
|
||||
for (let index = 0; index < value.length; index++) {
|
||||
stringArray.push(
|
||||
value.charCodeAt(index)
|
||||
);
|
||||
}
|
||||
|
||||
return new Uint8Array(stringArray);
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aFromString } = require('./index');
|
||||
|
||||
describe('u8aFromString', () => {
|
||||
it('encodes a string byte for byte', () => {
|
||||
expect(
|
||||
u8aFromString('12345678901234567890123456789012')
|
||||
).toEqual(
|
||||
new Uint8Array([
|
||||
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
|
||||
0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
|
||||
0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34,
|
||||
0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,7 @@
|
||||
const u8aFromBuffer = require('./fromBuffer');
|
||||
const u8aFromHex = require('./fromHex');
|
||||
const u8aFromUtf8 = require('./fromUtf8');
|
||||
const u8aFromString = require('./fromString');
|
||||
const u8aToBuffer = require('./toBuffer');
|
||||
const u8aToUtf8 = require('./toUtf8');
|
||||
|
||||
@@ -15,6 +16,7 @@ const u8aToUtf8 = require('./toUtf8');
|
||||
module.exports = {
|
||||
u8aFromBuffer,
|
||||
u8aFromHex,
|
||||
u8aFromString,
|
||||
u8aFromUtf8,
|
||||
u8aToBuffer,
|
||||
u8aToUtf8
|
||||
|
||||
Reference in New Issue
Block a user