Schnorrkel support (#297)

* Initial schnorrkel support

* Start conversion of actual keyring

* Wrap encoding/decoding (needs another check)

* Manage encoding version & types with passthrough

* Update to version with working sign

* Cleanup pair interface (with seed)

* Fix failing tests

* Align naclSign signature

* Update tests for nacl (with TS conversion)

* Bump version with CHANGELOG

* Remove unneeded dep

* Use KeyringOptions instead of PairType

* Do isNumber check on addressPrefix
This commit is contained in:
Jaco Greeff
2019-02-13 16:46:33 +01:00
committed by GitHub
parent 0f53f0ebf0
commit 2aba82e569
60 changed files with 693 additions and 293 deletions
+3
View File
@@ -0,0 +1,3 @@
# 0.34.1
- Keyring now allows for ed25519 and sr25519 instances. Upon creation, you need to pass the `ed25519`or `sr25519` string to denote the type
+1 -1
View File
@@ -9,5 +9,5 @@
"packages": [
"packages/*"
],
"version": "0.33.36"
"version": "0.34.0"
}
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/chainspec",
"version": "0.33.36",
"version": "0.34.0",
"description": "Contains chain specifications for known chains",
"main": "index.js",
"keywords": [
@@ -29,6 +29,6 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/chainspec#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/util": "^0.33.36"
"@polkadot/util": "^0.34.0"
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/db",
"version": "0.33.36",
"version": "0.34.0",
"description": "Implementation of a basic sync in-memory and on-disk databases with overlays",
"main": "index.js",
"keywords": [
@@ -29,8 +29,8 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/db#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/trie-hash": "^0.33.36",
"@polkadot/util": "^0.33.36",
"@polkadot/trie-hash": "^0.34.0",
"@polkadot/util": "^0.34.0",
"@types/mkdirp": "^0.5.2",
"@types/snappy": "^6.0.0",
"lru_map": "^0.3.3",
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/keyring",
"version": "0.33.36",
"version": "0.34.0",
"description": "Keyring management",
"main": "index.js",
"engines": {
@@ -31,8 +31,8 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/util": "^0.33.36",
"@polkadot/util-crypto": "^0.33.36",
"@polkadot/util": "^0.34.0",
"@polkadot/util-crypto": "^0.34.0",
"@types/bs58": "^4.0.0",
"bs58": "^4.0.1"
}
@@ -6,7 +6,7 @@
import testingPairs from '../testingPairs';
import decode from './decode';
const keyring = testingPairs();
const keyring = testingPairs({ type: 'ed25519' });
describe('decode', () => {
it('decodes an address', () => {
@@ -6,7 +6,7 @@
import testingPairs from '../testingPairs';
import encode from './encode';
const keyring = testingPairs();
const keyring = testingPairs({ type: 'ed25519' });
describe('encode', () => {
it('encodes an address to a valid value', () => {
-61
View File
@@ -1,61 +0,0 @@
// Copyright 2017-2019 @polkadot/keyring authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { hexToU8a, stringToU8a } from '@polkadot/util/index';
import Keyring from './index';
import setPrefix from './address/setPrefix';
describe('keypair', () => {
const publicKeyOne = new Uint8Array([47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238]);
const publicKeyTwo = new Uint8Array([215, 90, 152, 1, 130, 177, 10, 183, 213, 75, 254, 211, 201, 100, 7, 58, 14, 225, 114, 243, 218, 166, 35, 37, 175, 2, 26, 104, 247, 7, 81, 26]);
const seedOne = stringToU8a('12345678901234567890123456789012');
const seedTwo = hexToU8a('0x9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60');
let keypair;
beforeEach(() => {
keypair = new Keyring();
keypair.addFromSeed(seedOne);
setPrefix(42);
});
it('adds the pair', () => {
expect(
keypair.addFromSeed(seedTwo).publicKey()
).toEqual(publicKeyTwo);
});
it('adds from a mnemonic', () => {
setPrefix(68);
expect(
keypair.addFromMnemonic('moral movie very draw assault whisper awful rebuild speed purity repeat card').address()
).toEqual('7pDZKLEixRnF6Q5jzr7DsCEiNPt3d6Rknc14SyUcnRwTQK14');
});
it('allows publicKeys retrieval', () => {
keypair.addFromSeed(seedTwo);
expect(
keypair.getPublicKeys()
).toEqual([ publicKeyOne, publicKeyTwo ]);
});
it('allows retrieval of a specific item', () => {
expect(
keypair.getPair(publicKeyOne).publicKey()
).toEqual(publicKeyOne);
});
it('allows adding from JSON', () => {
expect(
keypair.addFromJson(
JSON.parse('{"address":"5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ","encoded":"0xb4a14995d25ab609f3686e9fa45f1fb237cd833f33f00d4b12c51858ca070d96972e47d73aae5eeb0fc06f923826cf0943fdb02c2c2ee30ef52a7912663053940d1da4da66b3a3f520ae07422c1c94b2d95690fca9d1f4a997623bb2923a8833280e19e7f72c3c5cfa343974e60e2b3dc53b404fdaf330756daad5e4e3","encoding":{"content":"pkcs8","type":"xsalsa20-poly1305","version":"0"},"meta":{"isTesting":true,"name":"alice"}}')
).publicKey()
).toEqual(
new Uint8Array([209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79])
);
});
});
+63
View File
@@ -0,0 +1,63 @@
// Copyright 2017-2019 @polkadot/keyring authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { hexToU8a, stringToU8a } from '@polkadot/util/index';
import Keyring from './index';
import setPrefix from './address/setPrefix';
describe('keypair', () => {
describe('ed25519', () => {
const publicKeyOne = new Uint8Array([47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238]);
const publicKeyTwo = new Uint8Array([215, 90, 152, 1, 130, 177, 10, 183, 213, 75, 254, 211, 201, 100, 7, 58, 14, 225, 114, 243, 218, 166, 35, 37, 175, 2, 26, 104, 247, 7, 81, 26]);
const seedOne = stringToU8a('12345678901234567890123456789012');
const seedTwo = hexToU8a('0x9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60');
let keypair: Keyring;
beforeEach(() => {
keypair = new Keyring({ type: 'ed25519' });
keypair.addFromSeed(seedOne, {});
setPrefix(42);
});
it('adds the pair', () => {
expect(
keypair.addFromSeed(seedTwo, {}).publicKey()
).toEqual(publicKeyTwo);
});
it('adds from a mnemonic', () => {
setPrefix(68);
expect(
keypair.addFromMnemonic('moral movie very draw assault whisper awful rebuild speed purity repeat card', {}).address()
).toEqual('7pDZKLEixRnF6Q5jzr7DsCEiNPt3d6Rknc14SyUcnRwTQK14');
});
it('allows publicKeys retrieval', () => {
keypair.addFromSeed(seedTwo, {});
expect(
keypair.getPublicKeys()
).toEqual([ publicKeyOne, publicKeyTwo ]);
});
it('allows retrieval of a specific item', () => {
expect(
keypair.getPair(publicKeyOne).publicKey()
).toEqual(publicKeyOne);
});
it('allows adding from JSON', () => {
expect(
keypair.addFromJson(
JSON.parse('{"address":"5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ","encoded":"0xb4a14995d25ab609f3686e9fa45f1fb237cd833f33f00d4b12c51858ca070d96972e47d73aae5eeb0fc06f923826cf0943fdb02c2c2ee30ef52a7912663053940d1da4da66b3a3f520ae07422c1c94b2d95690fca9d1f4a997623bb2923a8833280e19e7f72c3c5cfa343974e60e2b3dc53b404fdaf330756daad5e4e3","encoding":{"content":"pkcs8","type":"xsalsa20-poly1305","version":"0"},"meta":{"isTesting":true,"name":"alice"}}')
).publicKey()
).toEqual(
new Uint8Array([209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79])
);
});
});
});
+19 -82
View File
@@ -2,10 +2,10 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringInstance, KeyringPair, KeyringPair$Json, KeyringPair$Meta } from './types';
import { KeyringInstance, KeyringPair, KeyringPair$Json, KeyringPair$Meta, KeyringOptions, PairType } from './types';
import { hexToU8a } from '@polkadot/util/index';
import { mnemonicToSeed , naclKeypairFromSeed } from '@polkadot/util-crypto/index';
import { assert, hexToU8a, isNumber } from '@polkadot/util/index';
import { mnemonicToSeed , naclKeypairFromSeed as naclFromSeed, schnorrkelKeypairFromSeed as schnorrkelFromSeed } from '@polkadot/util-crypto/index';
import { decodeAddress, encodeAddress, setAddressPrefix } from './address';
import createPair from './pair';
@@ -26,14 +26,18 @@ import Pairs from './pairs';
* dictionary is achieved using `removePair`. Retrieval of all the stored pairs via `getPairs` or perform
* lookup of a pair for a given account address or public key using `getPair`. JSON metadata associated with
* an account may be obtained using `toJson` accompanied by the account passphrase.
* @example See [Polkadot-JS Common Keyring Examples](https://polkadot.js.org/api/common/examples/keyring/) and the [Polkadot-JS Apps ui-keyring package](https://github.com/polkadot-js/apps/tree/master/packages/ui-keyring)
* where it is implemented.
*/
export default class Keyring implements KeyringInstance {
private _pairs: Pairs;
private _type: PairType;
constructor (options: KeyringOptions) {
assert(options && ['ed25519', 'sr25519'].includes(options.type || 'undefined'), `Expected a keyring type of either 'ed25519' or 'sr25519', found '${options.type}`);
constructor () {
this._pairs = new Pairs();
this._type = options.type as PairType;
setAddressPrefix(isNumber(options.addressPrefix) ? options.addressPrefix : 42);
}
decodeAddress = decodeAddress;
@@ -58,9 +62,8 @@ export default class Keyring implements KeyringInstance {
* of an account backup), and then generates a keyring pair from them that it passes to
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
*/
addFromAddress (address: string | Uint8Array, meta?: KeyringPair$Meta, defaultEncoded?: Uint8Array): KeyringPair {
// @ts-ignore no secretKey - cannot unlock
return this.addPair(createPair({ publicKey: this.decodeAddress(address) }, meta, defaultEncoded));
addFromAddress (address: string | Uint8Array, meta: KeyringPair$Meta, encoded: Uint8Array | null): KeyringPair {
return this.addPair(createPair(this._type, { publicKey: this.decodeAddress(address) }, meta, encoded));
}
/**
@@ -70,8 +73,6 @@ export default class Keyring implements KeyringInstance {
* @description Allows user to provide a json object argument that contains account information (that may be obtained from the json file
* of an account backup), and then generates a keyring pair from it that it passes to
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
* @example
* Refer to an actual implementation in [Polkadot-JS ui-keyring > account > save](https://github.com/polkadot-js/apps/blob/master/packages/ui-keyring/src/account/save.ts#L15)
*/
addFromJson ({ address, encoded, meta }: KeyringPair$Json): KeyringPair {
return this.addFromAddress(address, meta, hexToU8a(encoded));
@@ -86,7 +87,7 @@ export default class Keyring implements KeyringInstance {
* of an account backup), and then generates a keyring pair from it that it passes to
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
*/
addFromMnemonic (mnemonic: string, meta?: KeyringPair$Meta): KeyringPair {
addFromMnemonic (mnemonic: string, meta: KeyringPair$Meta): KeyringPair {
return this.addFromSeed(mnemonicToSeed(mnemonic), meta);
}
@@ -97,26 +98,13 @@ export default class Keyring implements KeyringInstance {
* @description Stores in a keyring pair dictionary the public key of the pair as a key and the pair as the associated value.
* Allows user to provide the account seed as an argument, and then generates a keyring pair from it that it passes to
* `addPair` to store in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
* @example
* <BR>
*
* ```javascript
* import Keyring from '@polkadot/keyring';
* import stringToU8a from '@polkadot/util/string/toU8a';
*
* const ALICE_SEED = 'Alice'.padEnd(32, ' ');
*
* // Create an instance of the Keyring
* const keyring = new Keyring();
*
* // Add Alice to our keyring pair dictionary (with the known seed for the account)
* const pairAlice = keyring.addFromSeed(stringToU8a(ALICE_SEED));
* ```
*
* Refer to an actual implementation in [Polkadot-JS ui-keyring > account > create](https://github.com/polkadot-js/apps/blob/master/packages/ui-keyring/src/account/create.ts#L11)
*/
addFromSeed (seed: Uint8Array, meta?: KeyringPair$Meta): KeyringPair {
return this.addPair(createPair(naclKeypairFromSeed(seed), meta));
addFromSeed (seed: Uint8Array, meta: KeyringPair$Meta): KeyringPair {
const keypair = this._type === 'sr25519'
? schnorrkelFromSeed(seed)
: naclFromSeed(seed);
return this.addPair(createPair(this._type, { ...keypair, seed }, meta, null));
}
/**
@@ -125,31 +113,6 @@ export default class Keyring implements KeyringInstance {
* @summary Retrieves an account keyring pair from the Keyring Pair Dictionary, given an account address
* @description Returns a keyring pair value from the keyring pair dictionary by performing
* a key lookup using the provided account address or public key (after decoding it).
* @example
* <BR>
*
* ```javascript
* import Keyring from '@polkadot/keyring';
* import stringToU8a from '@polkadot/util/string/toU8a';
*
* const ALICE_SEED = 'Alice'.padEnd(32, ' ');
*
* // Create an instance of the Keyring
* const keyring = new Keyring();
*
* // Add Alice to our keyring pair dictionary (with the known seed for the account)
* const pairAlice = keyring.addFromSeed(stringToU8a(ALICE_SEED));
*
* // Retrieve the same pair that is stored in the keyring pair dictionary by
* // providing the account address as an argument, and also the public key as an argument.
* // The pairs that are returned should both be the same value as `pairAlice` above
* const pairAliceRetrievedWithAddress = keyring.getPair(pairAlice.address());
* const pairAliceRetrievedWithPublicKey = keyring.getPair(pairAlice.publicKey());
*
* // Check if all returned pairs are the same
* console.log(`Are all pairs the same? : ` +
* `${pairAlice === pairAliceRetrievedWithAddress === pairAliceRetrievedWithPublicKey}`);
* ```
*/
getPair (address: string | Uint8Array): KeyringPair {
return this._pairs.get(address);
@@ -160,28 +123,6 @@ export default class Keyring implements KeyringInstance {
* @signature getPairs (): Array<KeyringPair>
* @summary Retrieves all account keyring pairs from the Keyring Pair Dictionary
* @description Returns an array list of all the keyring pair values that are stored in the keyring pair dictionary.
* @example
* <BR>
*
* ```javascript
* import testKeyring from '@polkadot/keyring/testing';
*
* // Create an instance of Keyring that includes test accounts
* const keyring = testingPairs();
*
* keyring
* .getPairs()
* .forEach((pair) => {
* const address = pair.address();
*
* keyring.addPair(address, {
* address,
* meta: pair.getMeta()
* });
* });
* ```
*
* Refer to an actual implementation in [Polkadot-JS ui-keyring > loadAll](https://github.com/polkadot-js/apps/blob/master/packages/ui-keyring/src/loadAll.ts)
*/
getPairs (): Array<KeyringPair> {
return this._pairs.all();
@@ -205,8 +146,6 @@ export default class Keyring implements KeyringInstance {
* @name removePair
* @signature removePair (address: string | Uint8Array): void
* @description Deletes the provided input address or public key from the stored Keyring Pair Dictionary.
* @example
* Refer to an actual implementation in [Polkadot-JS ui-keyring > account > forget](https://github.com/polkadot-js/apps/blob/master/packages/ui-keyring/src/account/forget.ts)
*/
removePair (address: string | Uint8Array): void {
this._pairs.remove(address);
@@ -220,8 +159,6 @@ export default class Keyring implements KeyringInstance {
* when valid address or public key and when the account passphrase is provided if the account secret
* is not already unlocked and available in memory. Note that in [Polkadot-JS Apps](https://github.com/polkadot-js/apps) the user
* may backup their account to a JSON file that contains this information.
* @example
* Refer to an actual implementation in [Polkadot-JS ui-keyring > account > save](https://github.com/polkadot-js/apps/blob/master/packages/ui-keyring/src/account/save.ts#L9)
*/
toJson (address: string | Uint8Array, passphrase?: string): KeyringPair$Json {
return this._pairs.get(address).toJson(passphrase);
@@ -4,7 +4,7 @@
import testingPairs from '../testingPairs';
const keyring = testingPairs();
const keyring = testingPairs({ type: 'ed25519' });
describe('decode', () => {
it('fails when no data provided', () => {
@@ -14,9 +14,11 @@ describe('decode', () => {
});
it('returns correct publicKey from encoded', () => {
const PASS = 'testing';
expect(
() => keyring.alice.decodePkcs8(
'testing', keyring.alice.encodePkcs8('testing')
PASS, keyring.alice.encodePkcs8(PASS)
)
).not.toThrow();
});
+19 -16
View File
@@ -2,44 +2,47 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assert, stringToU8a, u8aConcat, u8aFixLength } from '@polkadot/util/index';
import { naclDecrypt, naclKeypairFromSeed } from '@polkadot/util-crypto/index';
import { assert, stringToU8a, u8aFixLength } from '@polkadot/util/index';
import { naclDecrypt } from '@polkadot/util-crypto/index';
import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
import { KEY_LENGTH, NONCE_LENGTH, PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
const KEY_LENGTH = 32;
const SEED_OFFSET = PKCS8_HEADER.length;
const DIV_OFFSET = SEED_OFFSET + KEY_LENGTH;
const PUBLIC_OFFSET = SEED_OFFSET + KEY_LENGTH + PKCS8_DIVIDER.length;
const PUBLIC_OFFSET = DIV_OFFSET + PKCS8_DIVIDER.length;
export default function decode (passphrase?: string, _encrypted?: Uint8Array) {
type DecodeResult = {
publicKey: Uint8Array,
seed: Uint8Array
};
export default function decode (passphrase?: string, _encrypted?: Uint8Array | null): DecodeResult {
assert(_encrypted, `No encrypted data available to decode`);
const encrypted = (_encrypted as Uint8Array);
const encoded = passphrase
? (naclDecrypt(
encrypted.subarray(24),
encrypted.subarray(0, 24),
u8aFixLength(stringToU8a(passphrase), 256, true)) as Uint8Array)
encrypted.subarray(NONCE_LENGTH),
encrypted.subarray(0, NONCE_LENGTH),
u8aFixLength(stringToU8a(passphrase), 256, true)
) as Uint8Array)
: encrypted;
assert(encoded, `Unable to unencrypt using the supplied passphrase`);
const header = encoded.subarray(0, PKCS8_HEADER.length);
const divider = encoded.subarray(DIV_OFFSET, DIV_OFFSET + PKCS8_DIVIDER.length);
assert(header.toString() === PKCS8_HEADER.toString(), 'Invalid Pkcs8 header found in body');
const divider = encoded.subarray(DIV_OFFSET, DIV_OFFSET + PKCS8_DIVIDER.length);
assert(divider.toString() === PKCS8_DIVIDER.toString(), 'Invalid Pkcs8 divider found in body');
const publicKey = encoded.subarray(PUBLIC_OFFSET, PUBLIC_OFFSET + KEY_LENGTH);
const seed = encoded.subarray(SEED_OFFSET, SEED_OFFSET + KEY_LENGTH);
const secretKey = u8aConcat(seed, publicKey);
const validate = naclKeypairFromSeed(seed);
assert(validate.publicKey.toString() === publicKey.toString(), 'Pkcs8 decoded publicKeys are not matching');
const publicKey = encoded.subarray(PUBLIC_OFFSET, PUBLIC_OFFSET + KEY_LENGTH);
return {
publicKey,
secretKey
seed
};
}
+6
View File
@@ -4,8 +4,14 @@
const PKCS8_DIVIDER = new Uint8Array([161, 35, 3, 33, 0]);
const PKCS8_HEADER = new Uint8Array([48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
const KEY_LENGTH = 32;
const SEC_LENGTH = 64;
const NONCE_LENGTH = 24;
export {
KEY_LENGTH,
SEC_LENGTH,
NONCE_LENGTH,
PKCS8_DIVIDER,
PKCS8_HEADER
};
@@ -8,7 +8,7 @@ import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
const PKCS8_LENGTH = PKCS8_DIVIDER.length + PKCS8_HEADER.length + 64;
const ENCODED_LENGTH = 125;
const keyring = testingPairs();
const keyring = testingPairs({ type: 'ed25519' });
describe('encode', () => {
it('returns PKCS8 when no passphrase supplied', () => {
+6 -4
View File
@@ -2,17 +2,19 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { stringToU8a, u8aConcat, u8aFixLength } from '@polkadot/util/index';
import { stringToU8a, u8aConcat, u8aFixLength, assert } from '@polkadot/util/index';
import { naclEncrypt } from '@polkadot/util-crypto/index';
import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
export default function encode (secretKey: Uint8Array, passphrase?: string): Uint8Array {
export default function encode (publicKey: Uint8Array, seed?: Uint8Array | null, passphrase?: string): Uint8Array {
assert(seed, 'Expected a valid seed to be passed to encode');
const encoded = u8aConcat(
PKCS8_HEADER,
secretKey.subarray(0, 32),
seed as Uint8Array,
PKCS8_DIVIDER,
secretKey.subarray(32, 64)
publicKey
);
if (!passphrase) {
-10
View File
@@ -1,10 +0,0 @@
// Copyright 2017-2019 @polkadot/keyring authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair$Meta } from '../types';
import { PairState } from './types';
export default function getMeta (state: PairState): KeyringPair$Meta {
return state.meta;
}
@@ -4,7 +4,7 @@
import testingPairs from '../testingPairs';
const keyring = testingPairs();
const keyring = testingPairs({ type: 'ed25519' });
describe('pair', () => {
const SIGNATURE = new Uint8Array([80, 191, 198, 147, 225, 207, 75, 88, 126, 39, 129, 109, 191, 38, 72, 181, 75, 254, 81, 143, 244, 79, 237, 38, 236, 141, 28, 252, 134, 26, 169, 234, 79, 33, 153, 158, 151, 34, 175, 188, 235, 20, 35, 135, 83, 120, 139, 211, 233, 130, 1, 208, 201, 215, 73, 80, 56, 98, 185, 196, 11, 8, 193, 14]);
+33 -23
View File
@@ -3,21 +3,18 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeypairType } from '@polkadot/util-crypto/types';
import { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '../types';
import { PairState } from './types';
import { KeyringPair, KeyringPair$Json, KeyringPair$Meta, PairType } from '../types';
import { PairInfo } from './types';
import { naclSign, naclVerify } from '@polkadot/util-crypto/index';
import { naclKeypairFromSeed as naclFromSeed, naclSign, naclVerify, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVerify } from '@polkadot/util-crypto/index';
import { encodeAddress } from '../address';
import decode from './decode';
import encode from './encode';
import getMeta from './getMeta';
import setMeta from './setMeta';
import toJson from './toJson';
/**
* @name pair
* @signature pair ({ publicKey, secretKey }: KeypairType, meta: KeyringPair$Meta = {}, defaultEncoded?: Uint8Array): KeyringPair
* @summary Creates a keyring pair object
* @description Creates a keyring pair object with provided account public key, metadata, and encoded arguments.
* The keyring pair stores the account state including the encoded address and associated metadata.
@@ -47,25 +44,37 @@ import toJson from './toJson';
* an `encoded` property that is assigned with the encoded public key in hex format, and an `encoding`
* property that indicates whether the public key value of the `encoded` property is encoded or not.
*/
export default function pair ({ publicKey, secretKey }: KeypairType, meta: KeyringPair$Meta = {}, defaultEncoded?: Uint8Array): KeyringPair {
const state: PairState = {
meta: { ...meta },
publicKey
};
export default function pair (type: PairType, { publicKey, seed }: PairInfo, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null): KeyringPair {
const fromSeed = (seed: Uint8Array) =>
type === 'sr25519'
? schnorrkelFromSeed(seed)
: naclFromSeed(seed);
const sign = (message: Uint8Array, pair: Partial<KeypairType>) =>
type === 'sr25519'
? schnorrkelSign(message, pair)
: naclSign(message, pair);
const verify = (message: Uint8Array, signature: Uint8Array) =>
type === 'sr25519'
? schnorrkelVerify(message, signature, publicKey)
: naclVerify(message, signature, publicKey);
let secretKey: Uint8Array | undefined = seed
? fromSeed(seed).secretKey
: undefined;
return {
address: (): string =>
encodeAddress(state.publicKey),
decodePkcs8: (passphrase?: string, encoded?: Uint8Array): void => {
const decoded = decode(passphrase, encoded || defaultEncoded);
encodeAddress(publicKey),
decodePkcs8: (passphrase?: string, _encoded?: Uint8Array | null): void => {
const decoded = decode(passphrase, _encoded || encoded);
state.publicKey = decoded.publicKey;
secretKey = decoded.secretKey;
publicKey = decoded.publicKey;
seed = decoded.seed;
secretKey = fromSeed(seed).secretKey;
},
encodePkcs8: (passphrase?: string): Uint8Array =>
encode(secretKey, passphrase),
encode(publicKey, seed, passphrase),
getMeta: (): KeyringPair$Meta =>
getMeta(state),
meta,
isLocked: (): boolean =>
(!secretKey || secretKey.length === 0),
lock: (): void => {
@@ -73,13 +82,14 @@ export default function pair ({ publicKey, secretKey }: KeypairType, meta: Keyri
},
publicKey: (): Uint8Array =>
publicKey,
setMeta: (meta: KeyringPair$Meta): void =>
setMeta(state, meta),
setMeta: (_meta: KeyringPair$Meta): void => {
meta = meta;
},
sign: (message: Uint8Array): Uint8Array =>
naclSign(message, secretKey),
sign(message, { publicKey, secretKey }),
toJson: (passphrase?: string): KeyringPair$Json =>
toJson(state, encode(secretKey, passphrase), !!passphrase),
toJson({ meta, publicKey }, encode(publicKey, seed, passphrase), !!passphrase),
verify: (message: Uint8Array, signature: Uint8Array): boolean =>
naclVerify(message, signature, publicKey)
verify(message, signature)
};
}
-13
View File
@@ -1,13 +0,0 @@
// Copyright 2017-2019 @polkadot/keyring authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair$Meta } from '../types';
import { PairState } from './types';
export default function setMeta (state: PairState, meta: KeyringPair$Meta): void {
state.meta = {
...state.meta,
...meta
};
}
@@ -4,7 +4,7 @@
import testingPairs from '../testingPairs';
const keyring = testingPairs();
const keyring = testingPairs({ type: 'ed25519' });
describe('toJson', () => {
it('creates an unencoded output with no passphrase', () => {
@@ -16,7 +16,7 @@ describe('toJson', () => {
encoding: {
content: 'pkcs8',
type: 'none',
version: '0'
version: '1'
},
meta: {
name: 'alice',
@@ -34,7 +34,7 @@ describe('toJson', () => {
encoding: {
content: 'pkcs8',
type: 'xsalsa20-poly1305',
version: '0'
version: '1'
}
});
});
+7 -4
View File
@@ -2,14 +2,17 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair$Json } from '../types';
import { PairState } from './types';
import { KeyringPair$Json, KeyringPair$Meta } from '../types';
import { u8aToHex } from '@polkadot/util/index';
import { encodeAddress } from '../address';
export default function toJson ({ publicKey, meta }: PairState, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json {
type PairStateJson = KeyringPair$Meta & {
publicKey: Uint8Array
};
export default function toJson ({ publicKey, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json {
return {
address: encodeAddress(publicKey),
encoded: u8aToHex(encoded),
@@ -18,7 +21,7 @@ export default function toJson ({ publicKey, meta }: PairState, encoded: Uint8Ar
type: isEncrypted
? 'xsalsa20-poly1305'
: 'none',
version: '0'
version: '1'
},
meta
};
+3 -5
View File
@@ -2,9 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair$Meta } from '../types';
export type PairState = {
meta: KeyringPair$Meta,
publicKey: Uint8Array
export type PairInfo = {
publicKey: Uint8Array,
seed?: Uint8Array
};
+3 -18
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringInstance } from './types';
import { KeyringInstance, KeyringOptions } from './types';
import { stringToU8a } from '@polkadot/util/index';
@@ -38,24 +38,9 @@ const SEEDS: { [index: string ]: Uint8Array } = {
* @summary Create an instance of Keyring pre-populated with locked test accounts
* @description The test accounts (i.e. alice, bob, dave, eve, ferdie)
* are available on the dev chain and each test account is initialised with DOT funds.
* @example
* <BR>
*
* ```javascript
* import testKeyring from '@polkadot/keyring/testing';
*
* // Create an instance of Keyring that includes test accounts
* const keyring = testingPairs();
*
* // Retrieve the address of one of the test accounts
* const addressAlice = keyring.alice.address();
*
* // Retrieve the public key of one of the test accounts
* const publicKeyAlice = keyring.alice.publicKey();
* ```
*/
export default function testKeyring (): KeyringInstance {
const keyring = new Keyring();
export default function testKeyring (options: KeyringOptions): KeyringInstance {
const keyring = new Keyring(options);
Object
.keys(SEEDS)
+3 -3
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair } from './types';
import { KeyringPair, KeyringOptions } from './types';
import createKeyring from './testing';
import nobody from './pair/nobody';
@@ -11,8 +11,8 @@ type TestKeyringMap = {
[index: string]: KeyringPair
};
export default function testKeyringPairs (): TestKeyringMap {
const keyring = createKeyring();
export default function testKeyringPairs (options: KeyringOptions): TestKeyringMap {
const keyring = createKeyring(options);
const pairs = keyring.getPairs();
return pairs.reduce((result, pair) => {
+19 -8
View File
@@ -4,18 +4,29 @@
import { Prefix } from './address/types';
export type PairType = 'ed25519' | 'sr25519';
export type KeyringOptions = {
addressPrefix?: Prefix,
type?: PairType
};
export type KeyringPair$Meta = {
[index: string]: any
};
export type KeyringPair$JsonVersion = '0' | '1';
export type KeyringPair$JsonEncoding = {
content: 'pkcs8' | 'none',
type: 'xsalsa20-poly1305' | 'none',
version: KeyringPair$JsonVersion
};
export type KeyringPair$Json = {
address: string,
encoded: string,
encoding: {
content: 'pkcs8' | 'none',
type: 'xsalsa20-poly1305' | 'none',
version: '0'
},
encoding: KeyringPair$JsonEncoding,
meta: KeyringPair$Meta
};
@@ -46,9 +57,9 @@ export interface KeyringInstance {
setAddressPrefix (prefix: Prefix): void;
addPair (pair: KeyringPair): KeyringPair;
addFromAddress (address: string | Uint8Array, meta?: KeyringPair$Meta): KeyringPair;
addFromMnemonic (mnemonic: string, meta?: KeyringPair$Meta): KeyringPair;
addFromSeed (seed: Uint8Array, meta?: KeyringPair$Meta): KeyringPair;
addFromAddress (address: string | Uint8Array, meta: KeyringPair$Meta, encoded: Uint8Array | null): KeyringPair;
addFromMnemonic (mnemonic: string, meta: KeyringPair$Meta): KeyringPair;
addFromSeed (seed: Uint8Array, meta: KeyringPair$Meta): KeyringPair;
addFromJson (pair: KeyringPair$Json): KeyringPair;
getPair (address: string | Uint8Array): KeyringPair;
getPairs (): Array<KeyringPair>;
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/trie-codec",
"version": "0.33.36",
"version": "0.34.0",
"description": "This is an implementation of the codec for Substrate Trie",
"main": "index.js",
"keywords": [
@@ -29,6 +29,6 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-codec#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/util": "^0.33.36"
"@polkadot/util": "^0.34.0"
}
}
+8 -8
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/trie-db",
"version": "0.33.36",
"version": "0.34.0",
"description": "This is an implementation of the modified merkle patricia tree as specified in Ethereum's yellow paper, adapted for Polkadot",
"main": "index.js",
"keywords": [
@@ -29,15 +29,15 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-db#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/db": "^0.33.36",
"@polkadot/trie-codec": "^0.33.36",
"@polkadot/trie-hash": "^0.33.36",
"@polkadot/util": "^0.33.36",
"@polkadot/util-crypto": "^0.33.36"
"@polkadot/db": "^0.34.0",
"@polkadot/trie-codec": "^0.34.0",
"@polkadot/trie-hash": "^0.34.0",
"@polkadot/util": "^0.34.0",
"@polkadot/util-crypto": "^0.34.0"
},
"devDependencies": {
"@polkadot/chainspec": "^0.33.36",
"@polkadot/util-rlp": "^0.33.36",
"@polkadot/chainspec": "^0.34.0",
"@polkadot/util-rlp": "^0.34.0",
"ethereumjs-testing": "1.0.4"
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/trie-hash",
"version": "0.33.36",
"version": "0.34.0",
"description": "Generates trie roots from key-value pairs",
"main": "index.js",
"keywords": [
@@ -29,9 +29,9 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-hash#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/util": "^0.33.36"
"@polkadot/util": "^0.34.0"
},
"devDependencies": {
"@polkadot/chainspec": "^0.33.36"
"@polkadot/chainspec": "^0.34.0"
}
}
+3 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util-crypto",
"version": "0.33.36",
"version": "0.34.0",
"description": "A collection of useful crypto utilities for @polkadot",
"main": "index.js",
"keywords": [
@@ -29,8 +29,9 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/util": "^0.33.36",
"@polkadot/util": "^0.34.0",
"@types/bip39": "^2.4.1",
"@types/webassembly-js-api": "^0.0.2",
"bip39": "^2.5.0",
"blakejs": "^1.1.0",
"js-sha3": "^0.8.0",
+1
View File
@@ -7,5 +7,6 @@ export * from './keccak';
export * from './mnemonic';
export * from './nacl';
export * from './random';
export * from './schnorrkel';
export * from './sha512';
export * from './xxhash';
@@ -2,10 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeypairType } from '../../types';
import { naclKeypairFromRandom } from '../index';
describe('naclKeypairFromRandom', () => {
let keypair;
let keypair: KeypairType;
beforeEach(() => {
keypair = naclKeypairFromRandom();
@@ -7,7 +7,7 @@ import { stringToU8a } from '@polkadot/util/index';
import { naclSign, naclKeypairFromSeed } from './index';
describe('naclSign', () => {
let secretKey;
let secretKey: Uint8Array;
beforeEach(() => {
secretKey = naclKeypairFromSeed(
@@ -19,7 +19,7 @@ describe('naclSign', () => {
expect(
naclSign(
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
secretKey
{ secretKey }
)
).toEqual(
new Uint8Array([28, 58, 206, 239, 249, 70, 59, 191, 166, 40, 219, 218, 235, 170, 25, 79, 10, 94, 9, 197, 34, 126, 1, 150, 246, 68, 28, 238, 36, 26, 172, 163, 168, 90, 202, 211, 126, 246, 57, 212, 43, 24, 88, 197, 240, 113, 118, 76, 37, 81, 91, 110, 236, 50, 144, 134, 100, 223, 220, 238, 34, 185, 211, 7])
+7 -2
View File
@@ -2,7 +2,10 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeypairType } from '../types';
import nacl from 'tweetnacl';
import { assert } from '@polkadot/util/index';
/**
* @name naclSign
@@ -19,6 +22,8 @@ import nacl from 'tweetnacl';
* naclSign([...], [...]); // => [...]
* ```
*/
export default function naclSign (message: Uint8Array, secretKey: Uint8Array): Uint8Array {
return nacl.sign.detached(message, secretKey);
export default function naclSign (message: Uint8Array, { secretKey }: Partial<KeypairType>): Uint8Array {
assert(secretKey && secretKey.length === 64, 'Expected valid secretKey, 64 bytes');
return nacl.sign.detached(message, secretKey as Uint8Array);
}
@@ -7,8 +7,8 @@ import { stringToU8a } from '@polkadot/util/index';
import { naclVerify, naclKeypairFromSeed } from './index';
describe('naclSign', () => {
let publicKey;
let signature;
let publicKey: Uint8Array;
let signature: Uint8Array;
beforeEach(() => {
publicKey = naclKeypairFromSeed(
@@ -0,0 +1,8 @@
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export { default as schnorrkelKeypairFromRandom } from './keypair/fromRandom';
export { default as schnorrkelKeypairFromSeed } from './keypair/fromSeed';
export { default as schnorrkelSign } from './sign';
export { default as schnorrkelVerify } from './verify';
@@ -0,0 +1,19 @@
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { schnorrkelKeypairFromRandom } from '../index';
describe('schnorrkelKeypairFromRandom', () => {
it('generates a valid publicKey', () => {
expect(
schnorrkelKeypairFromRandom().publicKey
).toHaveLength(32);
});
it('generates a valid secretKey', () => {
expect(
schnorrkelKeypairFromRandom().secretKey
).toHaveLength(64);
});
});
@@ -0,0 +1,16 @@
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeypairType } from '../../types';
import randomAsU8a from '../../random/asU8a';
import fromSeed from './fromSeed';
/**
* @name schnorrkelKeypairFromRandom
* @description Returns a new generate object containing a `publicKey` & `secretKey`.
*/
export default function schnorrkelKeypairFromRandom (): KeypairType {
return fromSeed(randomAsU8a(32));
}
@@ -0,0 +1,21 @@
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { stringToU8a } from '@polkadot/util/index';
import { schnorrkelKeypairFromSeed } from '../index';
describe('schnorrkelKeypairFromSeed', () => {
const TEST = stringToU8a('12345678901234567890123456789012');
const RESULT = {
publicKey: new Uint8Array([116, 28, 8, 160, 111, 65, 197, 150, 96, 143, 103, 116, 37, 155, 217, 4, 51, 4, 173, 250, 93, 62, 234, 98, 118, 11, 217, 190, 151, 99, 77, 99]),
secretKey: new Uint8Array([240, 16, 102, 96, 195, 221, 162, 63, 22, 218, 169, 172, 91, 129, 27, 150, 48, 119, 245, 188, 10, 248, 159, 133, 128, 79, 13, 232, 228, 36, 240, 80, 249, 141, 102, 243, 148, 66, 80, 111, 249, 71, 253, 145, 31, 24, 199, 167, 165, 218, 99, 154, 99, 232, 211, 180, 226, 51, 247, 65, 67, 217, 81, 193])
};
it('generates a valid publicKey/secretKey pair (u8a)', () => {
expect(
schnorrkelKeypairFromSeed(TEST)
).toEqual(RESULT);
});
});
@@ -0,0 +1,23 @@
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeypairType } from '../../types';
import * as schnorrkel from '../schnorrkel-js';
const SEC_LEN = 64;
const PUB_LEN = 32;
/**
* @name schnorrkelKeypairFromSeed
* @description Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
*/
export default function schnorrkelKeypairFromSeed (seed: Uint8Array): KeypairType {
const full = schnorrkel.keypair_from_seed(seed);
return {
publicKey: full.slice(SEC_LEN, SEC_LEN + PUB_LEN),
secretKey: full.slice(0, SEC_LEN)
};
}
@@ -0,0 +1,5 @@
# schnorrkel-js
These are build outputs from https://github.com/paritytech/schnorrkel-js
To be removed once the above is published with Node/Browser/React-Native compatible versions
@@ -0,0 +1,3 @@
import './polyfill';
export * from './schnorrkel_js';
@@ -0,0 +1,13 @@
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
if (process.env.NODE_ENV === 'test') {
const crypto = require('crypto');
Object.defineProperty((global as any).self, 'crypto', {
value: {
getRandomValues: (arr: Array<number>) => crypto.randomBytes(arr.length)
}
});
}
@@ -0,0 +1,5 @@
/* tslint:disable */
export function sign(arg0: Uint8Array, arg1: Uint8Array, arg2: Uint8Array): Uint8Array;
export function verify(arg0: Uint8Array, arg1: Uint8Array, arg2: Uint8Array): boolean;
export function secret_from_seed(arg0: Uint8Array): Uint8Array;
export function keypair_from_seed(arg0: Uint8Array): Uint8Array;
@@ -0,0 +1,256 @@
/* tslint:disable */
var wasm;
let cachegetUint8Memory = null;
function getUint8Memory() {
if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) {
cachegetUint8Memory = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8Memory;
}
let WASM_VECTOR_LEN = 0;
function passArray8ToWasm(arg) {
const ptr = wasm.__wbindgen_malloc(arg.length * 1);
getUint8Memory().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
function getArrayU8FromWasm(ptr, len) {
return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);
}
let cachedGlobalArgumentPtr = null;
function globalArgumentPtr() {
if (cachedGlobalArgumentPtr === null) {
cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
}
return cachedGlobalArgumentPtr;
}
let cachegetUint32Memory = null;
function getUint32Memory() {
if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
}
return cachegetUint32Memory;
}
/**
* Sign a message
*
* The combination of both public and private key must be provided.
* This is effectively equivalent to a keypair.
*
* * public: UIntArray with 32 element
* * private: UIntArray with 64 element
* * message: Arbitrary length UIntArray
*
* * returned vector is the signature consisting of 64 bytes.
* @param {Uint8Array} arg0
* @param {Uint8Array} arg1
* @param {Uint8Array} arg2
* @returns {Uint8Array}
*/
module.exports.sign = function(arg0, arg1, arg2) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm(arg1);
const len1 = WASM_VECTOR_LEN;
const ptr2 = passArray8ToWasm(arg2);
const len2 = WASM_VECTOR_LEN;
const retptr = globalArgumentPtr();
try {
wasm.sign(retptr, ptr0, len0, ptr1, len1, ptr2, len2);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
wasm.__wbindgen_free(ptr1, len1 * 1);
wasm.__wbindgen_free(ptr2, len2 * 1);
}
};
/**
* Verify a message and its corresponding against a public key;
*
* * signature: UIntArray with 64 element
* * message: Arbitrary length UIntArray
* * pubkey: UIntArray with 32 element
* @param {Uint8Array} arg0
* @param {Uint8Array} arg1
* @param {Uint8Array} arg2
* @returns {boolean}
*/
module.exports.verify = function(arg0, arg1, arg2) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm(arg1);
const len1 = WASM_VECTOR_LEN;
const ptr2 = passArray8ToWasm(arg2);
const len2 = WASM_VECTOR_LEN;
try {
return (wasm.verify(ptr0, len0, ptr1, len1, ptr2, len2)) !== 0;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
wasm.__wbindgen_free(ptr1, len1 * 1);
wasm.__wbindgen_free(ptr2, len2 * 1);
}
};
/**
* Generate a secret key (aka. private key) from a seed phrase.
*
* * seed: UIntArray with 32 element
*
* returned vector is the private key consisting of 64 bytes.
* @param {Uint8Array} arg0
* @returns {Uint8Array}
*/
module.exports.secret_from_seed = function(arg0) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const retptr = globalArgumentPtr();
try {
wasm.secret_from_seed(retptr, ptr0, len0);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
}
};
/**
* Generate a key pair. .
*
* * seed: UIntArray with 32 element
*
* returned vector is the concatenation of first the private key (64 bytes)
* followed by the public key (32) bytes.
* @param {Uint8Array} arg0
* @returns {Uint8Array}
*/
module.exports.keypair_from_seed = function(arg0) {
const ptr0 = passArray8ToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const retptr = globalArgumentPtr();
try {
wasm.keypair_from_seed(retptr, ptr0, len0);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
}
};
const TextDecoder = require('util').TextDecoder;
let cachedTextDecoder = new TextDecoder('utf-8');
function getStringFromWasm(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
}
const heap = new Array(32);
heap.fill(undefined);
heap.push(undefined, null, true, false);
let heap_next = heap.length;
function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];
heap[idx] = obj;
return idx;
}
module.exports.__wbg_new_886f15c1b20b061b = function(arg0, arg1) {
let varg0 = getStringFromWasm(arg0, arg1);
return addHeapObject(new Function(varg0));
};
function getObject(idx) { return heap[idx]; }
module.exports.__wbg_call_a2b503e0ee1234e4 = function(arg0, arg1) {
return addHeapObject(getObject(arg0).call(getObject(arg1)));
};
module.exports.__wbg_self_ddd2d80076091e5f = function(arg0) {
return addHeapObject(getObject(arg0).self);
};
module.exports.__wbg_crypto_4b7669ff1793d881 = function(arg0) {
return addHeapObject(getObject(arg0).crypto);
};
module.exports.__wbg_getRandomValues_6de85818bd2ad699 = function(arg0) {
return addHeapObject(getObject(arg0).getRandomValues);
};
module.exports.__wbg_getRandomValues_95cef5eed1acafda = function(arg0, arg1, arg2) {
let varg1 = getArrayU8FromWasm(arg1, arg2);
getObject(arg0).getRandomValues(varg1);
};
module.exports.__wbg_require_86edd37cfda5f13d = function(arg0, arg1) {
let varg0 = getStringFromWasm(arg0, arg1);
return addHeapObject(require(varg0));
};
module.exports.__wbg_randomFillSync_571502126f344d60 = function(arg0, arg1, arg2) {
let varg1 = getArrayU8FromWasm(arg1, arg2);
getObject(arg0).randomFillSync(varg1);
};
function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
heap_next = idx;
}
module.exports.__wbindgen_object_drop_ref = function(i) { dropObject(i); };
module.exports.__wbindgen_is_undefined = function(idx) {
return getObject(idx) === undefined ? 1 : 0;
};
module.exports.__wbindgen_jsval_eq = function(a, b) {
return getObject(a) === getObject(b) ? 1 : 0;
};
wasm = require('./schnorrkel_js_bg');
@@ -0,0 +1,9 @@
/* tslint:disable */
export const memory: WebAssembly.Memory;
export function sign(a: number, b: number, c: number, d: number, e: number, f: number, g: number): void;
export function verify(a: number, b: number, c: number, d: number, e: number, f: number): number;
export function secret_from_seed(a: number, b: number, c: number): void;
export function keypair_from_seed(a: number, b: number, c: number): void;
export function __wbindgen_global_argument_ptr(): number;
export function __wbindgen_malloc(a: number): number;
export function __wbindgen_free(a: number, b: number): void;
File diff suppressed because one or more lines are too long
@@ -0,0 +1,20 @@
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeypairType } from '../types';
import { assert } from '@polkadot/util/index';
import * as schnorrkel from './schnorrkel-js';
/**
* @name schnorrkelSign
* @description Returns message signature of `message`, using the supplied pair
*/
export default function schnorrkelSign (message: Uint8Array, { publicKey, secretKey }: Partial<KeypairType>): Uint8Array {
assert(publicKey && publicKey.length === 32, 'Expected valid publicKey, 32-bytes');
assert(secretKey && secretKey.length === 64, 'Expected valid secretKey, 364-bytes');
return schnorrkel.sign(publicKey as Uint8Array, secretKey as Uint8Array, message);
}
@@ -0,0 +1,26 @@
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { stringToU8a } from '@polkadot/util/index';
import pairFromRandom from './keypair/fromRandom';
import sign from './sign';
import verify from './verify';
const MESSAGE = stringToU8a('this is a message');
describe('sign and verify', () => {
it('has 64-byte signatures', () => {
const pair = pairFromRandom();
expect(sign(MESSAGE, pair)).toHaveLength(64);
});
it('can sign and verify a message', () => {
const pair = pairFromRandom();
const signature = sign(MESSAGE, pair);
expect(verify(MESSAGE, signature, pair.publicKey)).toBe(true);
});
});
@@ -0,0 +1,13 @@
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import * as schnorrkel from './schnorrkel-js';
/**
* @name schnorrkelVerify
* @description Verifies the signature of `message`, using the supplied pair
*/
export default function schnorrkelVerify (message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean {
return schnorrkel.verify(signature, message, publicKey);
}
+4 -3
View File
@@ -2,6 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { SignKeyPair } from 'tweetnacl';
export type KeypairType = SignKeyPair;
export type KeypairType = {
publicKey: Uint8Array;
secretKey: Uint8Array;
};
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util-rlp",
"version": "0.33.36",
"version": "0.34.0",
"description": "RLP encoding and decoding",
"main": "index.js",
"keywords": [
@@ -31,7 +31,7 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-rlp#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/util": "^0.33.36"
"@polkadot/util": "^0.34.0"
},
"devDependencies": {
"ethereumjs-testing": "1.0.4"
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util",
"version": "0.33.36",
"version": "0.34.0",
"description": "A collection of useful utilities for @polkadot",
"main": "index.js",
"keywords": [
+5
View File
@@ -1459,6 +1459,11 @@
dependencies:
"@types/node" "*"
"@types/webassembly-js-api@^0.0.2":
version "0.0.2"
resolved "https://registry.yarnpkg.com/@types/webassembly-js-api/-/webassembly-js-api-0.0.2.tgz#43a04bd75fa20332133c6c3986156bfeb4a3ced7"
integrity sha512-htlxJRag6RUiMYUkS8Fjup+TMHO0VarpiF9MrqYaGJ0wXtIraQFz40rfA8VIeCiWy8sgpv3RLmigpgicG8fqGA==
"@types/xxhashjs@^0.1.1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@types/xxhashjs/-/xxhashjs-0.1.1.tgz#980709096c7138713b7777f49dccd4b8f93908e5"