AccountIndex extends U32 (#355)
* AccountIndex extends U32 * Re-add toU8a with isBare override * Small clarifications * case-sensitive import * Update comment
This commit is contained in:
@@ -8,7 +8,7 @@ import AccountIndex from './AccountIndex';
|
||||
describe('AccountIndex', () => {
|
||||
it('creates a BN representation', () => {
|
||||
expect(
|
||||
new AccountIndex([17, 18, 19, 20]).toBn().toString()
|
||||
new AccountIndex(new Uint8Array([17, 18, 19, 20])).toBn().toString()
|
||||
).toEqual('336794129');
|
||||
});
|
||||
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { decodeAddress, encodeAddress } from '@polkadot/keyring';
|
||||
import { bnToBn, bnToU8a, hexToU8a, isBn, isNumber, isU8a, isHex, u8aToBn } from '@polkadot/util';
|
||||
import { bnToBn, bnToU8a, isBn, isNumber, isU8a, isHex, hexToU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { AnyU8a } from './types';
|
||||
import U8a from './codec/U8a';
|
||||
import { AnyNumber } from './types';
|
||||
import UInt from './codec/UInt';
|
||||
import U32 from './U32';
|
||||
|
||||
export const ENUMSET_SIZE = new BN(64);
|
||||
|
||||
@@ -22,39 +23,25 @@ const MAX_4BYTE = new BN(1).shln(32);
|
||||
// A wrapper around an AccountIndex, which is a shortened, variable-length encoding
|
||||
// for an Account. We extends from U8a which is basically
|
||||
// just a Uint8Array wrapper.
|
||||
export default class AccountIndex extends U8a {
|
||||
// The maxLength refers to the maximum input. Effectively when returned as a storage value
|
||||
// (see EventRecord) the actual encoded length is 4 bytes. Set that as the default,
|
||||
// however pass it through as a computed value from the Address constructors
|
||||
//
|
||||
// FIXME Encoding is not handled 100% correctly with the maxLength. Change the base to be actually
|
||||
// compilant with the Rust implementation, https://github.com/polkadot-js/api/issues/354
|
||||
constructor (value: BN | number | AnyU8a = new Uint8Array(), maxLength: number = 4) {
|
||||
export default class AccountIndex extends U32 {
|
||||
constructor (value: AnyNumber = new BN(0)) {
|
||||
super(
|
||||
AccountIndex.decodeAccountIndex(value, maxLength)
|
||||
AccountIndex.decodeAccountIndex(value)
|
||||
);
|
||||
}
|
||||
|
||||
static decodeAccountIndex (value: BN | number | AnyU8a, maxLength: number): Uint8Array {
|
||||
if (value instanceof U8a) {
|
||||
static decodeAccountIndex (value: AnyNumber): BN | Uint8Array | number | string {
|
||||
if (value instanceof UInt) {
|
||||
return value.raw;
|
||||
} else if (Array.isArray(value)) {
|
||||
return AccountIndex.decodeAccountIndex(Uint8Array.from(value), maxLength);
|
||||
} else if (isU8a(value)) {
|
||||
if (!value.length) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return value.subarray(0, Math.min(maxLength, value.length));
|
||||
} else if (isNumber(value) || isBn(value)) {
|
||||
const bitLength = 8 * AccountIndex.calcLength(value);
|
||||
|
||||
return bnToU8a(value, bitLength, true);
|
||||
} else if (isBn(value) || isNumber(value) || isU8a(value)) {
|
||||
return value;
|
||||
} else if (isHex(value)) {
|
||||
// Here we convert via hexToU8a since we expect the LE encoded value representation. This
|
||||
// is different than UInt where we expect a BE (human-readable representation)
|
||||
return hexToU8a(value);
|
||||
}
|
||||
|
||||
return decodeAddress(value);
|
||||
return AccountIndex.decodeAccountIndex(decodeAddress(value));
|
||||
}
|
||||
|
||||
static calcLength (_value: BN | number): number {
|
||||
@@ -94,16 +81,20 @@ export default class AccountIndex extends U8a {
|
||||
}
|
||||
}
|
||||
|
||||
// Like in our decoding function, we explicitly override this to allow us to output
|
||||
// LE-hex encoded numbers (generally UInt in JSON are expected as BE, these LE)
|
||||
toHex (): string {
|
||||
return u8aToHex(this.toU8a());
|
||||
}
|
||||
|
||||
toJSON (): any {
|
||||
return this.toString();
|
||||
}
|
||||
|
||||
toBn (): BN {
|
||||
if (this.raw.length === 1) {
|
||||
return new BN(this.raw[0]);
|
||||
}
|
||||
toString (): string {
|
||||
const length = AccountIndex.calcLength(this.raw);
|
||||
|
||||
return u8aToBn(this.raw, true);
|
||||
return encodeAddress(this.toU8a().subarray(0, length));
|
||||
}
|
||||
|
||||
toU8a (isBare?: boolean): Uint8Array {
|
||||
@@ -113,10 +104,6 @@ export default class AccountIndex extends U8a {
|
||||
// work in the way it was intended
|
||||
return isBare
|
||||
? bnToU8a(this.toBn().div(ENUMSET_SIZE), 32, true)
|
||||
: this.raw;
|
||||
}
|
||||
|
||||
toString (): string {
|
||||
return encodeAddress(this.raw);
|
||||
: super.toU8a();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,11 +68,14 @@ describe('Address', () => {
|
||||
Uint8Array.from([0xfd, 17, 18, 19, 20]),
|
||||
'Mwz15xN8'
|
||||
);
|
||||
testDecode(
|
||||
'Uint8Array (with prefix 8 bytes)',
|
||||
Uint8Array.from([0xfe, 17, 18, 19, 20, 21, 22, 23, 24]),
|
||||
'3N5RJXxM5fLd4h'
|
||||
);
|
||||
// FIXME The specification allows for 8 byte addresses, however since AccountIndex is u32 internally
|
||||
// (and defined that way in thd efault Substrate),this does not actually work since it is 8 bytes,
|
||||
// instead of 4 bytes max u32 length
|
||||
// testDecode(
|
||||
// 'Uint8Array (with prefix 8 bytes)',
|
||||
// Uint8Array.from([0xfe, 17, 18, 19, 20, 21, 22, 23, 24]),
|
||||
// '3N5RJXxM5fLd4h'
|
||||
// );
|
||||
});
|
||||
|
||||
describe('encoding', () => {
|
||||
|
||||
@@ -2,12 +2,15 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { decodeAddress } from '@polkadot/keyring';
|
||||
import { hexToU8a, isHex, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { hexToU8a, isBn, isHex, isNumber, isU8a, u8aConcat, u8aToHex, u8aToU8a, u8aToBn } from '@polkadot/util';
|
||||
|
||||
import Base from './codec/Base';
|
||||
import AccountId from './AccountId';
|
||||
import AccountIndex from './AccountIndex';
|
||||
import Base from './codec/Base';
|
||||
|
||||
type AnyAddress = BN | Address | AccountId | AccountIndex | Array<number> | Uint8Array | number | string;
|
||||
|
||||
export const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]);
|
||||
|
||||
@@ -17,14 +20,16 @@ export const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]);
|
||||
// is encoded as
|
||||
// [ <prefix-byte>, ...publicKey/...bytes ]
|
||||
export default class Address extends Base<AccountId | AccountIndex> {
|
||||
constructor (value: Address | AccountId | AccountIndex | Array<number> | Uint8Array | string = new Uint8Array()) {
|
||||
constructor (value: AnyAddress = new Uint8Array()) {
|
||||
super(
|
||||
Address.decodeAddress(value)
|
||||
);
|
||||
}
|
||||
|
||||
static decodeAddress (value: Address | AccountId | AccountIndex | Array<number> | Uint8Array | string): AccountId | AccountIndex {
|
||||
if (value instanceof Address) {
|
||||
static decodeAddress (value: AnyAddress): AccountId | AccountIndex {
|
||||
if (isBn(value) || isNumber(value)) {
|
||||
return new AccountIndex(value);
|
||||
} else if (value instanceof Address) {
|
||||
return value.raw;
|
||||
} else if (value instanceof AccountId || value instanceof AccountIndex) {
|
||||
return value;
|
||||
@@ -41,7 +46,7 @@ export default class Address extends Base<AccountId | AccountIndex> {
|
||||
|
||||
const [offset, length] = AccountIndex.readLength(value);
|
||||
|
||||
return new AccountIndex(value.subarray(offset, offset + length), length);
|
||||
return new AccountIndex(u8aToBn(value.subarray(offset, offset + length), true));
|
||||
} else if (isHex(value)) {
|
||||
return Address.decodeAddress(hexToU8a(value));
|
||||
}
|
||||
@@ -50,15 +55,21 @@ export default class Address extends Base<AccountId | AccountIndex> {
|
||||
|
||||
return decoded.length === 32
|
||||
? new AccountId(decoded)
|
||||
: new AccountIndex(decoded, decoded.length);
|
||||
: new AccountIndex(u8aToBn(decoded, true));
|
||||
}
|
||||
|
||||
get rawLength (): number {
|
||||
return this.raw instanceof AccountIndex
|
||||
? AccountIndex.calcLength(this.raw.toBn())
|
||||
: this.raw.encodedLength;
|
||||
}
|
||||
|
||||
get encodedLength (): number {
|
||||
const dataLength = this.raw.encodedLength;
|
||||
const rawLength = this.rawLength;
|
||||
|
||||
return dataLength + (
|
||||
return rawLength + (
|
||||
// for 1 byte AccountIndexes, we are not adding a specific prefix
|
||||
dataLength > 1
|
||||
rawLength > 1
|
||||
? 1
|
||||
: 0
|
||||
);
|
||||
@@ -77,7 +88,7 @@ export default class Address extends Base<AccountId | AccountIndex> {
|
||||
}
|
||||
|
||||
toU8a (isBare?: boolean): Uint8Array {
|
||||
const encoded = this.raw.toU8a(isBare);
|
||||
const encoded = this.raw.toU8a().subarray(0, this.rawLength);
|
||||
|
||||
return isBare
|
||||
? encoded
|
||||
|
||||
Reference in New Issue
Block a user