diff --git a/assembly/Arrays/ByteArray.ts b/assembly/Arrays/ByteArray.ts index 35f91d2..93d52cb 100644 --- a/assembly/Arrays/ByteArray.ts +++ b/assembly/Arrays/ByteArray.ts @@ -27,7 +27,7 @@ export class ByteArray extends AbstractArray { public toHexString (): string { let result = "0x"; for (let i = 0; i < super.values.length; i++) { - const str = super.values[i].toString(); + const str = super.values[i].toString(16); if (str.length == 1) { result += "0"; } diff --git a/assembly/__tests__/ScaleString.spec.ts b/assembly/__tests__/ScaleString.spec.ts index 831602c..59439cd 100644 --- a/assembly/__tests__/ScaleString.spec.ts +++ b/assembly/__tests__/ScaleString.spec.ts @@ -47,6 +47,15 @@ describe("String", () => { expect(scaleString1.toString()).toStrictEqual("da"); }); + it("should encode to hex", () => { + const scaleString = new ScaleString("as-scale-codec"); + expect(scaleString.toHexString()).toStrictEqual("0x61732d7363616c652d636f646563"); + const scaleString1 = new ScaleString("The 1963 Impala featured rectilinear styling with an engine-turned aluminum rear taillight panel surrounded by a chrome border on SS models."); + expect(scaleString1.toHexString()).toStrictEqual("0x546865203139363320496d70616c612066656174757265642072656374696c696e656172207374796c696e67207769746820616e20656e67696e652d7475726e656420616c756d696e756d2072656172207461696c6c696768742070616e656c20737572726f756e6465642062792061206368726f6d6520626f72646572206f6e205353206d6f64656c732e"); + const scaleString2 = new ScaleString("A set of words that is complete in itself, typically containing a subject and predicate"); + expect(scaleString2.toHexString()).toStrictEqual("0x4120736574206f6620776f726473207468617420697320636f6d706c65746520696e20697473656c662c207479706963616c6c7920636f6e7461696e696e672061207375626a65637420616e6420707265646963617465"); + }) + itThrows("should throw on incorrect encoding", () => { ScaleString.fromU8a([0x04]); // Encoded length = 1, actual data length = 0 }); diff --git a/assembly/index.ts b/assembly/index.ts index 09ac127..e425cd6 100644 --- a/assembly/index.ts +++ b/assembly/index.ts @@ -4,7 +4,7 @@ export * from "./Byte"; export * from "./BytesReader"; export * from "./Hash"; export * from "./Int"; -export * from "./interfaces/Codec"; +export * from "./interfaces"; export * from "./ScaleMap"; export * from "./ScaleString"; export * from "./UInt"; diff --git a/assembly/interfaces/index.ts b/assembly/interfaces/index.ts new file mode 100644 index 0000000..e6c2fdb --- /dev/null +++ b/assembly/interfaces/index.ts @@ -0,0 +1,3 @@ +export * from './Codec'; +export * from './DecodedData'; +export * from './UnwrappableCodec'; diff --git a/package-lock.json b/package-lock.json index b20936f..0512bed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "as-scale-codec", - "version": "0.1.1", + "version": "0.2.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 6ad43ab..0d0cd10 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "as-scale-codec", - "version": "0.1.1", + "version": "0.2.1", "description": "AssemblyScript implementation of the SCALE codec used in the Parity Substrate framework", "main": "./assembly/index.ts", "contributors": [