Document getters and functions for all types (static missing) (#431)

* Document getters and functions for all types (static missing)

* Update SignedBlock.ts

* Bump @polkadot/dev, introduce @noInheritDoc

* Text inherits from String

* Link to indv. types from README

* Add base to UInt toString
This commit is contained in:
Jaco Greeff
2018-12-03 15:47:22 +01:00
committed by GitHub
parent f1df2f7c75
commit a7b177d7dd
41 changed files with 704 additions and 83 deletions
+6
View File
@@ -40,10 +40,16 @@ export default class AccountId extends U8aFixed {
return value;
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
return this.toString();
}
/**
* @description Returns the string representation of the value
*/
toString (): string {
return AccountId.encode(this);
}
+18 -2
View File
@@ -86,26 +86,42 @@ export default class AccountIndex extends U32 {
}
}
/**
* @description Returns the BN representation of the AccountIndex
*/
toBn (): BN {
return new BN(this);
}
// 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)
/**
* @description Returns a hex string representation of the value
*/
toHex (): string {
// 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)
return u8aToHex(this.toU8a());
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
return this.toString();
}
/**
* @description Returns the string representation of the value
*/
toString (): string {
const length = AccountIndex.calcLength(this);
return encodeAddress(this.toU8a().subarray(0, length));
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
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
+19
View File
@@ -61,6 +61,9 @@ export default class Address extends Base<AccountId | AccountIndex> {
: new AccountIndex(u8aToBn(decoded, true));
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
get encodedLength (): number {
const rawLength = this.rawLength;
@@ -72,24 +75,40 @@ export default class Address extends Base<AccountId | AccountIndex> {
);
}
/**
* @description The length of the raw value, either AccountIndex or AccountId
*/
get rawLength (): number {
return this.raw instanceof AccountIndex
? AccountIndex.calcLength(this.raw)
: this.raw.encodedLength;
}
/**
* @description Returns a hex string representation of the value
*/
toHex (): string {
return u8aToHex(this.toU8a());
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
return this.raw.toJSON();
}
/**
* @description Returns the string representation of the value
*/
toString (): string {
return this.raw.toString();
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
const encoded = this.raw.toU8a().subarray(0, this.rawLength);
+12
View File
@@ -28,10 +28,16 @@ export class BftAuthoritySignature extends Tuple {
}, value);
}
/**
* @description The wrapped [[AuthoriyId]]
*/
get authorityId (): AuthorityId {
return this.getAtIndex(0) as AuthorityId;
}
/**
* @description The wrapped [[Signature]] value
*/
get signature (): Signature {
return this.getAtIndex(1) as Signature;
}
@@ -56,10 +62,16 @@ export class BftHashSignature extends Tuple {
}, value);
}
/**
* @description The wrapped [[Hash]]
*/
get hash (): Hash {
return this.getAtIndex(0) as Hash;
}
/**
* @description The wrapped [[Signature]]
*/
get signature (): Signature {
return this.getAtIndex(1) as Signature;
}
+9 -1
View File
@@ -29,17 +29,25 @@ export default class Block extends Struct {
}, value);
}
/**
* @description The [[Extrinsics]] conatined in the block
*/
get extrinsics (): Extrinsics {
return this.get('extrinsics') as Extrinsics;
}
// convenience, encodes the header and returns the actual hash
/**
* @description Coinvenience getter, encoded the [[Hash]] for the block
*/
get hash (): Hash {
return new Hash(
blake2AsU8a(this.toU8a(), 256)
);
}
/**
* @description The [[Header]] in the block
*/
get header (): Header {
return this.get('header') as Header;
}
+22 -5
View File
@@ -9,7 +9,8 @@ import { Codec } from './types';
/**
* @name Bool
* @description
* Representation for a boolean value in the system
* Representation for a boolean value in the system. It extends the base JS `Boolean` class
* @noInheritDoc
*/
export default class Bool extends Boolean implements Codec {
constructor (value: Bool | Boolean | Uint8Array | boolean | number = false) {
@@ -28,22 +29,38 @@ export default class Bool extends Boolean implements Codec {
return !!value;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
get encodedLength (): number {
return 1;
}
toJSON (): any {
return this.valueOf();
}
/**
* @description Returns a hex string representation of the value
*/
toHex (): string {
return u8aToHex(this.toU8a());
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
return this.valueOf();
}
/**
* @description Returns the string representation of the value
*/
toString (): string {
return `${this.toJSON()}`;
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
return new Uint8Array([this ? 1 : 0]);
}
+7
View File
@@ -58,10 +58,17 @@ export default class Bytes extends U8a {
return value;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
get encodedLength (): number {
return this.length + Compact.encodeU8a(this.length).length;
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
return isBare
? super.toU8a(isBare)
+9
View File
@@ -49,10 +49,16 @@ export class Seal extends Tuple {
}, value);
}
/**
* @description The wrapped [[Signature]]
*/
get signature (): Signature {
return this.get('signature') as Signature;
}
/**
* @description The wrapped [[U64]] slot
*/
get slot (): U64 {
return this.get('slot') as U64;
}
@@ -87,6 +93,9 @@ export default class Digest extends Struct {
}, value);
}
/**
* @description The [[DigestItem]] logs
*/
get logs (): Vector<DigestItem> {
return this.get('logs') as Vector<DigestItem>;
}
+30
View File
@@ -34,18 +34,30 @@ export class EventData extends Tuple {
this._typeDef = typeDef;
}
/**
* @description The wrapped [[EventMetadata]]
*/
get meta (): EventMetadata {
return this._meta;
}
/**
* @description The method as a string
*/
get method (): string {
return this._method;
}
/**
* @description The section as a string
*/
get section (): string {
return this._section;
}
/**
* @description The [[TypeDef]] for this event
*/
get typeDef (): Array<TypeDef> {
return this._typeDef;
}
@@ -123,26 +135,44 @@ export default class Event extends Struct {
});
}
/**
* @description The wrapped [[EventData]]
*/
get data (): EventData {
return this.get('data') as EventData;
}
/**
* @description The [[EventIndex]], identifying the raw event
*/
get index (): EventIndex {
return this.get('index') as EventIndex;
}
/**
* @description The [[EventMetadata]] with the documentation
*/
get meta (): EventMetadata {
return this.data.meta;
}
/**
* @description The method string identifying the event
*/
get method (): string {
return this.data.method;
}
/**
* @description The section string identifying the event
*/
get section (): string {
return this.data.section;
}
/**
* @description The [[TypeDef]] for the event
*/
get typeDef (): Array<TypeDef> {
return this.data.typeDef;
}
+6
View File
@@ -52,10 +52,16 @@ export default class EventRecord extends Struct {
}, value);
}
/**
* @description The [[Event]] this record refers to
*/
get event (): Event {
return this.get('event') as Event;
}
/**
* @description The [[Phase]] where the event was generated
*/
get phase (): Phase {
return this.get('phase') as Phase;
}
+46 -3
View File
@@ -63,73 +63,116 @@ export default class Extrinsic extends Struct {
return value as any;
}
// expose args so it is compatible with Method (as constructor value)
/**
* @description The arguments passed to for the call, exposes args so it is compatible with [[Method]]
*/
get args (): Array<Codec> {
return this.method.args;
}
// the actual [sectionIndex, methodIndex] as used
/**
* @description The actual `[sectionIndex, methodIndex]` as used in the Method
*/
get callIndex (): Uint8Array {
return this.method.callIndex;
}
/**
* @description The actual data for the Method
*/
get data (): Uint8Array {
return this.method.data;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
get encodedLength (): number {
const length = this.length;
return length + Compact.encodeU8a(length).length;
}
// convernience, encodes the extrinsic and returns the actual hash
/**
* @description Convernience function, encodes the extrinsic and returns the actual hash
*/
get hash (): Hash {
return new Hash(
blake2AsU8a(this.toU8a(), 256)
);
}
/**
* @description `true` id the extrinsic is signed
*/
get isSigned (): boolean {
return this.signature.isSigned;
}
/**
* @description The length of the encoded value
*/
get length (): number {
return this.toU8a(true).length;
}
/**
* @description The [[FunctionMetadata]] that describes the extrinsic
*/
get meta (): FunctionMetadata {
return this.method.meta;
}
/**
* @description The [[Method]] this extrinsic wraps
*/
get method (): Method {
return this.get('method') as Method;
}
/**
* @description The [[ExtrinsicSignature]]
*/
get signature (): ExtrinsicSignature {
return this.get('signature') as ExtrinsicSignature;
}
/**
* @description Add an [[ExtrinsicSignature]] to the extrinsic (already generated)
*/
addSignature (signer: Address | Uint8Array, signature: Uint8Array, nonce: AnyNumber, era?: Uint8Array): Extrinsic {
this.signature.addSignature(signer, signature, nonce, era);
return this;
}
/**
* @description Sign the extrinsic with a specific keypair
*/
sign (signerPair: KeyringPair, nonce: AnyNumber, blockHash: AnyU8a, era?: Uint8Array): Extrinsic {
this.signature.sign(this.method, signerPair, nonce, blockHash, era);
return this;
}
/**
* @description Returns a hex string representation of the value
*/
toHex (): string {
return u8aToHex(this.toU8a());
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
return this.toHex();
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
const encoded = super.toU8a();
+31
View File
@@ -65,6 +65,9 @@ export default class ExtrinsicSignature extends Struct {
return value as any;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
get encodedLength (): number {
// version has 1 byte, signature takes the rest
return 1 + (
@@ -74,26 +77,44 @@ export default class ExtrinsicSignature extends Struct {
);
}
/**
* @description `true` if the signature is valid
*/
get isSigned (): boolean {
return this.signature.length !== 0;
}
/**
* @description The [[ExtrinsicEra]] (mortal or immortal) this signature applies to
*/
get era (): ExtrinsicEra {
return this.get('era') as ExtrinsicEra;
}
/**
* @description The [[Nonce]] for the signature
*/
get nonce (): Nonce {
return this.get('nonce') as Nonce;
}
/**
* @description The actuall [[Signature]] hash
*/
get signature (): Signature {
return this.get('signature') as Signature;
}
/**
* @description The [[Address]] that signed
*/
get signer (): Address {
return this.get('signer') as Address;
}
/**
* @description The encoded version for the signature
*/
get version (): number {
// Version Information.
// 1 byte: version information:
@@ -115,6 +136,9 @@ export default class ExtrinsicSignature extends Struct {
return this;
}
/**
* @description Adds a raw signature
*/
addSignature (_signer: Address | Uint8Array, _signature: Uint8Array, _nonce: AnyNumber, _era: Uint8Array = IMMORTAL_ERA): ExtrinsicSignature {
const signer = new Address(_signer);
const nonce = new Nonce(_nonce);
@@ -124,6 +148,9 @@ export default class ExtrinsicSignature extends Struct {
return this.injectSignature(signature, signer, nonce, era);
}
/**
* @description Generate a payload and pplies the signature from a keypair
*/
sign (method: Method, signerPair: KeyringPair, nonce: AnyNumber, blockHash: AnyU8a, era: Uint8Array = IMMORTAL_ERA): ExtrinsicSignature {
const signer = new Address(signerPair.publicKey());
const signingPayload = new SignaturePayload({
@@ -137,6 +164,10 @@ export default class ExtrinsicSignature extends Struct {
return this.injectSignature(signature, signer, signingPayload.nonce, signingPayload.era);
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
return u8aConcat(
new Uint8Array([this.version]),
+18 -1
View File
@@ -37,29 +37,46 @@ export default class Header extends Struct {
}, value);
}
/**
* @description The wrapped [[BlockNumber]]
*/
get blockNumber (): BlockNumber {
return (this.get('number') as Compact).toBn() as BlockNumber;
}
/**
* @description The wrapped [[Digest]]
*/
get digest (): Digest {
return this.get('digest') as Digest;
}
/**
* @description The wrapped extrisics root as a [[Hash]]
*/
get extrinsicsRoot (): Hash {
return this.get('extrinsicsRoot') as Hash;
}
// convenience, encodes the header and returns the actual hash
/**
* @description Convenience method, encodes the header and calculates the [[Hash]]
*/
get hash (): Hash {
return new Hash(
blake2AsU8a(this.toU8a(), 256)
);
}
/**
* @description The wrapped parent as a [[Hash]]
*/
get parentHash (): Hash {
return this.get('parentHash') as Hash;
}
/**
* @description The wrapped state root as a [[Hash]]
*/
get stateRoot (): Hash {
return this.get('stateRoot') as Hash;
}
+9
View File
@@ -41,14 +41,23 @@ export class RhdJustification extends Struct {
]));
}
/**
* @description The justification [[Hash]]
*/
get hash (): Hash {
return this.get('hash') as Hash;
}
/**
* @description The round this justification wraps as a [[U32]]
*/
get roundNumber (): U32 {
return this.get('roundNumber') as U32;
}
/**
* @description The [[BftAuthoritySignature]] array
*/
get signatures (): Vector<BftAuthoritySignature> {
return this.get('signatures') as Vector<BftAuthoritySignature>;
}
+12
View File
@@ -31,10 +31,16 @@ export default class KeyValue extends Struct {
}, value);
}
/**
* @description The [[StorageKey]]
*/
get key (): StorageKey {
return this.get('key') as StorageKey;
}
/**
* @description The [[StorageData]]
*/
get value (): StorageData {
return this.get('value') as StorageData;
}
@@ -60,10 +66,16 @@ export class KeyValueOption extends Tuple {
}, value);
}
/**
* @description The [[StorageKey]]
*/
get key (): StorageKey {
return this.getAtIndex(0) as StorageKey;
}
/**
* @description The [[Option]] [[StorageData]]
*/
get value (): Option<StorageData> {
return this.getAtIndex(1) as Option<StorageData>;
}
+123 -1
View File
@@ -47,14 +47,23 @@ export class OuterDispatchCall extends Struct {
}, value);
}
/**
* @description The [[U16]] index for the call
*/
get index (): U16 {
return this.get('index') as U16;
}
/**
* @description The name for the call
*/
get name (): Text {
return this.get('name') as Text;
}
/**
* @description The call prefix (or section)
*/
get prefix (): Text {
return this.get('prefix') as Text;
}
@@ -68,10 +77,16 @@ export class OuterDispatchMetadata extends Struct {
}, value);
}
/**
* @description The [[OuterDispathCall]] wrapped
*/
get calls (): Vector<OuterDispatchCall> {
return this.get('calls') as Vector<OuterDispatchCall>;
}
/**
* @description The name for the dispatch
*/
get name (): Text {
return this.get('name') as Text;
}
@@ -86,14 +101,23 @@ export class EventMetadata extends Struct {
}, value);
}
/**
* @description The arguments of [[Type]]
*/
get arguments (): Vector<Type> {
return this.get('arguments') as Vector<Type>;
}
/**
* @description The [[Text]] documentation
*/
get documentation (): Vector<Text> {
return this.get('documentation') as Vector<Text>;
}
/**
* @description The name for the event
*/
get name (): Text {
return this.get('name') as Text;
}
@@ -107,10 +131,16 @@ export class OuterEventMetadataEvent extends Tuple {
}, value);
}
/**
* @description The [[EventMetadata]]
*/
get events (): Vector<EventMetadata> {
return this.getAtIndex(1) as Vector<EventMetadata>;
}
/**
* @description The name of the section
*/
get name (): Text {
return this.getAtIndex(0) as Text;
}
@@ -124,10 +154,16 @@ export class OuterEventMetadata extends Struct {
}, value);
}
/**
* @description The [[OuterEventMetadataEvent]]
*/
get events (): Vector<OuterEventMetadataEvent> {
return this.get('events') as Vector<OuterEventMetadataEvent>;
}
/**
* @description The name of the event
*/
get name (): Text {
return this.get('name') as Text;
}
@@ -141,10 +177,16 @@ export class FunctionArgumentMetadata extends Struct {
}, value);
}
/**
* @description The argument name
*/
get name (): Text {
return this.get('name') as Text;
}
/**
* @description The [[Type]]
*/
get type (): Type {
return this.get('type') as Type;
}
@@ -160,18 +202,30 @@ export class FunctionMetadata extends Struct {
}, value);
}
/**
* @description The [[FunctionArgumentMetadata]] for arguments
*/
get arguments (): Vector<FunctionArgumentMetadata> {
return this.get('arguments') as Vector<FunctionArgumentMetadata>;
}
/**
* @description The [[Text]] documentation
*/
get documentation (): Vector<Text> {
return this.get('documentation') as Vector<Text>;
}
/**
* @description The `[sectionIndex, methodIndex]` call id
*/
get id (): U16 {
return this.get('id') as U16;
}
/**
* @description The call name
*/
get name (): Text {
return this.get('name') as Text;
}
@@ -185,10 +239,16 @@ export class CallMetadata extends Struct {
}, value);
}
/**
* @description The functions available as [[FunctionMetadata]]
*/
get functions (): Vector<FunctionMetadata> {
return this.get('functions') as Vector<FunctionMetadata>;
}
/**
* @description The section name
*/
get name (): Text {
return this.get('name') as Text;
}
@@ -202,10 +262,16 @@ export class ModuleMetadata extends Struct {
}, value);
}
/**
* @description The calls as [[CallMetadata]]
*/
get call (): CallMetadata {
return this.get('call') as CallMetadata;
}
/**
* @description The name
*/
get name (): Text {
return this.get('name') as Text;
}
@@ -225,10 +291,16 @@ export class StorageFunctionType$Map extends Struct {
}, value);
}
/**
* @description The mapped key as [[Type]]
*/
get key (): Type {
return this.get('key') as Type;
}
/**
* @description The mapped value as [[Type]]
*/
get value (): Type {
return this.get('value') as Type;
}
@@ -242,18 +314,30 @@ export class StorageFunctionType extends EnumType<Type | StorageFunctionType$Map
], value, index);
}
/**
* @description `true` if the storage entry is a map
*/
get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description The value as a mapped value
*/
get asMap (): StorageFunctionType$Map {
return this.value as StorageFunctionType$Map;
}
/**
* @description The value as a [[Type]] value
*/
get asType (): Type {
return this.value as Type;
}
/**
* @description Returns the string representation of the value
*/
toString (): string {
return this.isMap
? this.asMap.value.toString()
@@ -278,18 +362,30 @@ export class StorageFunctionMetadata extends Struct {
}, value);
}
/**
* @description The [[Text]] documentation
*/
get documentation (): Vector<Text> {
return this.get('documentation') as Vector<Text>;
}
/**
* @description The key name
*/
get name (): Text {
return this.get('name') as Text;
}
/**
* @description The modifier
*/
get modifier (): StorageFunctionModifier {
return this.get('modifier') as StorageFunctionModifier;
}
/**
* @description The [[StorageFunctionType]]
*/
get type (): StorageFunctionType {
return this.get('type') as StorageFunctionType;
}
@@ -303,10 +399,16 @@ export class StorageMetadata extends Struct {
}, value);
}
/**
* @description The [[StorageFunctionMetadata]] for the section
*/
get functions (): Vector<StorageFunctionMetadata> {
return this.get('functions') as Vector<StorageFunctionMetadata>;
}
/**
* @description The section prefix
*/
get prefix (): Text {
return this.get('prefix') as Text;
}
@@ -321,14 +423,23 @@ export class RuntimeModuleMetadata extends Struct {
}, value);
}
/**
* @description The [[ModuleMetadata]]
*/
get module (): ModuleMetadata {
return this.get('module') as ModuleMetadata;
}
/**
* @description The prefix
*/
get prefix (): Text {
return this.get('prefix') as Text;
}
/**
* @description The optional [[StorageMetadata]]
*/
get storage (): Option<StorageMetadata> {
return this.get('storage') as Option<StorageMetadata>;
}
@@ -370,19 +481,30 @@ export default class RuntimeMetadata extends Struct {
return value;
}
/**
* @description Wrapped [[OuterDispatchCall]]
*/
get calls (): Vector<OuterDispatchCall> {
return (this.get('outerDispatch') as OuterDispatchMetadata).calls;
}
/**
* @description Wrapped [[OuterEventMetadataEvent]]
*/
get events (): Vector<OuterEventMetadataEvent> {
return (this.get('outerEvent') as OuterEventMetadata).events;
}
/**
* @description Wrapped [[RuntimeModuleMetadata]]
*/
get modules (): Vector<RuntimeModuleMetadata> {
return this.get('modules') as Vector<RuntimeModuleMetadata>;
}
// Helper to retrieve a list of all type that are found, sorted and de-deuplicated
/**
* @description Helper to retrieve a list of all type that are found, sorted and de-deuplicated
*/
getUniqTypes (): Array<string> {
const events = this.events.map((module) =>
module.events.map((event) =>
+21 -1
View File
@@ -50,7 +50,12 @@ export interface Extrinsics {
const extrinsicFns: { [index: string]: ExtrinsicFunction } = {};
class MethodIndex extends U8aFixed {
/**
* @name MethodIndex
* @description
* A wrapper around the `[sectionIndex, methodIndex]` value that uniquely identifies a method
*/
export class MethodIndex extends U8aFixed {
constructor (value?: AnyU8a) {
super(value, 16);
}
@@ -185,23 +190,38 @@ export default class Method extends Struct {
);
}
/**
* @description The arguments for the function call
*/
get args (): Array<Codec> {
// FIXME This should return a Struct instead of an Array
return [...(this.get('args') as Struct).values()];
}
/**
* @description Thge argument defintions
*/
get argsDef (): ArgsDef {
return Method.getArgsDef(this.meta);
}
/**
* @description The encoded `[sectionIndex, methodIndex]` identifier
*/
get callIndex (): Uint8Array {
return (this.get('callIndex') as MethodIndex).toU8a();
}
/**
* @description The encoded data
*/
get data (): Uint8Array {
return (this.get('args') as Struct).toU8a();
}
/**
* @description The [[FunctionMetadata]]
*/
get meta (): FunctionMetadata {
return this._meta;
}
+27
View File
@@ -39,14 +39,23 @@ export class BftAtReport extends Struct {
}, value);
}
/**
* @description The first report [[BftHashSignature]]
*/
get a (): BftHashSignature {
return this.get('a') as BftHashSignature;
}
/**
* @description The second report [[BftHashSignature]]
*/
get b (): BftHashSignature {
return this.get('b') as BftHashSignature;
}
/**
* @description The round this report applies to as [[U32]]
*/
get round (): U32 {
return this.get('round') as U32;
}
@@ -65,10 +74,16 @@ export class BftProposeOutOfTurn extends Struct {
}, value);
}
/**
* @description The [[BftHashSignature]] the report applies to
*/
get a (): BftHashSignature {
return this.get('a') as BftHashSignature;
}
/**
* @description The round as [[u32]]
*/
get round (): U32 {
return this.get('round') as U32;
}
@@ -136,18 +151,30 @@ export default class MisbehaviorReport extends Struct {
}, value);
}
/**
* @description The [[MisbehaviorKind]]
*/
get misbehavior (): MisbehaviorKind {
return this.get('misbehavior') as MisbehaviorKind;
}
/**
* @description The [[Hash]] of the parent block
*/
get parentHash (): Hash {
return this.get('parentHash') as Hash;
}
/**
* @description The [[BlockNumber]] of the parent
*/
get parentNumber (): BlockNumber {
return this.get('parentNumber') as BlockNumber;
}
/**
* @description The [[authorityId]] the report applies to
*/
get target (): AuthorityId {
return this.get('target') as AuthorityId;
}
+35 -3
View File
@@ -15,11 +15,13 @@ const BITLENGTH: UIntBitLength = 64;
* @description
* A wrapper around seconds/timestamps. Internally the representation only has
* second precicion (aligning with Rust), so any numbers passed an/out are always
* per-second. For any encoding/decoding the 1000 multiplier would be applied to
* get it in line with JavaScript formats
* per-second. For any encoding/decoding the 1000 multiplier would be applied to
* get it in line with JavaScript formats. It extends the base JS `Date` object
* and has all the methods available that are applicable to any `Date`
* @noInheritDoc
*/
export default class Moment extends Date implements Codec {
public raw: Date; // FIXME Remove this once we convert all types out of Base
protected raw: Date; // FIXME Remove this once we convert all types out of Base
constructor (value: Moment | Date | AnyNumber = 0) {
super(
@@ -43,30 +45,60 @@ export default class Moment extends Date implements Codec {
);
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
get encodedLength (): number {
return BITLENGTH / 8;
}
/**
* @description Returns the number of bits in the value
*/
bitLength (): UIntBitLength {
return BITLENGTH;
}
/**
* @description Returns the BN representation of the timestamp
*/
toBn (): BN {
return new BN(this.toNumber());
}
/**
* @description Returns a hex string representation of the value
*/
toHex (): string {
return bnToHex(this.toBn(), BITLENGTH);
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
return this.toNumber();
}
/**
* @description Returns the number representation for the timestamp
*/
toNumber (): number {
return Math.ceil(this.getTime() / 1000);
}
/**
* @description Returns the string representation of the value
*/
toString (): string {
// only included here since we do not inherit docs
return super.toString();
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
return bnToU8a(this.toNumber(), BITLENGTH, true);
}
+1 -1
View File
@@ -11,5 +11,5 @@ import U64 from './U64';
* with extrinsics to determine the order of execution. implemented as a Substrate
* [[U64]]
*/
export default class Index extends U64 {
export default class Nonce extends U64 {
}
+6
View File
@@ -10,10 +10,16 @@ import { Codec } from './types';
* Implements a type that does not contain anything (apart from `null`)
*/
export default class Null implements Codec {
/**
* @description The length of the value when encoded as a Uint8Array
*/
get encodedLength (): number {
return 0;
}
/**
* @description Returns a hex string representation of the value
*/
toHex (): string {
return '0x';
}
+24
View File
@@ -40,10 +40,16 @@ export class RuntimeVersionApi extends Tuple {
}, value);
}
/**
* @description The [[ApiId]]
*/
get id (): ApiId {
return this.getAtIndex(0) as ApiId;
}
/**
* @description The specific version as [[U32]]
*/
get version (): U32 {
return this.getAtIndex(1) as U32;
}
@@ -81,26 +87,44 @@ export default class RuntimeVersion extends Struct {
]));
}
/**
* @description The available APIs as [[RuntimeVersionApi]]
*/
get apis (): Vector<RuntimeVersionApi> {
return this.get('apis') as Vector<RuntimeVersionApi>;
}
/**
* @description The authoring version as [[U32]]
*/
get authoringVersion (): U32 {
return this.get('authoringVersion') as U32;
}
/**
* @description The implementation name
*/
get implName (): Text {
return this.get('implName') as Text;
}
/**
* @description The implementation version
*/
get implVersion (): U32 {
return this.get('implVersion') as U32;
}
/**
* @description The specification name
*/
get specName (): Text {
return this.get('specName') as Text;
}
/**
* @description The specification version
*/
get specVersion (): U32 {
return this.get('specVersion') as U32;
}
+21
View File
@@ -41,26 +41,44 @@ export default class SignaturePayload extends Struct {
}, value);
}
/**
* @description `true` if the payload refers to a valid signature
*/
get isSigned (): boolean {
return !!(this._signature && this._signature.length === 64);
}
/**
* @description The block [[Hash]] the signature applies to (mortal/immortal)
*/
get blockHash (): Hash {
return this.get('blockHash') as Hash;
}
/**
* @description The [[Method]] contained in the payload
*/
get method (): Method {
return this.get('method') as Method;
}
/**
* @description The [[ExtrinsicEra]]
*/
get era (): ExtrinsicEra {
return this.get('era') as ExtrinsicEra;
}
/**
* @description The [[Nonce]]
*/
get nonce (): Nonce {
return this.get('nonce') as Nonce;
}
/**
* @description The raw signature as a `Uint8Array`
*/
get signature (): Uint8Array {
if (!this.isSigned) {
throw new Error('Transaction is not signed');
@@ -69,6 +87,9 @@ export default class SignaturePayload extends Struct {
return this._signature as Uint8Array;
}
/**
* @description Sign the payload with the keypair
*/
sign (signerPair: KeyringPair): Uint8Array {
this._signature = signerPair.sign(this.toU8a());
+6
View File
@@ -26,10 +26,16 @@ export default class SignedBlock extends Struct {
}, value);
}
/**
* @description The wrapped [[Block]]
*/
get block (): Block {
return this.get('block') as Block;
}
/**
* @description The wrapped [[Justification]]
*/
get justification (): Justification {
return this.get('justification') as Justification;
}
+6
View File
@@ -29,10 +29,16 @@ export default class StorageChangeSet extends Struct {
}, value);
}
/**
* @description The applicable changes as [[KeyValueOption]]
*/
get changes (): Vector<KeyValueOption> {
return this.get('changes') as Vector<KeyValueOption>;
}
/**
* @description The block [[Hash]]
*/
get block (): Hash {
return this.get('block') as Hash;
}
+3
View File
@@ -60,6 +60,9 @@ export default class StorageKey extends Bytes {
return null;
}
/**
* @description The output type, `null` when not available
*/
get outputType (): string | null {
return this._outputType;
}
+32 -1
View File
@@ -11,7 +11,9 @@ import Compact from './codec/Compact';
* @name Text
* @description
* This is a string wrapper, along with the length. It is used both for strings as well
* as items such as documentation.
* as items such as documentation. It simply extends the standard JS `String` built-in
* object, inheriting all methods exposed from `String`.
* @noInheritDoc
*/
// TODO
// - Strings should probably be trimmed (docs do come through with extra padding)
@@ -39,18 +41,47 @@ export default class Text extends String implements Codec {
return `${value}`;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
get encodedLength (): number {
return this.length + Compact.encodeU8a(this.length).length;
}
/**
* @description The length of the value
*/
get length (): number {
// only included here since we ignore inherited docs
return super.length;
}
/**
* @description Returns a hex string representation of the value
*/
toHex (): string {
return u8aToHex(this.toU8a());
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
return this.toString();
}
/**
* @description Returns the string representation of the value
*/
toString (): string {
// only included here since we do not inherit docs
return super.toString();
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
const encoded = stringToU8a(this.toString());
+7
View File
@@ -61,6 +61,9 @@ export default class Type extends Text {
}, value).trim();
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
get encodedLength (): number {
// NOTE Length is used in the decoding calculations, so return the original (pre-cleanup)
// length of the data. Since toU8a is disabled, this does not affect encoding, but rather
@@ -68,6 +71,10 @@ export default class Type extends Text {
return this._originalLength + Compact.encodeU8a(this._originalLength).length;
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
// Note Since we are mangling what we get in beyond recognition, we really should
// not allow the re-encoding. Additionally, this is probably more of a decoder-only
+6
View File
@@ -26,10 +26,16 @@ export default class ValidatorPrefs extends Struct {
}, value);
}
/**
* @description The unstake threshold as [[U32]]
*/
get unstakeThreshold (): U32 {
return this.get('unstakeThreshold') as U32;
}
/**
* @description The payment config for the validator as [[Balance]]
*/
get validatorPayment (): Balance {
return this.get('validatorPayment') as Balance;
}
+12
View File
@@ -14,6 +14,7 @@ import { Codec, Constructor, ConstructorDef } from '../types';
* values in the constructor and it manages the decoding. It is important that the constructor
* values matches 100% to the order in th Rust code, i.e. don't go crazy and make it alphabetical,
* it needs to decoded in the specific defined order.
* @noInheritDoc
*/
export default class Struct<
// The actual Class structure, i.e. key -> Class
@@ -126,6 +127,9 @@ export default class Struct<
};
}
/**
* @description Returns the Type description to sthe structure
*/
get Type (): E {
return this._Types;
}
@@ -139,6 +143,14 @@ export default class Struct<
}, 0);
}
/**
* @description Returns a specific names entry in the structure
* @param name The name of the entry to retrieve
*/
get (name: keyof S): Codec | undefined {
return super.get(name);
}
/**
* @description Returns the values of a member at a specific index (Rather use get(name) for performance)
*/
+14 -2
View File
@@ -13,6 +13,7 @@ import { AnyU8a, Codec } from '../types';
* from other implementations wher it will consume the full Uint8Array as passed to
* it. As such it is meant to be subclassed where the wrapper takes care of the
* actual lengths instead of used directly.
* @noInheritDoc
*/
export default class U8a extends Uint8Array implements Codec {
constructor (value: AnyU8a) {
@@ -36,8 +37,19 @@ export default class U8a extends Uint8Array implements Codec {
return this.length;
}
// Create a new subarray from the actual buffer. This is needed
// for compat reasons since a new Uint8Array gets returned here
/**
* @description The length of the value
*/
get length (): number {
// only included here since we ignore inherited docs
return super.length;
}
/**
* @description Create a new subarray from the actual buffer. This is needed for compat reasons since a new Uint8Array gets returned here
* @param begin The position to start at
* @param end The position to end at
*/
subarray (begin: number, end?: number): Uint8Array {
return Uint8Array.from(this).subarray(begin, end);
}
+12 -1
View File
@@ -16,7 +16,9 @@ export const DEFAULT_UINT_BITS = 64;
* @description
* A generic number codec. For Substrate all numbers are LE encoded, this handles the encoding
* and decoding of those numbers. Upon construction the bitLength is provided and any additional
* use keeps the number to this length.
* use keeps the number to this length. This extends `BN`, so all methods available on a normal
* `BN` object is available here.
* @noInheritDoc
*/
// TODO:
// - Apart from encoding/decoding we don't actually keep check on the sizes, is this good enough?
@@ -86,6 +88,15 @@ export default class UInt extends BN implements Codec {
: this.toNumber();
}
/**
* @description Returns the string representation of the value
* @param base The base to use for the conversion
*/
toString (base?: number): string {
// only included here since we do not inherit docs
return super.toString(base);
}
/**
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
+19
View File
@@ -13,6 +13,7 @@ import { Codec, Constructor } from '../types';
* This manages codec arrays. Intrernally it keeps track of the length (as decoded) and allows
* construction with the passed `Type` in the constructor. It is an extension to Array, providing
* specific encoding/decoding on top of the base type.
* @noInheritDoc
*/
export default class Vector<
T extends Codec
@@ -74,6 +75,14 @@ export default class Vector<
}, Compact.encodeU8a(this.length).length);
}
/**
* @description The length of the value
*/
get length (): number {
// only included here since we ignore inherited docs
return super.length;
}
/**
* @description Converts the Object to an standard JavaScript Array
*/
@@ -130,10 +139,20 @@ export default class Vector<
// we want it to return an Array. We only override the methods that return a
// new instance.
/**
* @description Filters the array with the callback
* @param callbackfn The filter function
* @param thisArg The `this` object to apply the result to
*/
filter (callbackfn: (value: T, index: number, array: Array<T>) => any, thisArg?: any): Array<T> {
return this.toArray().filter(callbackfn, thisArg);
}
/**
* @description Maps the array with the callback
* @param callbackfn The mapping function
* @param thisArg The `this` onject to apply the result to
*/
map<U> (callbackfn: (value: T, index: number, array: Array<T>) => U, thisArg?: any): Array<U> {
return this.toArray().map(callbackfn, thisArg);
}