@polkadot/keyring
Advanced tools
Comparing version 0.34.5 to 0.34.6
@@ -1,2 +0,2 @@ | ||
import { KeyringInstance, KeyringPair, KeyringPair$Json, KeyringPair$Meta, KeyringOptions, PairType } from './types'; | ||
import { KeyringInstance, KeyringPair, KeyringPair$Json, KeyringPair$Meta, KeyringOptions, KeyringPairType } from './types'; | ||
import { decodeAddress, encodeAddress, setAddressPrefix } from './address'; | ||
@@ -29,3 +29,3 @@ /** | ||
*/ | ||
readonly type: PairType; | ||
readonly type: KeyringPairType; | ||
/** | ||
@@ -44,3 +44,3 @@ * @name addPair | ||
*/ | ||
addFromAddress(address: string | Uint8Array, meta: KeyringPair$Meta, encoded: Uint8Array | null): KeyringPair; | ||
addFromAddress(address: string | Uint8Array, meta: KeyringPair$Meta, encoded: Uint8Array | null, type?: KeyringPairType): KeyringPair; | ||
/** | ||
@@ -53,3 +53,3 @@ * @name addFromJson | ||
*/ | ||
addFromJson({ address, encoded, meta }: KeyringPair$Json): KeyringPair; | ||
addFromJson({ address, encoded, encoding: { type, version }, meta }: KeyringPair$Json): KeyringPair; | ||
/** | ||
@@ -63,3 +63,3 @@ * @name addFromMnemonic | ||
*/ | ||
addFromMnemonic(mnemonic: string, meta: KeyringPair$Meta): KeyringPair; | ||
addFromMnemonic(mnemonic: string, meta: KeyringPair$Meta, type?: KeyringPairType): KeyringPair; | ||
/** | ||
@@ -72,3 +72,3 @@ * @name addFromSeed | ||
*/ | ||
addFromSeed(seed: Uint8Array, meta: KeyringPair$Meta): KeyringPair; | ||
addFromSeed(seed: Uint8Array, meta: KeyringPair$Meta, type?: KeyringPairType): KeyringPair; | ||
/** | ||
@@ -75,0 +75,0 @@ * @name getPair |
17
index.js
@@ -99,3 +99,4 @@ "use strict"; | ||
addFromAddress(address, meta, encoded) { | ||
return this.addPair((0, _pair.default)(this._type, { | ||
let type = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : this.type; | ||
return this.addPair((0, _pair.default)(type, { | ||
publicKey: this.decodeAddress(address) | ||
@@ -116,4 +117,8 @@ }, meta, encoded)); | ||
encoded = _ref.encoded, | ||
_ref$encoding = _ref.encoding, | ||
type = _ref$encoding.type, | ||
version = _ref$encoding.version, | ||
meta = _ref.meta; | ||
return this.addFromAddress(address, meta, (0, _index.hexToU8a)(encoded)); | ||
const keytype = version === '0' || !Array.isArray(type) ? this.type : type[1]; | ||
return this.addFromAddress(address, meta, (0, _index.hexToU8a)(encoded), keytype); | ||
} | ||
@@ -131,3 +136,4 @@ /** | ||
addFromMnemonic(mnemonic, meta) { | ||
return this.addFromSeed((0, _index2.mnemonicToSeed)(mnemonic), meta); | ||
let type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.type; | ||
return this.addFromSeed((0, _index2.mnemonicToSeed)(mnemonic), meta, type); | ||
} | ||
@@ -144,4 +150,5 @@ /** | ||
addFromSeed(seed, meta) { | ||
const keypair = this._type === 'sr25519' ? (0, _index2.schnorrkelKeypairFromSeed)(seed) : (0, _index2.naclKeypairFromSeed)(seed); | ||
return this.addPair((0, _pair.default)(this._type, (0, _objectSpread2.default)({}, keypair, { | ||
let type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.type; | ||
const keypair = type === 'sr25519' ? (0, _index2.schnorrkelKeypairFromSeed)(seed) : (0, _index2.naclKeypairFromSeed)(seed); | ||
return this.addPair((0, _pair.default)(type, (0, _objectSpread2.default)({}, keypair, { | ||
seed | ||
@@ -148,0 +155,0 @@ }), meta, null)); |
@@ -26,3 +26,2 @@ "use strict"; | ||
keypair.addFromSeed(seedOne, {}); | ||
(0, _setPrefix.default)(42); | ||
}); | ||
@@ -46,3 +45,61 @@ it('adds the pair', () => { | ||
}); | ||
it('signs and verifies', () => { | ||
const MESSAGE = (0, _index.stringToU8a)('this is a message'); | ||
const pair = keypair.getPair(publicKeyOne); | ||
const signature = pair.sign(MESSAGE); | ||
expect(pair.verify(MESSAGE, signature)).toBe(true); | ||
}); | ||
it('converts, signs and verifies', () => { | ||
const MESSAGE = (0, _index.stringToU8a)('this is a message'); | ||
const pair = keypair.getPair(publicKeyOne).toType('sr25519'); | ||
const signature = pair.sign(MESSAGE); | ||
expect(pair.type).toBe('sr25519'); | ||
expect(pair.publicKey()).toEqual(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])); | ||
expect(pair.verify(MESSAGE, signature)).toBe(true); | ||
}); | ||
}); | ||
describe('sr25519', () => { | ||
const publicKeyOne = 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]); | ||
const publicKeyTwo = (0, _index.hexToU8a)('0x44a996beb1eef7bdcab976ab6d2ca26104834164ecf28fb375600576fcc6eb0f'); | ||
const seedOne = (0, _index.stringToU8a)('12345678901234567890123456789012'); | ||
const seedTwo = (0, _index.hexToU8a)('0x9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60'); | ||
let keypair; | ||
beforeEach(() => { | ||
keypair = new _index2.default({ | ||
type: 'sr25519' | ||
}); | ||
keypair.addFromSeed(seedOne, {}); | ||
}); | ||
it('adds the pair', () => { | ||
expect(keypair.addFromSeed(seedTwo, {}).publicKey()).toEqual(publicKeyTwo); | ||
}); | ||
it('adds from a mnemonic', () => { | ||
(0, _setPrefix.default)(68); | ||
expect(keypair.addFromMnemonic('moral movie very draw assault whisper awful rebuild speed purity repeat card', {}).address()).toEqual('7nmZ1fkYHpnEz6tqR9cLkmwYRbqda9wPsiDq56JhgmNXqGjt'); | ||
}); | ||
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])); | ||
}); | ||
it('signs and verifies', () => { | ||
const MESSAGE = (0, _index.stringToU8a)('this is a message'); | ||
const pair = keypair.getPair(publicKeyOne); | ||
const signature = pair.sign(MESSAGE); | ||
expect(pair.verify(MESSAGE, signature)).toBe(true); | ||
}); | ||
it('converts, signs and verifies', () => { | ||
const MESSAGE = (0, _index.stringToU8a)('this is a message'); | ||
const pair = keypair.getPair(publicKeyOne).toType('ed25519'); | ||
const signature = pair.sign(MESSAGE); | ||
expect(pair.type).toBe('ed25519'); | ||
expect(pair.publicKey()).toEqual(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])); | ||
expect(pair.verify(MESSAGE, signature)).toBe(true); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "@polkadot/keyring", | ||
"version": "0.34.5", | ||
"version": "0.34.6", | ||
"description": "Keyring management", | ||
@@ -34,4 +34,4 @@ "main": "index.js", | ||
"@babel/runtime": "^7.2.0", | ||
"@polkadot/util": "^0.34.5", | ||
"@polkadot/util-crypto": "^0.34.5", | ||
"@polkadot/util": "^0.34.6", | ||
"@polkadot/util-crypto": "^0.34.6", | ||
"@types/bs58": "^4.0.0", | ||
@@ -38,0 +38,0 @@ "bs58": "^4.0.1" |
@@ -1,3 +0,3 @@ | ||
declare type DecodeResult = { | ||
publicKey: Uint8Array; | ||
import { PairInfo } from './types'; | ||
declare type DecodeResult = PairInfo & { | ||
seed: Uint8Array; | ||
@@ -4,0 +4,0 @@ }; |
@@ -1,1 +0,2 @@ | ||
export default function encode(publicKey: Uint8Array, seed?: Uint8Array | null, passphrase?: string): Uint8Array; | ||
import { PairInfo } from './types'; | ||
export default function encode({ publicKey, seed }: PairInfo, passphrase?: string): Uint8Array; |
@@ -17,3 +17,5 @@ "use strict"; | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
function encode(publicKey, seed, passphrase) { | ||
function encode(_ref, passphrase) { | ||
let publicKey = _ref.publicKey, | ||
seed = _ref.seed; | ||
(0, _index.assert)(seed, 'Expected a valid seed to be passed to encode'); | ||
@@ -20,0 +22,0 @@ const encoded = (0, _index.u8aConcat)(_defaults.PKCS8_HEADER, seed, _defaults.PKCS8_DIVIDER, publicKey); |
@@ -1,2 +0,2 @@ | ||
import { KeyringPair, KeyringPair$Meta, PairType } from '../types'; | ||
import { KeyringPair, KeyringPair$Meta, KeyringPairType } from '../types'; | ||
import { PairInfo } from './types'; | ||
@@ -34,2 +34,2 @@ /** | ||
*/ | ||
export default function pair(type: PairType, { publicKey, seed }: PairInfo, meta: KeyringPair$Meta | undefined, encoded: Uint8Array | null): KeyringPair; | ||
export default function createPair(type: KeyringPairType, { publicKey, seed }: PairInfo, meta: KeyringPair$Meta | undefined, encoded: Uint8Array | null): KeyringPair; |
@@ -8,3 +8,3 @@ "use strict"; | ||
}); | ||
exports.default = pair; | ||
exports.default = createPair; | ||
@@ -24,3 +24,7 @@ var _index = require("@polkadot/util-crypto/index"); | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
const fromSeed = (type, seed) => type === 'sr25519' ? (0, _index.schnorrkelKeypairFromSeed)(seed) : (0, _index.naclKeypairFromSeed)(seed); | ||
const _sign = (type, message, pair) => type === 'sr25519' ? (0, _index.schnorrkelSign)(message, pair) : (0, _index.naclSign)(message, pair); | ||
const _verify = (type, message, signature, publicKey) => type === 'sr25519' ? (0, _index.schnorrkelVerify)(message, signature, publicKey) : (0, _index.naclVerify)(message, signature, publicKey); | ||
/** | ||
@@ -57,3 +61,5 @@ * @name pair | ||
*/ | ||
function pair(type, _ref) { | ||
function createPair(type, _ref) { | ||
let _publicKey = _ref.publicKey, | ||
@@ -63,11 +69,12 @@ seed = _ref.seed; | ||
let encoded = arguments.length > 3 ? arguments[3] : undefined; | ||
let secretKey; | ||
const fromSeed = seed => type === 'sr25519' ? (0, _index.schnorrkelKeypairFromSeed)(seed) : (0, _index.naclKeypairFromSeed)(seed); | ||
if (seed) { | ||
const pair = fromSeed(type, seed); | ||
_publicKey = pair.publicKey; | ||
secretKey = pair.secretKey; | ||
} | ||
const _sign = (message, pair) => type === 'sr25519' ? (0, _index.schnorrkelSign)(message, pair) : (0, _index.naclSign)(message, pair); | ||
const _verify = (message, signature) => type === 'sr25519' ? (0, _index.schnorrkelVerify)(message, signature, _publicKey) : (0, _index.naclVerify)(message, signature, _publicKey); | ||
let secretKey = seed ? fromSeed(seed).secretKey : undefined; | ||
return { | ||
type, | ||
address: () => (0, _address.encodeAddress)(_publicKey), | ||
@@ -78,5 +85,8 @@ decodePkcs8: (passphrase, _encoded) => { | ||
seed = decoded.seed; | ||
secretKey = fromSeed(seed).secretKey; | ||
secretKey = fromSeed(type, seed).secretKey; | ||
}, | ||
encodePkcs8: passphrase => (0, _encode.default)(_publicKey, seed, passphrase), | ||
encodePkcs8: passphrase => (0, _encode.default)({ | ||
publicKey: _publicKey, | ||
seed | ||
}, passphrase), | ||
getMeta: () => meta, | ||
@@ -91,12 +101,19 @@ isLocked: () => !secretKey || secretKey.length === 0, | ||
}, | ||
sign: message => _sign(message, { | ||
sign: message => _sign(type, message, { | ||
publicKey: _publicKey, | ||
secretKey | ||
}), | ||
toJson: passphrase => (0, _toJson2.default)({ | ||
toJson: passphrase => (0, _toJson2.default)(type, { | ||
meta, | ||
publicKey: _publicKey | ||
}, (0, _encode.default)(_publicKey, seed, passphrase), !!passphrase), | ||
verify: (message, signature) => _verify(message, signature) | ||
}, (0, _encode.default)({ | ||
publicKey: _publicKey, | ||
seed | ||
}, passphrase), !!passphrase), | ||
toType: type => createPair(type, { | ||
publicKey: _publicKey, | ||
seed | ||
}, meta, null), | ||
verify: (message, signature) => _verify(type, message, signature, _publicKey) | ||
}; | ||
} |
@@ -26,3 +26,3 @@ "use strict"; | ||
encoding: { | ||
content: 'none', | ||
content: ['pkcs8', 'ed25519'], | ||
type: 'none', | ||
@@ -36,2 +36,3 @@ version: '0' | ||
return { | ||
type: 'ed25519', | ||
address: () => _address, | ||
@@ -48,4 +49,5 @@ decodePkcs8: (passphrase, encoded) => undefined, | ||
toJson: passphrase => json, | ||
toType: () => everybody(), | ||
verify: (message, signature) => false | ||
}; | ||
} |
@@ -1,6 +0,6 @@ | ||
import { KeyringPair$Json, KeyringPair$Meta } from '../types'; | ||
import { KeyringPair$Json, KeyringPair$Meta, KeyringPairType } from '../types'; | ||
declare type PairStateJson = KeyringPair$Meta & { | ||
publicKey: Uint8Array; | ||
}; | ||
export default function toJson({ publicKey, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json; | ||
export default function toJson(type: KeyringPairType, { publicKey, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json; | ||
export {}; |
@@ -15,3 +15,3 @@ "use strict"; | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
function toJson(_ref, encoded, isEncrypted) { | ||
function toJson(type, _ref, encoded, isEncrypted) { | ||
let publicKey = _ref.publicKey, | ||
@@ -23,3 +23,3 @@ meta = _ref.meta; | ||
encoding: { | ||
content: 'pkcs8', | ||
content: ['pkcs8', type], | ||
type: isEncrypted ? 'xsalsa20-poly1305' : 'none', | ||
@@ -26,0 +26,0 @@ version: '1' |
@@ -19,3 +19,3 @@ "use strict"; | ||
encoding: { | ||
content: 'pkcs8', | ||
content: ['pkcs8', 'ed25519'], | ||
type: 'none', | ||
@@ -36,3 +36,3 @@ version: '1' | ||
encoding: { | ||
content: 'pkcs8', | ||
content: ['pkcs8', 'ed25519'], | ||
type: 'xsalsa20-poly1305', | ||
@@ -39,0 +39,0 @@ version: '1' |
export declare type PairInfo = { | ||
publicKey: Uint8Array; | ||
seed?: Uint8Array; | ||
seed?: Uint8Array | null; | ||
}; |
import { Prefix } from './address/types'; | ||
export declare type PairType = 'ed25519' | 'sr25519'; | ||
export declare type KeyringPairType = 'ed25519' | 'sr25519'; | ||
export declare type KeyringOptions = { | ||
addressPrefix?: Prefix; | ||
type?: PairType; | ||
type?: KeyringPairType; | ||
}; | ||
@@ -12,3 +12,3 @@ export declare type KeyringPair$Meta = { | ||
export declare type KeyringPair$JsonEncoding = { | ||
content: 'pkcs8' | 'none'; | ||
content: ['pkcs8', KeyringPairType]; | ||
type: 'xsalsa20-poly1305' | 'none'; | ||
@@ -23,3 +23,4 @@ version: KeyringPair$JsonVersion; | ||
}; | ||
export declare type KeyringPair = { | ||
export interface KeyringPair { | ||
readonly type: KeyringPairType; | ||
address: () => string; | ||
@@ -35,4 +36,5 @@ decodePkcs8: (passphrase?: string, encoded?: Uint8Array) => void; | ||
toJson(passphrase?: string): KeyringPair$Json; | ||
toType(type: KeyringPairType): KeyringPair; | ||
verify(message: Uint8Array, signature: Uint8Array): boolean; | ||
}; | ||
} | ||
export interface KeyringPairs { | ||
@@ -45,3 +47,3 @@ add: (pair: KeyringPair) => KeyringPair; | ||
export interface KeyringInstance { | ||
type: PairType; | ||
readonly type: KeyringPairType; | ||
decodeAddress(encoded: string | Uint8Array): Uint8Array; | ||
@@ -51,5 +53,5 @@ encodeAddress(key: Uint8Array | string): string; | ||
addPair(pair: KeyringPair): 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; | ||
addFromAddress(address: string | Uint8Array, meta: KeyringPair$Meta, encoded: Uint8Array | null, type?: KeyringPairType): KeyringPair; | ||
addFromMnemonic(mnemonic: string, meta: KeyringPair$Meta, type?: KeyringPairType): KeyringPair; | ||
addFromSeed(seed: Uint8Array, meta: KeyringPair$Meta, type?: KeyringPairType): KeyringPair; | ||
addFromJson(pair: KeyringPair$Json): KeyringPair; | ||
@@ -56,0 +58,0 @@ getPair(address: string | Uint8Array): KeyringPair; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71332
1192
Updated@polkadot/util@^0.34.6