Expand api-codec types (#163)

* Add missing U* (number) types

* Missing number types & indexes

* Add H256, H512, Hash & Signature

* Add Bool, KeyValue & U8a (base)

* Fix LengthCompact encoder (with tests)

* AccountId

* AccountId toU8a

* Order imports

* Apply type aliasses, PropIndex -> ProposalIndex

* Cleanup mappers

* Add fromJSON to Type

* Spliut with Address, AccountId & AccountIndex

* Add some comments around the types

* instanceOf check

* Address can construct from AccountIndex or AccountId

* Remove Address.encode (unused)

* Allow instance of class in constructor

* Cleanup instanceof (String, Bool, H256, H512)

* AccountId/AccountIndex check around CodecU8a

* Fix unused imports

* Additional in-constructor instanceof constructions

* u8a allows hex, string or u8a inputs

* Metadata.fromJSON

* Add Header

* Allow CodecNumber to take hex/string inputs

* fromJSON uses decoder

* Remove LengthFixed (only compact used), add tests

* Some tests around Type

* Remove unused package

* PR comments

* Expand tests (and fix some bug - yay for tests)

* Rename /base -> /codec
This commit is contained in:
Jaco Greeff
2018-09-25 13:33:28 +02:00
committed by GitHub
parent 3a54bf18f6
commit 60d3d6396a
50 changed files with 1296 additions and 282 deletions
+41
View File
@@ -0,0 +1,41 @@
// 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.
// The actual base class for any type. As it stands, this should not be used on it's own,
// so we expose the interface, but throw on anything. This allows subclasses to actaully
// only implement part (things that are unused/unexpected should just error-out)
//
// TODO
// - This could probably be abstract, as long as we have the functions abstract as well
export default class CodecBase <T = any> {
raw: T;
constructor (value?: any) {
this.raw = value;
}
byteLength (): number {
throw new Error('CodecBase::byteLength: unimplemented');
}
fromJSON (input: any): CodecBase <T> {
throw new Error('CodecBase::fromJSON: unimplemented');
}
fromU8a (input: Uint8Array): CodecBase <T> {
throw new Error('CodecBase::fromU8a: unimplemented');
}
toJSON (): any {
throw new Error('CodecBase::toJSON: unimplemented');
}
toString (): string {
throw new Error('CodecBase::toString: unimplemented');
}
toU8a (): Uint8Array {
throw new Error('CodecBase::toU8a: unimplemented');
}
}