From 0f1684dcbc1d795f3437344f1a52207817ffb54b Mon Sep 17 00:00:00 2001 From: cdetrio Date: Mon, 5 Aug 2019 16:33:03 -0400 Subject: [PATCH] updates for latest asc version --- assembly/__tests__/dataTypes.spec.ts | 38 ++++++++++++++++++---------- assembly/index.ts | 17 +++++++------ assembly/type.ts | 15 ++++++++--- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/assembly/__tests__/dataTypes.spec.ts b/assembly/__tests__/dataTypes.spec.ts index fa4ce9d..17ba5a9 100644 --- a/assembly/__tests__/dataTypes.spec.ts +++ b/assembly/__tests__/dataTypes.spec.ts @@ -1,6 +1,9 @@ import * as RLP from '../index'; import {RLPData} from "../type"; +const nullUint8Arr = new Uint8Array(0); +const nullRlpData = new Array(); + function arrayToUint8Array(array: u8[]): Uint8Array { let len = array.length; let res = new Uint8Array(len); @@ -11,13 +14,13 @@ function arrayToUint8Array(array: u8[]): Uint8Array { } function bytesToString(bytes: Uint8Array): string { - return String.fromUTF8((bytes.buffer.data + bytes.byteOffset) as usize, bytes.byteLength); + return String.UTF8.decodeUnsafe((bytes.buffer as usize) + bytes.byteOffset, bytes.byteLength); } function stringToBytes(s: string): Uint8Array { - let len = s.lengthUTF8 - 1; - let bytes = new Uint8Array(len); - memory.copy(bytes.buffer.data, s.toUTF8(), len); + let len = String.UTF8.byteLength(s); + let str_bytes = String.UTF8.encode(s); + let bytes = Uint8Array.wrap(str_bytes, 0, len); return bytes; } @@ -96,14 +99,14 @@ describe('RLP decoding:', function() { it('first byte < 0x7f, return byte itself', (): void => { let decoded = RLP.decode(arrayToUint8Array([97])); expect(decoded.buffer.length).toBe(1); - expect(decoded.children).toBeNull(); + expect(decoded.children).toStrictEqual(nullRlpData); expect(bytesToString(decoded.buffer)).toBe('a'); }); it('first byte < 0xb7, data is everything except first byte', (): void => { let decoded = RLP.decode(arrayToUint8Array([131, 100, 111, 103])); expect(decoded.buffer.length).toBe(3); - expect(decoded.children).toBeNull(); + expect(decoded.children).toStrictEqual(nullRlpData); expect(bytesToString(decoded.buffer)).toBe('dog'); }); @@ -111,13 +114,13 @@ describe('RLP decoding:', function() { let testString = 'This function takes in a data, convert it to buffer if not, and a length for recursion'; let encoded = RLP.encode(new RLPData(stringToBytes(testString), null)); let decoded = RLP.decode(encoded); - expect(decoded.children).toBeNull(); + expect(decoded.children).toStrictEqual(nullRlpData); expect(bytesToString(decoded.buffer)).toBe(testString); }); it('list of items', (): void => { let decodedBufferArray = RLP.decode(arrayToUint8Array([204, 131, 100, 111, 103, 131, 103, 111, 100, 131, 99, 97, 116])); - expect(decodedBufferArray.buffer).toBeNull(); + expect(decodedBufferArray.buffer).toStrictEqual(nullUint8Arr); expect(decodedBufferArray.children.length).toBe(3); // as-pect does not support deep equal due to lack of metadata support from assemblyscript. expect(bytesToString(decodedBufferArray.children[0].buffer)).toBe('dog'); @@ -127,11 +130,20 @@ describe('RLP decoding:', function() { it('list over 55 bytes long', (): void => { let testString: string[] = ['This', 'function', 'takes', 'in', 'a', 'data', 'convert', 'it', 'to', 'buffer', 'if', 'not', 'and', 'a', 'length', 'for', 'recursion', 'a1', 'a2', 'a3', 'ia4', 'a5', 'a6', 'a7', 'a8', 'ba9']; - let rlpData = new RLPData(null, testString.map(s => new RLPData(stringToBytes(s), null))); + // map doesn't work. could be this issue https://github.com/AssemblyScript/assemblyscript/issues/696 + //let rlpData = new RLPData(null, testString.map(s => new RLPData(stringToBytes(s), null))); + + let rlp_string_datas: RLPData[] = new Array(); + for (let i=0; i(encoded.length).toBe(114); let decoded = RLP.decode(encoded); - expect(decoded.buffer).toBeNull(); + expect(decoded.buffer).toStrictEqual(nullUint8Arr); expect(decoded.children.length).toBe(testString.length); for (let i = 0; i < testString.length; i++) { expect(bytesToString(decoded.children[i].buffer)).toBe(testString[i]); @@ -141,7 +153,7 @@ describe('RLP decoding:', function() { describe('null values', function() { it('encode a null array', function() { - let encoded = RLP.encode(new RLPData(new Uint8Array(0), null)); + let encoded = RLP.encode(new RLPData(nullUint8Arr, null)); expect(encoded[0]).toBe(0x80); expect(encoded.length).toBe(1); }); @@ -149,7 +161,7 @@ describe('null values', function() { it('should decode a null value', function() { let decoded = RLP.decode(arrayToUint8Array([0x80])); expect(decoded.buffer.length).toBe(0); - expect(decoded.children).toBeNull(); + expect(decoded.children).toStrictEqual(nullRlpData); }); }); @@ -165,6 +177,6 @@ describe('zero values', function() { let decoded = RLP.decode(arrayToUint8Array([0])); expect(decoded.buffer[0]).toBe(0); expect(decoded.buffer.length).toBe(1); - expect(decoded.children).toBeNull(); + expect(decoded.children).toStrictEqual(nullRlpData); }) }); diff --git a/assembly/index.ts b/assembly/index.ts index ad11a6d..06001b9 100644 --- a/assembly/index.ts +++ b/assembly/index.ts @@ -23,7 +23,7 @@ function safeParseInt(v: string, base: u32): u32 { if (v.slice(0, 2) == '00') { throw new Error('invalid RLP: extra zeros'); } - return parseI32(v, base) as u32; + return I32.parseInt(v, base) as u32; } /** Transform an integer into its hexadecimal value */ @@ -46,18 +46,18 @@ function bytesToHex(bytes: Uint8Array): string { res[i*2] = hex.charCodeAt(0); res[i*2+1] = hex.charCodeAt(1); } - return String.fromUTF8(res.buffer.data, res.byteLength); + return String.UTF8.decodeUnsafe((res.buffer as usize) + res.byteOffset, res.byteLength); } function hexToBytes(hex: string): Uint8Array { if (!hex.length) { - return null; + return new Uint8Array(0); } assert(hex.length % 2 == 0); let byteLength = hex.length / 2; let res = new Uint8Array(byteLength); for (let i = 0; i < byteLength; i++) { - res[i] = parseI32(hex.substr(i*2, 2), 16) as u8; + res[i] = I32.parseInt(hex.substring(i*2, 2), 16) as u8; } return res; } @@ -65,8 +65,8 @@ function hexToBytes(hex: string): Uint8Array { function concatUint8Array(arr1: Uint8Array, arr2: Uint8Array): Uint8Array { let len = arr1.byteLength + arr2.byteLength; let res = new Uint8Array(len); - memory.copy(res.buffer.data, arr1.buffer.data + arr1.byteOffset, arr1.byteLength); - memory.copy(res.buffer.data + arr1.length, arr2.buffer.data + arr2.byteOffset, arr2.byteLength); + memory.copy((res.buffer as usize) + res.byteOffset, (arr1.buffer as usize) + arr1.byteOffset, arr1.byteLength); + memory.copy((res.buffer as usize) + res.byteOffset + arr1.byteLength, (arr2.buffer as usize) + arr2.byteOffset, arr2.byteLength); return res; } @@ -75,7 +75,8 @@ function concatUint8Arrays(arrays: Array): Uint8Array { let res = new Uint8Array(len); let counter = 0; for (let i = 0; i < arrays.length; i++) { - memory.copy(res.buffer.data + counter, arrays[i].buffer.data + arrays[1].byteOffset, arrays[i].byteLength); + // TODO: check that arrays[1].byteOffset is right and covered by tests + memory.copy((res.buffer as usize) + res.byteOffset + counter, (arrays[i].buffer as usize) + arrays[1].byteOffset, arrays[i].byteLength); counter += arrays[i].byteLength; } return res; @@ -88,7 +89,7 @@ function concatUint8Arrays(arrays: Array): Uint8Array { * @returns returns rlp encoded byte array. **/ export function encode(input: RLPData): Uint8Array { - if (input.children) { + if (input.children.length) { let output = new Array(); for (let i = 0; i < input.children.length; i++) { output.push(encode(input.children[i])); diff --git a/assembly/type.ts b/assembly/type.ts index fd98910..591e89e 100644 --- a/assembly/type.ts +++ b/assembly/type.ts @@ -6,8 +6,17 @@ export class RLPData { buffer: Uint8Array; children: RLPData[]; - constructor(input: Uint8Array, children: RLPData[]) { - this.buffer = input; - this.children = children; + constructor(input: Uint8Array | null, children: RLPData[] | null) { + if (input) { + this.buffer = input; + } else { + this.buffer = new Uint8Array(0); + } + + if (children) { + this.children = children; + } else { + this.children = new Array(); + } } }