Skip to content

Commit

Permalink
Merge 888a4ea into b13486f
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Sep 25, 2018
2 parents b13486f + 888a4ea commit 5ff752a
Show file tree
Hide file tree
Showing 39 changed files with 601 additions and 305 deletions.
12 changes: 6 additions & 6 deletions packages/api-codec/src/AccountId.ts
Expand Up @@ -9,16 +9,16 @@ import isHex from '@polkadot/util/is/hex';
import isU8a from '@polkadot/util/is/u8a';
import u8aToU8a from '@polkadot/util/u8a/toU8a';

import CodecU8a from './codec/U8a';
import CodecU8aFixed from './codec/U8aFixed';
import U8a from './codec/U8a';
import U8aFixed from './codec/U8aFixed';

// A wrapper around an AccountId/PublicKey representation. Since we are dealing with
// underlying PublicKeys (32 bytes in length), we extend from CodecU8aFixed which is
// underlying PublicKeys (32 bytes in length), we extend from U8aFixed which is
// basically just a Uint8Array wrapper with a fixed length.
export default class AccountId extends CodecU8aFixed {
constructor (value: CodecU8a | string | Uint8Array = new Uint8Array()) {
export default class AccountId extends U8aFixed {
constructor (value: U8a | string | Uint8Array = new Uint8Array()) {
super(
value instanceof CodecU8a
value instanceof U8a
? value.raw
: AccountId.decode(value),
256
Expand Down
21 changes: 6 additions & 15 deletions packages/api-codec/src/AccountIndex.ts
Expand Up @@ -2,21 +2,18 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import hexToU8a from '@polkadot/util/hex/toU8a';
import isHex from '@polkadot/util/is/hex';
import isU8a from '@polkadot/util/is/u8a';
import u8aToHex from '@polkadot/util/u8a/toHex';
import u8aToU8a from '@polkadot/util/u8a/toU8a';

import CodecU8a from './codec/U8a';
import U8a from './codec/U8a';

// A wrapper around an AccountIndex, which is a shortened, variable-length encoding
// for an Account. We extends from CodecU8a which is basically
// for an Account. We extends from U8a which is basically
// just a Uint8Array wrapper.
export default class AccountIndex extends CodecU8a {
constructor (value: CodecU8a | string | Uint8Array = new Uint8Array()) {
export default class AccountIndex extends U8a {
constructor (value: U8a | string | Uint8Array = new Uint8Array()) {
super(
value instanceof CodecU8a
value instanceof U8a
? value.raw
: AccountIndex.decode(value)
);
Expand All @@ -29,13 +26,7 @@ export default class AccountIndex extends CodecU8a {
}

static decode (value: string | Uint8Array | Array<number>): Uint8Array {
if (isU8a(value) || Array.isArray(value)) {
return u8aToU8a(value);
} else if (isHex(value)) {
return hexToU8a(value);
}

throw new Error(`Unable to decode AccountIndex for [${value.toString()}]`);
return u8aToU8a(value);
}

fromJSON (input: any): AccountIndex {
Expand Down
4 changes: 2 additions & 2 deletions packages/api-codec/src/Address.ts
Expand Up @@ -7,7 +7,7 @@ import isU8a from '@polkadot/util/is/u8a';
import u8aConcat from '@polkadot/util/u8a/concat';
import u8aToU8a from '@polkadot/util/u8a/toU8a';

import CodecBase from './codec/Base';
import Base from './codec/Base';
import AccountId from './AccountId';
import AccountIndex from './AccountIndex';

Expand All @@ -16,7 +16,7 @@ import AccountIndex from './AccountIndex';
// we extend from Base with an AccountId/AccountIndex wrapper. Basically the Address
// is encoded as
// [ <prefix-byte>, ...publicKey/...bytes ]
export default class Address extends CodecBase<AccountId | AccountIndex> {
export default class Address extends Base<AccountId | AccountIndex> {
constructor (value: Address | AccountId | AccountIndex | string | Uint8Array = new Uint8Array()) {
super(
value instanceof Address
Expand Down
51 changes: 51 additions & 0 deletions packages/api-codec/src/Block.spec.js
@@ -0,0 +1,51 @@
// Copyright 2017-2018 @polkadot/api-codec authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import hexToU8a from '@polkadot/util/hex/toU8a';

import Block from './Block';

describe('Block', () => {
it('decodes a block properly', () => {
const block = new Block();

block.fromU8a(hexToU8a(
'0x' +
// parent_hash
'0900000000000000000000000000000000000000000000000000000000000009' +
// number
'4300000000000000' +
// state_root
'0800000000000000000000000000000000000000000000000000000000000008' +
// transaction_root
'0700000000000000000000000000000000000000000000000000000000000007' +
// digest
'08' + // 2 << 2
'0401' +
'0402' +
// transactions
'04' + // 1 << 2
// 111 bytes in compact encoding
'bd01' +
// prefix
'ff' +
'0000000000000000000000000000000000000000000000000000000000000000' +
'00000000' +
'0300' +
'7527000000000000' +
'0000000000000000000000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000000'
));

expect(
block.header.blockNumber.toNumber()
).toEqual(67);
expect(
block.extrinsics.length
).toEqual(1); // eslint-disable-line
expect(
block.extrinsics.at(0).length
).toEqual(111); // eslint-disable-line
});
});
27 changes: 27 additions & 0 deletions packages/api-codec/src/Block.ts
@@ -0,0 +1,27 @@
// Copyright 2017-2018 @polkadot/api-codec authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import Struct from './codec/Struct';
import Vector from './codec/Vector';

import Extrinsic from './Extrinsic';
import Header from './Header';

// A block encoded with header and extrinsics
export default class Block extends Struct {
constructor (value?: any) {
super({
header: Header,
extrinsics: Vector.with(Extrinsic)
}, value);
}

get extrinsics (): Vector<Extrinsic> {
return this.raw.extrinsics as Vector<Extrinsic>;
}

get header (): Header {
return this.raw.header as Header;
}
}
4 changes: 2 additions & 2 deletions packages/api-codec/src/Bool.ts
Expand Up @@ -2,9 +2,9 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import CodecBase from './codec/Base';
import Base from './codec/Base';

export default class Bool extends CodecBase<boolean> {
export default class Bool extends Base<boolean> {
constructor (value: Bool | boolean = false) {
super(
value instanceof Bool
Expand Down
22 changes: 22 additions & 0 deletions packages/api-codec/src/Extrinsic.ts
@@ -0,0 +1,22 @@
// Copyright 2017-2018 @polkadot/api-codec authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import blake2Asu8a from '@polkadot/util-crypto/blake2/asU8a';

import Bytes from './codec/Bytes';

import Hash from './Hash';

// Representation of an Extrinsic in the system.
//
// NOTE At this point we are just keeping to a bare-bones Vec<u8> structure,
// this should be expanded to do the automatic decoding of an extrinsic
export default class Extrinsic extends Bytes {
// convernience, encodes the extrinsic and returns the actual hash
get hash (): Hash {
return new Hash(
blake2Asu8a(this.toU8a(), 256)
);
}
}
8 changes: 4 additions & 4 deletions packages/api-codec/src/H256.ts
Expand Up @@ -2,13 +2,13 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import CodecU8a from './codec/U8a';
import CodecU8aFixed from './codec/U8aFixed';
import U8a from './codec/U8a';
import U8aFixed from './codec/U8aFixed';

// Hash containing 256 bits (32 bytes), typically used in blocks, extrinsics and
// as a sane default for fixed-length hash representations.
export default class H256 extends CodecU8aFixed {
constructor (value?: CodecU8a | Uint8Array) {
export default class H256 extends U8aFixed {
constructor (value?: U8a | string | Uint8Array) {
super(value, 256);
}
}
8 changes: 4 additions & 4 deletions packages/api-codec/src/H512.ts
Expand Up @@ -2,12 +2,12 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import CodecU8a from './codec/U8a';
import CodecU8aFixed from './codec/U8aFixed';
import U8a from './codec/U8a';
import U8aFixed from './codec/U8aFixed';

// Hash containing 512 bits (64 bytes), typically used for signatures
export default class H512 extends CodecU8aFixed {
constructor (value?: CodecU8a | Uint8Array) {
export default class H512 extends U8aFixed {
constructor (value?: U8a | string | Uint8Array) {
super(value, 512);
}
}
43 changes: 33 additions & 10 deletions packages/api-codec/src/Header.ts
Expand Up @@ -2,29 +2,45 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import CodecArray from './codec/Array';
import CodecBytes from './codec/Bytes';
import CodecStruct from './codec/Struct';
import BN from 'bn.js';
import blake2Asu8a from '@polkadot/util-crypto/blake2/asU8a';

import Bytes from './codec/Bytes';
import Long from './codec/Long';
import Struct from './codec/Struct';
import Vector from './codec/Vector';

import BlockNumber from './BlockNumber';
import Hash from './Hash';

type DigestStruct = {
logs?: Array<Uint8Array | string>
};

type HeaderStruct = {
digest?: DigestStruct,
extrinsicsRoot?: Hash | Uint8Array | string,
number?: Long | BN | number | string,
parentHash?: Hash | Uint8Array | string,
stateRoot?: Hash | Uint8Array | string
};

// A block header digest.
export class Digest extends CodecStruct {
constructor (value?: any) {
export class Digest extends Struct {
constructor (value: DigestStruct = {}) {
super({
logs: CodecArray.with(CodecBytes)
logs: Vector.with(Bytes)
}, value);
}

get logs (): CodecArray<CodecBytes> {
return this.raw.logs as CodecArray<CodecBytes>;
get logs (): Vector<Bytes> {
return this.raw.logs as Vector<Bytes>;
}
}

// A block header.
export default class Header extends CodecStruct {
constructor (value?: any) {
export default class Header extends Struct {
constructor (value: HeaderStruct = {}) {
super({
parentHash: Hash,
number: BlockNumber,
Expand All @@ -46,6 +62,13 @@ export default class Header extends CodecStruct {
return this.raw.extrinsicsRoot as Hash;
}

// convernience, encodes the header and returns the actual hash
get hash (): Hash {
return new Hash(
blake2Asu8a(this.toU8a(), 256)
);
}

get parentHash (): Hash {
return this.raw.parentHash as Hash;
}
Expand Down
26 changes: 13 additions & 13 deletions packages/api-codec/src/KeyValue.ts
Expand Up @@ -2,31 +2,31 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import CodecBytes from './codec/Bytes';
import CodecStruct from './codec/Struct';
import Bytes from './codec/Bytes';
import Struct from './codec/Struct';

type KeyValueStruct = {
key: Uint8Array,
value: Uint8Array
key?: Uint8Array | string,
value?: Uint8Array | string
};

// KeyValue structure. Since most of the keys and resultant values in Subtrate is
// hashed and/or encoded, this does not wrap a CodecString, but rather a CodecBytes
// hashed and/or encoded, this does not wrap a Text, but rather a Bytes
// for the keys and values. (Not to be confused with the KeyValue in Metadata, that
// is actually for Maps, whereas this is a representation of actaul storage values)
export default class KeyValue extends CodecStruct {
constructor (value: KeyValueStruct = {} as KeyValueStruct) {
export default class KeyValue extends Struct {
constructor (value: KeyValueStruct = {}) {
super({
key: CodecBytes,
value: CodecBytes
key: Bytes,
value: Bytes
}, value);
}

get key (): CodecBytes {
return this.raw.key as CodecBytes;
get key (): Bytes {
return this.raw.key as Bytes;
}

get value (): CodecBytes {
return this.raw.value as CodecBytes;
get value (): Bytes {
return this.raw.value as Bytes;
}
}

0 comments on commit 5ff752a

Please sign in to comment.