Use #<varname> instead of private _<varname> (#545)

* Use #<varname> insead of private _<varname>

* Update key extraction paths
This commit is contained in:
Jaco Greeff
2020-02-22 14:45:31 +01:00
committed by GitHub
parent 75cd547b49
commit 7c1ca3a5c9
8 changed files with 145 additions and 154 deletions
@@ -16,9 +16,9 @@ const BN_OPTIONS = {
};
export default class DeriveJunction {
private _chainCode: Uint8Array = new Uint8Array(32);
readonly #chainCode: Uint8Array = new Uint8Array(32);
private _isHard = false;
#isHard = false;
public static from (value: string): DeriveJunction {
const [code, isHard] = value.startsWith('/')
@@ -38,15 +38,15 @@ export default class DeriveJunction {
}
public get chainCode (): Uint8Array {
return this._chainCode;
return this.#chainCode;
}
public get isHard (): boolean {
return this._isHard;
return this.#isHard;
}
public get isSoft (): boolean {
return !this._isHard;
return !this.#isHard;
}
public hard (value: number | BN | string | Uint8Array): DeriveJunction {
@@ -54,7 +54,7 @@ export default class DeriveJunction {
}
public harden (): DeriveJunction {
this._isHard = true;
this.#isHard = true;
return this;
}
@@ -72,14 +72,14 @@ export default class DeriveJunction {
return this.soft(blake2AsU8a(value));
}
this._chainCode.fill(0);
this._chainCode.set(value, 0);
this.#chainCode.fill(0);
this.#chainCode.set(value, 0);
return this;
}
public soften (): DeriveJunction {
this._isHard = false;
this.#isHard = false;
return this;
}
@@ -6,37 +6,32 @@ import keyExtractPath, { ExtractResult } from './extractPath';
describe('keyExtractPath', (): void => {
it('extracts properly from soft', (): void => {
expect(
keyExtractPath('/1')
).toEqual({
parts: ['/1'],
path: [
{ _isHard: false, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
]
});
const test = keyExtractPath('/1');
expect(test.parts).toEqual(['/1']);
expect(test.path.length).toEqual(1);
expect(test.path[0].isHard).toEqual(false);
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
});
it('extracts properly from hard', (): void => {
expect(
keyExtractPath('//1')
).toEqual({
parts: ['//1'],
path: [
{ _isHard: true, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
]
});
const test = keyExtractPath('//1');
expect(test.parts).toEqual(['//1']);
expect(test.path.length).toEqual(1);
expect(test.path[0].isHard).toEqual(true);
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
});
it('extracts properly from hard/soft', (): void => {
expect(
keyExtractPath('//1/2')
).toEqual({
parts: ['//1', '/2'],
path: [
{ _isHard: true, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) },
{ _isHard: false, _chainCode: Uint8Array.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
]
});
const test = keyExtractPath('//1/2');
expect(test.parts).toEqual(['//1', '/2']);
expect(test.path.length).toEqual(2);
expect(test.path[0].isHard).toEqual(true);
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
expect(test.path[1].isHard).toEqual(false);
expect(test.path[1].chainCode).toEqual(Uint8Array.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
});
it('does not extract from invalid paths', (): void => {
@@ -12,126 +12,115 @@ describe('keyExtractSuri', (): void => {
});
it('derives on "hello world"', (): void => {
expect(
keyExtractSuri('hello world')
).toEqual({
path: [],
phrase: 'hello world'
});
const test = keyExtractSuri('hello world');
expect(test.phrase).toEqual('hello world');
expect(test.path.length).toEqual(0);
});
it('derives on "hello world/1', (): void => {
expect(
keyExtractSuri('hello world/1')
).toEqual({
path: [
{ _isHard: false, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
],
phrase: 'hello world'
});
const test = keyExtractSuri('hello world/1');
expect(test.password).not.toBeDefined();
expect(test.phrase).toEqual('hello world');
expect(test.path.length).toEqual(1);
expect(test.path[0].isHard).toEqual(false);
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
});
it('derives on "hello world/DOT', (): void => {
expect(
keyExtractSuri('hello world/DOT')
).toEqual({
path: [
{ _isHard: false, _chainCode: Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
],
phrase: 'hello world'
});
const test = keyExtractSuri('hello world/DOT');
expect(test.password).not.toBeDefined();
expect(test.phrase).toEqual('hello world');
expect(test.path.length).toEqual(1);
expect(test.path[0].isHard).toEqual(false);
expect(test.path[0].chainCode).toEqual(Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
});
it('derives on "hello world//1', (): void => {
expect(
keyExtractSuri('hello world//1')
).toEqual({
path: [
{ _isHard: true, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
],
phrase: 'hello world'
});
const test = keyExtractSuri('hello world//1');
expect(test.password).not.toBeDefined();
expect(test.phrase).toEqual('hello world');
expect(test.path.length).toEqual(1);
expect(test.path[0].isHard).toEqual(true);
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
});
it('derives on "hello world//DOT', (): void => {
expect(
keyExtractSuri('hello world//DOT')
).toEqual({
path: [
{ _isHard: true, _chainCode: Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
],
phrase: 'hello world'
});
const test = keyExtractSuri('hello world//DOT');
expect(test.password).not.toBeDefined();
expect(test.phrase).toEqual('hello world');
expect(test.path.length).toEqual(1);
expect(test.path[0].isHard).toEqual(true);
expect(test.path[0].chainCode).toEqual(Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
});
it('derives on "hello world//1/DOT', (): void => {
expect(
keyExtractSuri('hello world//1/DOT')
).toEqual({
path: [
{ _isHard: true, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) },
{ _isHard: false, _chainCode: Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
],
phrase: 'hello world'
});
const test = keyExtractSuri('hello world//1/DOT');
expect(test.password).not.toBeDefined();
expect(test.phrase).toEqual('hello world');
expect(test.path.length).toEqual(2);
expect(test.path[0].isHard).toEqual(true);
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
expect(test.path[1].isHard).toEqual(false);
expect(test.path[1].chainCode).toEqual(Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
});
it('derives on "hello world//DOT/1', (): void => {
expect(
keyExtractSuri('hello world//DOT/1')
).toEqual({
path: [
{ _isHard: true, _chainCode: Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) },
{ _isHard: false, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
],
phrase: 'hello world'
});
const test = keyExtractSuri('hello world//DOT/1');
expect(test.password).not.toBeDefined();
expect(test.phrase).toEqual('hello world');
expect(test.path.length).toEqual(2);
expect(test.path[0].isHard).toEqual(true);
expect(test.path[0].chainCode).toEqual(Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
expect(test.path[1].isHard).toEqual(false);
expect(test.path[1].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
});
it('derives on "hello world///password"', (): void => {
expect(
keyExtractSuri('hello world///password')
).toEqual({
password: 'password',
path: [],
phrase: 'hello world'
});
const test = keyExtractSuri('hello world///password');
expect(test.password).toEqual('password');
expect(test.phrase).toEqual('hello world');
expect(test.path.length).toEqual(0);
});
it('derives on "hello world//1/DOT///password"', (): void => {
expect(
keyExtractSuri('hello world//1/DOT///password')
).toEqual({
path: [
{ _isHard: true, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) },
{ _isHard: false, _chainCode: Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
],
password: 'password',
phrase: 'hello world'
});
const test = keyExtractSuri('hello world//1/DOT///password');
expect(test.password).toEqual('password');
expect(test.phrase).toEqual('hello world');
expect(test.path.length).toEqual(2);
expect(test.path[0].isHard).toEqual(true);
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
expect(test.path[1].isHard).toEqual(false);
expect(test.path[1].chainCode).toEqual(Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
});
it('derives on "hello world/1//DOT///password"', (): void => {
expect(
keyExtractSuri('hello world/1//DOT///password')
).toEqual({
path: [
{ _isHard: false, _chainCode: Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) },
{ _isHard: true, _chainCode: Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
],
password: 'password',
phrase: 'hello world'
});
const test = keyExtractSuri('hello world/1//DOT///password');
expect(test.password).toEqual('password');
expect(test.phrase).toEqual('hello world');
expect(test.path.length).toEqual(2);
expect(test.path[0].isHard).toEqual(false);
expect(test.path[0].chainCode).toEqual(Uint8Array.from([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
expect(test.path[1].isHard).toEqual(true);
expect(test.path[1].chainCode).toEqual(Uint8Array.from([12, 68, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
});
it('derives on actual Alice', (): void => {
expect(
keyExtractSuri('bottom drive obey lake curtain smoke basket hold race lonely fit walk//Alice')
).toEqual({
path: [
{ _isHard: true, _chainCode: Uint8Array.from([20, 65, 108, 105, 99, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }
],
phrase: 'bottom drive obey lake curtain smoke basket hold race lonely fit walk'
});
const test = keyExtractSuri('bottom drive obey lake curtain smoke basket hold race lonely fit walk//Alice');
expect(test.password).not.toBeDefined();
expect(test.phrase).toEqual('bottom drive obey lake curtain smoke basket hold race lonely fit walk');
expect(test.path.length).toEqual(1);
expect(test.path[0].isHard).toEqual(true);
expect(test.path[0].chainCode).toEqual(Uint8Array.from([20, 65, 108, 105, 99, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
});
});