Add keyring publicKey <-> address (#122)
* Add keyring publicKey <-> address * Add isObservable utility
This commit is contained in:
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
// @flow
|
||||
|
||||
declare module 'bs58' {
|
||||
declare module.exports: {
|
||||
decode: (input: string) => Buffer,
|
||||
encode: (input: Buffer | Array<number>) => string
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -10,7 +10,7 @@
|
||||
"test": "jest --coverage"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/dev": "^0.17.12",
|
||||
"@polkadot/dev": "^0.17.13",
|
||||
"lerna": "^2.5.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
"dependencies": {
|
||||
"@polkadot/util": "^0.19.4",
|
||||
"@polkadot/util-crypto": "^0.19.4",
|
||||
"babel-runtime": "^6.26.0"
|
||||
"babel-runtime": "^6.26.0",
|
||||
"bs58": "^4.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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
|
||||
|
||||
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L6
|
||||
|
||||
const bs58 = require('bs58');
|
||||
const assert = require('@polkadot/util/assert');
|
||||
const bufferToU8a = require('@polkadot/util/buffer/toU8a');
|
||||
const blake2b = require('@polkadot/util-crypto/blake2/asU8a512');
|
||||
|
||||
module.exports = function decode (encoded: string): Uint8Array {
|
||||
const decoded = bufferToU8a(bs58.decode(encoded));
|
||||
|
||||
assert(decoded[0] === 42, 'Invalid decoded address prefix');
|
||||
assert(decoded.length === 32 + 1 + 2, 'Invalid decoded address length');
|
||||
|
||||
const hash = blake2b(decoded.subarray(0, 33));
|
||||
|
||||
assert(decoded[33] === hash[0] && decoded[34] === hash[1], 'Invalid decoded address checksum');
|
||||
|
||||
return decoded.slice(1, 33);
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
// 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
|
||||
|
||||
const keyring = require('../testingPairs')();
|
||||
const decode = require('./decode');
|
||||
|
||||
describe('decode', () => {
|
||||
it('decodes an address', () => {
|
||||
expect(
|
||||
decode('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ')
|
||||
).toEqual(
|
||||
keyring.alice.publicKey()
|
||||
);
|
||||
});
|
||||
|
||||
it('fails when prefix is invalid', () => {
|
||||
expect(
|
||||
() => decode('BXHKtJjKdXjvbvTg6qK275sgcKKq8dnU38MQQAiR3LN2stP')
|
||||
).toThrow(/address prefix/);
|
||||
});
|
||||
|
||||
it('fails when length is invalid', () => {
|
||||
expect(
|
||||
() => decode('y9EMHt34JJo4rWLSaxoLGdYXvjgSXEd4zHUnQgfNzwES8b')
|
||||
).toThrow(/address length/);
|
||||
});
|
||||
|
||||
it('fails when the checksum does not match', () => {
|
||||
expect(
|
||||
() => decode('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMa9cj')
|
||||
).toThrow(/address checksum/);
|
||||
expect(
|
||||
() => decode('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDwU')
|
||||
).toThrow(/address checksum/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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
|
||||
|
||||
// Original implementation: https://github.com/paritytech/polka-ui/blob/4858c094684769080f5811f32b081dd7780b0880/src/polkadot.js#L34
|
||||
|
||||
const bs58 = require('bs58');
|
||||
const assert = require('@polkadot/util/assert');
|
||||
const u8aConcat = require('@polkadot/util/u8a/concat');
|
||||
const u8aToBuffer = require('@polkadot/util/u8a/toBuffer');
|
||||
const u8aToU8a = require('@polkadot/util/u8a/toU8a');
|
||||
const blake2b = require('@polkadot/util-crypto/blake2/asU8a512');
|
||||
|
||||
const PREFIX = new Uint8Array([42]);
|
||||
|
||||
module.exports = function encode (_publicKey: Uint8Array | string): string {
|
||||
const publicKey = u8aToU8a(_publicKey);
|
||||
|
||||
assert(publicKey.length === 32, `Expected a valid publicKey to convert`);
|
||||
|
||||
const input = u8aConcat(PREFIX, publicKey);
|
||||
const hash = blake2b(input);
|
||||
|
||||
return bs58.encode(
|
||||
u8aToBuffer(
|
||||
u8aConcat(input, hash.subarray(0, 2))
|
||||
)
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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
|
||||
|
||||
const keyring = require('../testingPairs')();
|
||||
const encode = require('./encode');
|
||||
|
||||
describe('encode', () => {
|
||||
it('encodes an address to a valid value', () => {
|
||||
expect(
|
||||
keyring.alice.address()
|
||||
).toEqual('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ');
|
||||
});
|
||||
|
||||
it('fails when non-valid publicKey provided', () => {
|
||||
expect(
|
||||
() => encode(
|
||||
keyring.alice.publicKey().slice(0, 30)
|
||||
)
|
||||
).toThrow(/Expected a valid publicKey/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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
|
||||
|
||||
const decodeAddress = require('./decode');
|
||||
const encodeAddress = require('./encode');
|
||||
|
||||
module.exports = {
|
||||
decodeAddress,
|
||||
encodeAddress
|
||||
};
|
||||
@@ -7,6 +7,7 @@ import type { KeyringInstance, KeyringPair } from './types';
|
||||
|
||||
const naclKeypairFromSeed = require('@polkadot/util-crypto/nacl/keypair/fromSeed');
|
||||
|
||||
const addressDecode = require('./address/decode');
|
||||
const decrypt = require('./decrypt');
|
||||
const encrypt = require('./encrypt');
|
||||
const createPair = require('./pair');
|
||||
@@ -22,6 +23,10 @@ module.exports = function keyring (): KeyringInstance {
|
||||
pairs.add(decrypt(encrypted, passphrase)),
|
||||
encrypt: (publicKey: Uint8Array, passphrase: Uint8Array | string): Uint8Array =>
|
||||
encrypt(pairs.get(publicKey), passphrase),
|
||||
getAddress: (address: string): KeyringPair =>
|
||||
pairs.get(
|
||||
addressDecode(address)
|
||||
),
|
||||
getPair: (publicKey: Uint8Array): KeyringPair =>
|
||||
pairs.get(publicKey),
|
||||
getPairs: pairs.all,
|
||||
|
||||
@@ -10,6 +10,7 @@ import type { PairState } from './types';
|
||||
const naclSign = require('@polkadot/util-crypto/nacl/sign');
|
||||
const naclVerify = require('@polkadot/util-crypto/nacl/verify');
|
||||
|
||||
const encodeAddress = require('../address/encode');
|
||||
const decode = require('./decode');
|
||||
const encode = require('./encode');
|
||||
const getMeta = require('./getMeta');
|
||||
@@ -17,15 +18,19 @@ const setMeta = require('./setMeta');
|
||||
|
||||
module.exports = function pair ({ publicKey, secretKey }: KeypairType): KeyringPair {
|
||||
const state: PairState = {
|
||||
address: encodeAddress(publicKey),
|
||||
meta: {}
|
||||
};
|
||||
|
||||
return {
|
||||
address: (): string =>
|
||||
state.address,
|
||||
decodePkcs8: (encoded: Uint8Array, passphrase?: Uint8Array | string): void => {
|
||||
const decoded = decode(encoded, passphrase);
|
||||
|
||||
publicKey = decoded.publicKey;
|
||||
secretKey = decoded.secretKey;
|
||||
state.address = encodeAddress(publicKey);
|
||||
},
|
||||
encodePkcs8: (passphrase?: Uint8Array | string): Uint8Array =>
|
||||
encode(secretKey, passphrase),
|
||||
|
||||
@@ -6,5 +6,6 @@
|
||||
import type { KeyringPair$Meta } from '../types';
|
||||
|
||||
export type PairState = {
|
||||
address: string,
|
||||
meta: KeyringPair$Meta
|
||||
};
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { KeyringPair } from './types';
|
||||
import type { KeyringPair, KeyringInstance } from './types';
|
||||
|
||||
type TestKeyringMap = {
|
||||
_keyring: KeyringInstance,
|
||||
[string]: KeyringPair
|
||||
};
|
||||
|
||||
@@ -21,5 +22,5 @@ module.exports = function testKeyringPairs (): TestKeyringMap {
|
||||
result[(name: string)] = pair;
|
||||
|
||||
return result;
|
||||
}, {});
|
||||
}, { _keyring: keyring });
|
||||
};
|
||||
|
||||
@@ -9,6 +9,7 @@ export type KeyringPair$Meta = {
|
||||
}
|
||||
|
||||
export type KeyringPair = {
|
||||
address: () => string,
|
||||
decodePkcs8: (encoded: Uint8Array, passphrase: Uint8Array | string) => void,
|
||||
encodePkcs8: (passphrase: Uint8Array | string) => Uint8Array,
|
||||
getMeta: () => KeyringPair$Meta,
|
||||
@@ -28,6 +29,7 @@ export type KeyringInstance = {
|
||||
addFromSeed (seed: Uint8Array): KeyringPair,
|
||||
decrypt (encrypted: Uint8Array, secret: Uint8Array | string): KeyringPair,
|
||||
encrypt (publicKey: Uint8Array, secret: Uint8Array | string): Uint8Array,
|
||||
getAddress (address: string): KeyringPair,
|
||||
getPair (publicKey: Uint8Array): KeyringPair,
|
||||
getPairs (): Array<KeyringPair>,
|
||||
getPublicKeys (): Array<Uint8Array>
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
"test": "echo \"Tests only available from root wrapper\""
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/api-jsonrpc": "^0.9.5"
|
||||
"@polkadot/api-jsonrpc": "^0.9.15"
|
||||
},
|
||||
"dependencies": {
|
||||
"babel-runtime": "^6.26.0",
|
||||
|
||||
@@ -6,17 +6,17 @@
|
||||
const BN = require('bn.js');
|
||||
|
||||
/**
|
||||
@name isBN
|
||||
@name isBn
|
||||
@signature isBN (value: mixed): boolean
|
||||
@summary Tests for a `BN` object instance.
|
||||
@description
|
||||
Checks to see if the input object is an instance of `BN` (bn.js).
|
||||
@example
|
||||
import BN from 'bn.js';
|
||||
import { isBN } from '@polkadot/util';
|
||||
import { isBn } from '@polkadot/util';
|
||||
|
||||
console.log('isBN', isBN(new BN(1))); // => true
|
||||
console.log('isBn', isBn(new BN(1))); // => true
|
||||
*/
|
||||
module.exports = function isBN (value: mixed): boolean {
|
||||
module.exports = function isBn (value: mixed): boolean {
|
||||
return BN.isBN(value);
|
||||
};
|
||||
|
||||
@@ -4,18 +4,18 @@
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const { isBN } = require('./index');
|
||||
const { isBn } = require('./index');
|
||||
|
||||
describe('isBN', () => {
|
||||
it('returns true when a BN value', () => {
|
||||
expect(
|
||||
isBN(new BN(123))
|
||||
isBn(new BN(123))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-BN values', () => {
|
||||
expect(
|
||||
isBN(0)
|
||||
isBn(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const isBN = require('./bn');
|
||||
const isBn = require('./bn');
|
||||
const isBuffer = require('./buffer');
|
||||
const isBoolean = require('./boolean');
|
||||
const isError = require('./error');
|
||||
@@ -14,6 +14,7 @@ const isIp = require('./ip');
|
||||
const isNull = require('./null');
|
||||
const isNumber = require('./number');
|
||||
const isObject = require('./object');
|
||||
const isObservable = require('./observable');
|
||||
const isString = require('./string');
|
||||
const isU8a = require('./u8a');
|
||||
const isUndefined = require('./undefined');
|
||||
@@ -22,7 +23,7 @@ const isUndefined = require('./undefined');
|
||||
@summary Type checking utilities
|
||||
*/
|
||||
module.exports = {
|
||||
isBN,
|
||||
isBn,
|
||||
isBoolean,
|
||||
isBuffer,
|
||||
isError,
|
||||
@@ -31,6 +32,7 @@ module.exports = {
|
||||
isInstanceOf,
|
||||
isIp,
|
||||
isObject,
|
||||
isObservable,
|
||||
isNull,
|
||||
isNumber,
|
||||
isString,
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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
|
||||
|
||||
const isFunction = require('./function');
|
||||
const isObject = require('./object');
|
||||
|
||||
/**
|
||||
@name isBObservable
|
||||
@signature isObservable (value: mixed): boolean
|
||||
@summary Tests for a `Observable` object instance.
|
||||
@description
|
||||
Checks to see if the input object is an instance of `BN` (bn.js).
|
||||
@example
|
||||
import { isObservable } from '@polkadot/util';
|
||||
|
||||
console.log('isObservable', isObservable(...));
|
||||
*/
|
||||
module.exports = function isObservable (value: mixed): boolean {
|
||||
// $FlowFixMe check for possibly non-existent .next
|
||||
return isObject(value) && isFunction(value.next);
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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 { isObservable } = require('./index');
|
||||
|
||||
describe('isObservable', () => {
|
||||
it('returns true on valid observables', () => {
|
||||
expect(
|
||||
isObservable({ next: () => true })
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid objects', () => {
|
||||
expect(
|
||||
isObservable('notAnObservable')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on invalid next functions', () => {
|
||||
expect(
|
||||
isObservable({ next: true })
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -79,15 +79,15 @@
|
||||
lodash "^4.2.0"
|
||||
to-fast-properties "^2.0.0"
|
||||
|
||||
"@polkadot/api-jsonrpc@^0.9.5":
|
||||
version "0.9.5"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.9.5.tgz#67cd9568ea25b7c1b7f1d1f3b0b7faa9f733ad25"
|
||||
"@polkadot/api-jsonrpc@^0.9.15":
|
||||
version "0.9.15"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.9.15.tgz#e210f5ce82d7c41c11ddf3cd83e521ede9f1ddae"
|
||||
dependencies:
|
||||
babel-runtime "^6.26.0"
|
||||
|
||||
"@polkadot/dev@^0.17.12":
|
||||
version "0.17.12"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.17.12.tgz#90d0d302681c5eae4683eea75949548a99e52831"
|
||||
"@polkadot/dev@^0.17.13":
|
||||
version "0.17.13"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.17.13.tgz#cd5690d4c45abcc1e4b2624944c79229a82930e2"
|
||||
dependencies:
|
||||
babel-cli "^6.26.0"
|
||||
babel-core "^6.26.0"
|
||||
@@ -112,7 +112,7 @@
|
||||
eslint-plugin-promise "^3.6.0"
|
||||
eslint-plugin-react "^7.7.0"
|
||||
eslint-plugin-standard "^3.0.1"
|
||||
flow-bin "^0.70.0"
|
||||
flow-bin "^0.71.0"
|
||||
flow-copy-source "^1.3.0"
|
||||
jest "^22.4.2"
|
||||
makeshift "^1.1.0"
|
||||
@@ -970,6 +970,12 @@ balanced-match@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
|
||||
|
||||
base-x@^3.0.2:
|
||||
version "3.0.4"
|
||||
resolved "https://registry.yarnpkg.com/base-x/-/base-x-3.0.4.tgz#94c1788736da065edb1d68808869e357c977fa77"
|
||||
dependencies:
|
||||
safe-buffer "^5.0.1"
|
||||
|
||||
base@^0.11.1:
|
||||
version "0.11.2"
|
||||
resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f"
|
||||
@@ -1086,6 +1092,12 @@ browserslist@^3.0.0:
|
||||
caniuse-lite "^1.0.30000809"
|
||||
electron-to-chromium "^1.3.33"
|
||||
|
||||
bs58@^4.0.1:
|
||||
version "4.0.1"
|
||||
resolved "https://registry.yarnpkg.com/bs58/-/bs58-4.0.1.tgz#be161e76c354f6f788ae4071f63f34e8c4f0a42a"
|
||||
dependencies:
|
||||
base-x "^3.0.2"
|
||||
|
||||
bser@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/bser/-/bser-2.0.0.tgz#9ac78d3ed5d915804fd87acb158bc797147a1719"
|
||||
@@ -2362,9 +2374,9 @@ flat-cache@^1.2.1:
|
||||
graceful-fs "^4.1.2"
|
||||
write "^0.2.1"
|
||||
|
||||
flow-bin@^0.70.0:
|
||||
version "0.70.0"
|
||||
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.70.0.tgz#080ae83a997f2b4ddb3dc2649bf13336825292b5"
|
||||
flow-bin@^0.71.0:
|
||||
version "0.71.0"
|
||||
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.71.0.tgz#fd1b27a6458c3ebaa5cb811853182ed631918b70"
|
||||
|
||||
flow-copy-source@^1.3.0:
|
||||
version "1.3.0"
|
||||
|
||||
Reference in New Issue
Block a user