@polkadot/types
Advanced tools
Comparing version 0.30.15 to 0.30.16
@@ -7,2 +7,3 @@ import { AnyU8a } from './types'; | ||
static decode(value: string | Uint8Array | Array<number>): Uint8Array; | ||
static readLength(input: Uint8Array): number; | ||
fromJSON(input: any): AccountIndex; | ||
@@ -9,0 +10,0 @@ fromU8a(input: Uint8Array): AccountIndex; |
@@ -37,2 +37,19 @@ "use strict"; | ||
return (0, _toU8a.default)(value); | ||
} // TODO Double check the +1 with actual e2e data | ||
static readLength(input) { | ||
const first = input[0]; | ||
if (first <= 0xef) { | ||
return 1; | ||
} else if (first === 0xfc) { | ||
return 2 + 1; | ||
} else if (first === 0xfd) { | ||
return 4 + 1; | ||
} else if (first === 0xfe) { | ||
return 8 + 1; | ||
} | ||
throw new Error(`Invalid account index byte, 0x${first.toString(16)}`); | ||
} | ||
@@ -43,9 +60,6 @@ | ||
return this; | ||
} // TODO Without specific data and actual real-world tests, unsure about how the | ||
// actual encoding here fits together. It is quite possibly prefix-encoded, so | ||
// prefixes may have to be stripped. | ||
} | ||
fromU8a(input) { | ||
super.fromU8a(input); | ||
super.fromU8a(input.subarray(0, AccountIndex.readLength(input))); | ||
return this; | ||
@@ -52,0 +66,0 @@ } |
@@ -34,2 +34,19 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
// TODO Double check the +1 with actual e2e data | ||
static readLength (input: Uint8Array): number { | ||
const first = input[0]; | ||
if (first <= 0xef) { | ||
return 1; | ||
} else if (first === 0xfc) { | ||
return 2 + 1; | ||
} else if (first === 0xfd) { | ||
return 4 + 1; | ||
} else if (first === 0xfe) { | ||
return 8 + 1; | ||
} | ||
throw new Error(`Invalid account index byte, 0x${first.toString(16)}`); | ||
} | ||
fromJSON (input: any): AccountIndex { | ||
@@ -41,7 +58,6 @@ super.fromJSON(AccountIndex.decode(input)); | ||
// TODO Without specific data and actual real-world tests, unsure about how the | ||
// actual encoding here fits together. It is quite possibly prefix-encoded, so | ||
// prefixes may have to be stripped. | ||
fromU8a (input: Uint8Array): AccountIndex { | ||
super.fromU8a(input); | ||
super.fromU8a( | ||
input.subarray(0, AccountIndex.readLength(input)) | ||
); | ||
@@ -48,0 +64,0 @@ return this; |
@@ -7,4 +7,2 @@ import Base from './codec/Base'; | ||
static decode(value: AccountId | AccountIndex | string | Uint8Array | Array<number>): AccountId | AccountIndex; | ||
static readLength(input: Uint8Array): number; | ||
static writeLength(length: number): Uint8Array; | ||
byteLength(): number; | ||
@@ -11,0 +9,0 @@ fromJSON(input: any): Address; |
@@ -29,3 +29,3 @@ "use strict"; | ||
// of the ISC license. See the LICENSE file for details. | ||
// A wrapper around an AccountId and/or AccountIndex that is encoded with a prefix. | ||
const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]); // A wrapper around an AccountId and/or AccountIndex that is encoded with a prefix. | ||
// Since we are dealing with underlying publicKeys (or shorter encoded addresses), | ||
@@ -35,2 +35,3 @@ // we extend from Base with an AccountId/AccountIndex wrapper. Basically the Address | ||
// [ <prefix-byte>, ...publicKey/...bytes ] | ||
class Address extends _Base.default { | ||
@@ -48,51 +49,11 @@ constructor(value = new Uint8Array()) { | ||
return value.length === 66 ? new _AccountId.default(value) : new _AccountIndex.default(value); | ||
} // FIXME This _may_ be an issue. Depending on how the actual AccountIndex is | ||
// actually encoded as a string. Here we just assume that it is an ss-58 value | ||
// and use the stock-stand AccountId as PublicKey | ||
} // FIXME This is an issue. If AccountIndex is encoded with ss-58, it will not | ||
// have the correct length. We need encoders/decoders for ss-58 AccountIndex | ||
return new _AccountId.default(value); | ||
} // TODO | ||
// - Double-check this logic again - if really <= 0xef, the throw | ||
// is unneeded since all cases are catered for here | ||
// - We probably want to clean the read/write up with enums if it | ||
// is an exact check and not a <= check (first item) | ||
static readLength(input) { | ||
const first = input[0]; | ||
if (first <= 0xef) { | ||
return 1; | ||
} else if (first === 0xfc) { | ||
return 2; | ||
} else if (first === 0xfd) { | ||
return 4; | ||
} else if (first === 0xfe) { | ||
return 8; | ||
} else if (first === 0xff) { | ||
return 32; | ||
} | ||
throw new Error(`Invalid account index byte, 0x${first.toString(16)}`); | ||
} | ||
static writeLength(length) { | ||
if (length === 1) { | ||
return new Uint8Array([0xef]); | ||
} else if (length === 2) { | ||
return new Uint8Array([0xfc]); | ||
} else if (length === 4) { | ||
return new Uint8Array([0xfd]); | ||
} else if (length === 8) { | ||
return new Uint8Array([0xfe]); | ||
} else if (length === 32) { | ||
return new Uint8Array([0xff]); | ||
} | ||
throw new Error(`Invalid bitLength, ${length * 8}`); | ||
} | ||
byteLength() { | ||
return 1 + super.byteLength(); | ||
return super.byteLength() + (this.raw instanceof _AccountIndex.default ? 0 : 1); | ||
} | ||
@@ -103,11 +64,6 @@ | ||
return this; | ||
} // FIXME I actually believe this is wrong, it needs to be checked. Here 1-byte | ||
// addresses are actually 2 bytes, 2-byte is 3-bytes, etc. I believe the actual | ||
// short-encoding should be left completely inside AccountId (For now, leaving | ||
// as-is until we can actually pull storage data and check - and/or read the | ||
// actual code again to check the implementation) | ||
} | ||
fromU8a(input) { | ||
this.raw = Address.readLength(input) === 32 ? new _AccountId.default().fromU8a(input.subarray(1)) : new _AccountIndex.default().fromU8a(input.subarray(1)); | ||
this.raw = input[0] === 0xff ? new _AccountId.default().fromU8a(input.subarray(1)) : new _AccountIndex.default().fromU8a(input); | ||
return this; | ||
@@ -125,3 +81,3 @@ } | ||
toU8a(isBare) { | ||
return isBare ? this.raw.toU8a(true) : (0, _concat.default)(Address.writeLength(this.raw.byteLength()), this.raw.toU8a(isBare)); | ||
return isBare || this.raw instanceof _AccountIndex.default ? this.raw.toU8a(isBare) : (0, _concat.default)(ACCOUNT_ID_PREFIX, this.raw.toU8a(isBare)); | ||
} | ||
@@ -128,0 +84,0 @@ |
@@ -55,3 +55,3 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
new Uint8Array([ | ||
1, 17 | ||
17 | ||
]) | ||
@@ -72,3 +72,3 @@ ) | ||
.toString() | ||
).toEqual('0x1112'); | ||
).toEqual('0xfc1112'); | ||
}); | ||
@@ -85,3 +85,3 @@ | ||
.toString() | ||
).toEqual('0x11121314'); | ||
).toEqual('0xfd11121314'); | ||
}); | ||
@@ -98,10 +98,4 @@ | ||
.toString() | ||
).toEqual('0x1112131415161718'); | ||
).toEqual('0xfe1112131415161718'); | ||
}); | ||
it('fails to code invalid lengths', () => { | ||
expect( | ||
() => Address.writeLength(34) | ||
).toThrow(/Invalid bitLength/); | ||
}); | ||
}); |
@@ -14,2 +14,4 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]); | ||
// A wrapper around an AccountId and/or AccountIndex that is encoded with a prefix. | ||
@@ -42,49 +44,13 @@ // Since we are dealing with underlying publicKeys (or shorter encoded addresses), | ||
// FIXME This _may_ be an issue. Depending on how the actual AccountIndex is | ||
// actually encoded as a string. Here we just assume that it is an ss-58 value | ||
// and use the stock-stand AccountId as PublicKey | ||
// FIXME This is an issue. If AccountIndex is encoded with ss-58, it will not | ||
// have the correct length. We need encoders/decoders for ss-58 AccountIndex | ||
return new AccountId(value); | ||
} | ||
// TODO | ||
// - Double-check this logic again - if really <= 0xef, the throw | ||
// is unneeded since all cases are catered for here | ||
// - We probably want to clean the read/write up with enums if it | ||
// is an exact check and not a <= check (first item) | ||
static readLength (input: Uint8Array): number { | ||
const first = input[0]; | ||
if (first <= 0xef) { | ||
return 1; | ||
} else if (first === 0xfc) { | ||
return 2; | ||
} else if (first === 0xfd) { | ||
return 4; | ||
} else if (first === 0xfe) { | ||
return 8; | ||
} else if (first === 0xff) { | ||
return 32; | ||
} | ||
throw new Error(`Invalid account index byte, 0x${first.toString(16)}`); | ||
} | ||
static writeLength (length: number): Uint8Array { | ||
if (length === 1) { | ||
return new Uint8Array([0xef]); | ||
} else if (length === 2) { | ||
return new Uint8Array([0xfc]); | ||
} else if (length === 4) { | ||
return new Uint8Array([0xfd]); | ||
} else if (length === 8) { | ||
return new Uint8Array([0xfe]); | ||
} else if (length === 32) { | ||
return new Uint8Array([0xff]); | ||
} | ||
throw new Error(`Invalid bitLength, ${length * 8}`); | ||
} | ||
byteLength (): number { | ||
return 1 + super.byteLength(); | ||
return super.byteLength() + ( | ||
this.raw instanceof AccountIndex | ||
? 0 | ||
: 1 | ||
); | ||
} | ||
@@ -98,11 +64,6 @@ | ||
// FIXME I actually believe this is wrong, it needs to be checked. Here 1-byte | ||
// addresses are actually 2 bytes, 2-byte is 3-bytes, etc. I believe the actual | ||
// short-encoding should be left completely inside AccountId (For now, leaving | ||
// as-is until we can actually pull storage data and check - and/or read the | ||
// actual code again to check the implementation) | ||
fromU8a (input: Uint8Array): Address { | ||
this.raw = Address.readLength(input) === 32 | ||
this.raw = input[0] === 0xff | ||
? new AccountId().fromU8a(input.subarray(1)) | ||
: new AccountIndex().fromU8a(input.subarray(1)); | ||
: new AccountIndex().fromU8a(input); | ||
@@ -121,6 +82,6 @@ return this; | ||
toU8a (isBare?: boolean): Uint8Array { | ||
return isBare | ||
? this.raw.toU8a(true) | ||
return isBare || this.raw instanceof AccountIndex | ||
? this.raw.toU8a(isBare) | ||
: u8aConcat( | ||
Address.writeLength(this.raw.byteLength()), | ||
ACCOUNT_ID_PREFIX, | ||
this.raw.toU8a(isBare) | ||
@@ -127,0 +88,0 @@ ); |
{ | ||
"name": "@polkadot/types", | ||
"version": "0.30.15", | ||
"version": "0.30.16", | ||
"description": "Implementation of the Parity codec", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
413843
9977