Create createKeyMulti & createKeySub for utility (util-crypto) (#587)

* Create keyMulti & keysub for utility

* Bumps
This commit is contained in:
Jaco Greeff
2020-04-24 15:34:47 +02:00
committed by GitHub
parent b196158125
commit 60a9866abe
9 changed files with 716 additions and 529 deletions
+4
View File
@@ -1,3 +1,7 @@
# 2.9.0-beta.x
- Added `createKeyMulti` & `createKeySub` to create utility sub & multi keys
# 2.8.1 Apr 9, 2020
- Keypair will now throw an error when attempting to sign/derive using a locked pair (Thanks to https://github.com/h4x3rotab)
+1 -1
View File
@@ -22,7 +22,7 @@
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@polkadot/dev": "^0.52.8",
"@polkadot/dev": "^0.52.11",
"@polkadot/ts": "^0.3.18",
"@types/jest": "^25.2.1"
}
@@ -4,6 +4,8 @@
import checkAddress from './check';
import checkAddressChecksum from './checkChecksum';
import createKeyMulti from './keyMulti';
import createKeySub from './keySub';
import decodeAddress from './decode';
import encodeAddress from './encode';
import setSS58Format from './setSS58Format';
@@ -11,6 +13,8 @@ import setSS58Format from './setSS58Format';
export {
checkAddress,
checkAddressChecksum,
createKeyMulti,
createKeySub,
decodeAddress,
encodeAddress,
setSS58Format
@@ -0,0 +1,33 @@
// Copyright 2017-2020 @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 createKeyMulti, { sortU8a } from './keyMulti';
describe('sortU8a', (): void => {
it('sorts a simple set of u8a', (): void => {
expect(
sortU8a([new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6]), new Uint8Array([2, 3, 4])])
).toEqual([new Uint8Array([1, 2, 3]), new Uint8Array([2, 3, 4]), new Uint8Array([4, 5, 6])]);
});
it('sorts a simple set of u8a (not the same lengths)', (): void => {
expect(
sortU8a([new Uint8Array([1, 2, 3, 4]), new Uint8Array([4, 5, 6]), new Uint8Array([1, 2, 3, 5])])
).toEqual([new Uint8Array([1, 2, 3, 4]), new Uint8Array([1, 2, 3, 5]), new Uint8Array([4, 5, 6])]);
});
});
describe('createKeyMulti', (): void => {
it('creates a valid multikey (aligning with Rust, needs sorting)', (): void => {
expect(
createKeyMulti([
new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0]),
new Uint8Array([3, 0, 0, 0, 0, 0, 0, 0]),
new Uint8Array([2, 0, 0, 0, 0, 0, 0, 0])
], 2)
).toEqual(
new Uint8Array([67, 151, 196, 155, 179, 207, 47, 123, 90, 2, 35, 54, 162, 111, 241, 226, 88, 148, 54, 193, 252, 195, 93, 101, 16, 5, 93, 101, 186, 186, 254, 79])
);
});
});
@@ -0,0 +1,44 @@
// Copyright 2017-2020 @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 BN from 'bn.js';
import { bnToU8a, compactToU8a, isUndefined, u8aConcat } from '@polkadot/util';
import blake2AsU8a from '../blake2/asU8a';
import decodeAddress from './decode';
export function sortU8a (u8as: Uint8Array[]): Uint8Array[] {
return u8as.sort((a, b): number => {
let i = 0;
while (true) {
if (isUndefined(a[i]) && isUndefined(b[i])) {
return 0;
} else if (isUndefined(a[i])) {
return -1;
} else if (isUndefined(b[i])) {
return 1;
}
const cmp = a[i] - b[i];
if (cmp !== 0) {
return cmp;
}
i++;
}
});
}
export default function createKeyMulti (who: (Uint8Array | string)[], index: BigInt | BN | number): Uint8Array {
return blake2AsU8a(
u8aConcat(
'modlpy/utilisuba',
compactToU8a(who.length),
...sortU8a(who.map((who) => decodeAddress(who))),
bnToU8a(index, { bitLength: 16, isLe: true })
)
);
}
@@ -0,0 +1,23 @@
// Copyright 2017-2020 @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 createKeySub from './keySub';
describe('createKeySub', (): void => {
it('matches sub accounts with Rust', (): void => {
expect(
createKeySub(new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0]), 0)
).toEqual(
new Uint8Array([234, 236, 28, 96, 177, 168, 152, 193, 71, 179, 226, 102, 179, 155, 188, 240, 90, 182, 21, 175, 47, 47, 250, 179, 178, 0, 81, 222, 70, 56, 52, 234])
);
});
it('creates a valid subkey', (): void => {
expect(
createKeySub('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', 1)
).toEqual(
new Uint8Array([248, 19, 86, 209, 254, 89, 84, 48, 54, 128, 166, 239, 153, 212, 143, 34, 191, 60, 210, 50, 39, 77, 122, 71, 29, 60, 247, 198, 95, 101, 246, 83])
);
});
});
@@ -0,0 +1,19 @@
// Copyright 2017-2020 @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 BN from 'bn.js';
import { bnToU8a, u8aConcat } from '@polkadot/util';
import blake2AsU8a from '../blake2/asU8a';
import decodeAddress from './decode';
export default function createKeySub (who: Uint8Array | string, index: BigInt | BN | number): Uint8Array {
return blake2AsU8a(
u8aConcat(
'modlpy/utilisuba',
decodeAddress(who),
bnToU8a(index, { bitLength: 16, isLe: true })
)
);
}
+2 -3
View File
@@ -22,9 +22,8 @@ import u8aToU8a from './toU8a';
* ```
*/
export default function u8aConcat (..._list: (Uint8Array | string)[]): Uint8Array {
const list: Uint8Array[] = _list.map(u8aToU8a);
const length = list.reduce((total, item): number => total + item.length, 0);
const result = new Uint8Array(length);
const list = _list.map(u8aToU8a);
const result = new Uint8Array(list.reduce((total, item): number => total + item.length, 0));
let offset = 0;
return list.reduce((result, item): Uint8Array => {
+586 -525
View File
File diff suppressed because it is too large Load Diff