diff --git a/packages/types/src/AccountIndex.ts b/packages/types/src/AccountIndex.ts index 4969094acd..d0f724a632 100644 --- a/packages/types/src/AccountIndex.ts +++ b/packages/types/src/AccountIndex.ts @@ -2,6 +2,7 @@ // 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/address/decode'; import encodeAddress from '@polkadot/keyring/address/encode'; import hexToU8a from '@polkadot/util/hex/toU8a'; @@ -11,6 +12,10 @@ import u8aConcat from '@polkadot/util/u8a/concat'; import { AnyU8a } from './types'; import U8a from './codec/U8a'; +import u8aToBn from '@polkadot/util/u8a/toBn'; +import bnToU8a from '@polkadot/util/bn/toU8a'; + +const ENUMSET_SIZE = new BN(64); // A wrapper around an AccountIndex, which is a shortened, variable-length encoding // for an Account. We extends from U8a which is basically @@ -85,9 +90,21 @@ export default class AccountIndex extends U8a { return this.toString(); } + toBn (): BN { + if (this.raw.length === 1) { + return new BN(this.raw[0]); + } + + return u8aToBn(this.raw, true); + } + toU8a (isBare?: boolean): Uint8Array { + // HACK 15 Oct 2018 For isBare assume that we are dealing with an AccountIndex + // lookup (it is the only place where AccountIndex is used in such a manner to + // construct a query). This is needed to get enumSet(AccountIndex) queries to + // work in the way it was intended return isBare - ? this.raw + ? bnToU8a(this.toBn().div(ENUMSET_SIZE), 32, true) : u8aConcat( AccountIndex.writeLength(this.raw), this.raw