From 544034bf2e8ebc39161dbf8e2b1368d5a9917186 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 28 Jun 2018 17:15:09 +0200 Subject: [PATCH] Allow hex or number[] -> u8a conversion for 'bytes' (#155) --- package.json | 2 +- packages/type-primitives/src/json/bytes/decode.spec.js | 6 ++++++ packages/type-primitives/src/json/bytes/decode.ts | 4 +++- packages/type-primitives/src/json/types.d.ts | 2 +- yarn.lock | 6 +++--- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 7785ac93a..cb475c12f 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,6 @@ }, "devDependencies": { "@polkadot/dev": "^0.20.8", - "@polkadot/ts": "^0.1.5" + "@polkadot/ts": "^0.1.6" } } diff --git a/packages/type-primitives/src/json/bytes/decode.spec.js b/packages/type-primitives/src/json/bytes/decode.spec.js index 1d8e1ab20..70f4ef6a7 100644 --- a/packages/type-primitives/src/json/bytes/decode.spec.js +++ b/packages/type-primitives/src/json/bytes/decode.spec.js @@ -10,4 +10,10 @@ describe('bytesDecode', () => { bytesDecode('0x0012345600') ).toEqual(new Uint8Array([0x00, 0x12, 0x34, 0x56, 0x00])); }); + + it('decodes byte array (number', () => { + expect( + bytesDecode([0x00, 0x12, 0x34, 0x56, 0x00]) + ).toEqual(new Uint8Array([0x00, 0x12, 0x34, 0x56, 0x00])); + }); }); diff --git a/packages/type-primitives/src/json/bytes/decode.ts b/packages/type-primitives/src/json/bytes/decode.ts index 8db1ca943..b08730955 100644 --- a/packages/type-primitives/src/json/bytes/decode.ts +++ b/packages/type-primitives/src/json/bytes/decode.ts @@ -8,5 +8,7 @@ import { JsonBytes } from '../types'; import hexToU8a from '@polkadot/util/hex/toU8a'; export default function bytesDecode (value: JsonBytes): Bytes { - return hexToU8a(value); + return Array.isArray(value) + ? new Uint8Array(value) + : hexToU8a(value); } diff --git a/packages/type-primitives/src/json/types.d.ts b/packages/type-primitives/src/json/types.d.ts index 86291b292..c2a08b1c3 100644 --- a/packages/type-primitives/src/json/types.d.ts +++ b/packages/type-primitives/src/json/types.d.ts @@ -5,7 +5,7 @@ export type JsonBnType = string; export type JsonHash = string; -export type JsonBytes = string; +export type JsonBytes = string | Array; export type JsonH160 = JsonHash; export type JsonH256 = JsonHash; export type JsonH512 = JsonHash; diff --git a/yarn.lock b/yarn.lock index ca2e29fa1..1f1dccd87 100644 --- a/yarn.lock +++ b/yarn.lock @@ -736,9 +736,9 @@ tslint-config-semistandard "^7.0.0" typescript "^2.9.2" -"@polkadot/ts@^0.1.5": - version "0.1.5" - resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.5.tgz#bd944090a788d9379866059fb61b5708b4696cc7" +"@polkadot/ts@^0.1.6": + version "0.1.6" + resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.6.tgz#0138490e0e625ea34a9931be94fd436dd6015c7d" "@types/async@^2.0.49": version "2.0.49"