Skip to content
This repository has been archived by the owner on Feb 13, 2021. It is now read-only.

updates for latest asc version #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions assembly/__tests__/dataTypes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as RLP from '../index';
import {RLPData} from "../type";

const nullUint8Arr = new Uint8Array(0);
const nullRlpData = new Array<RLPData>();

function arrayToUint8Array(array: u8[]): Uint8Array {
let len = array.length;
let res = new Uint8Array(len);
Expand All @@ -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;
}

Expand Down Expand Up @@ -96,28 +99,28 @@ describe('RLP decoding:', function() {
it('first byte < 0x7f, return byte itself', (): void => {
let decoded = RLP.decode(arrayToUint8Array([97]));
expect<usize>(decoded.buffer.length).toBe(1);
expect<RLPData[]>(decoded.children).toBeNull();
expect<RLPData[]>(decoded.children).toStrictEqual(nullRlpData);
expect<string>(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<usize>(decoded.buffer.length).toBe(3);
expect<RLPData[]>(decoded.children).toBeNull();
expect<RLPData[]>(decoded.children).toStrictEqual(nullRlpData);
expect<string>(bytesToString(decoded.buffer)).toBe('dog');
});

it('strings over 55 bytes long', (): void => {
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<RLPData[]>(decoded.children).toBeNull();
expect<RLPData[]>(decoded.children).toStrictEqual(nullRlpData);
expect<string>(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<Uint8Array>(decodedBufferArray.buffer).toBeNull();
expect<Uint8Array>(decodedBufferArray.buffer).toStrictEqual(nullUint8Arr);
expect<usize>(decodedBufferArray.children.length).toBe(3);
// as-pect does not support deep equal due to lack of metadata support from assemblyscript.
expect<string>(bytesToString(decodedBufferArray.children[0].buffer)).toBe('dog');
Expand All @@ -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<RLPData>(s => new RLPData(stringToBytes(s), null)));
// map<RLPData> doesn't work. could be this issue https://github.com/AssemblyScript/assemblyscript/issues/696
//let rlpData = new RLPData(null, testString.map<RLPData>(s => new RLPData(stringToBytes(s), null)));

let rlp_string_datas: RLPData[] = new Array<RLPData>();
for (let i=0; i<testString.length; i++) {
let elem_string = new RLPData(stringToBytes(testString[i]), nullRlpData);
rlp_string_datas.push(elem_string);
}
let rlpData = new RLPData(nullUint8Arr, rlp_string_datas);

let encoded = RLP.encode(rlpData);
expect<usize>(encoded.length).toBe(114);
let decoded = RLP.decode(encoded);
expect<Uint8Array>(decoded.buffer).toBeNull();
expect<Uint8Array>(decoded.buffer).toStrictEqual(nullUint8Arr);
expect<usize>(decoded.children.length).toBe(testString.length);
for (let i = 0; i < testString.length; i++) {
expect<string>(bytesToString(decoded.children[i].buffer)).toBe(testString[i]);
Expand All @@ -141,15 +153,15 @@ 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<u8>(encoded[0]).toBe(0x80);
expect<usize>(encoded.length).toBe(1);
});

it('should decode a null value', function() {
let decoded = RLP.decode(arrayToUint8Array([0x80]));
expect<usize>(decoded.buffer.length).toBe(0);
expect<RLPData[]>(decoded.children).toBeNull();
expect<RLPData[]>(decoded.children).toStrictEqual(nullRlpData);
});
});

Expand All @@ -165,6 +177,6 @@ describe('zero values', function() {
let decoded = RLP.decode(arrayToUint8Array([0]));
expect<u8>(decoded.buffer[0]).toBe(0);
expect<usize>(decoded.buffer.length).toBe(1);
expect<RLPData[]>(decoded.children).toBeNull();
expect<RLPData[]>(decoded.children).toStrictEqual(nullRlpData);
})
});
17 changes: 9 additions & 8 deletions assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -46,27 +46,27 @@ 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;
}

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;
}

Expand All @@ -75,7 +75,8 @@ function concatUint8Arrays(arrays: Array<Uint8Array>): 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;
Expand All @@ -88,7 +89,7 @@ function concatUint8Arrays(arrays: Array<Uint8Array>): Uint8Array {
* @returns returns rlp encoded byte array.
**/
export function encode(input: RLPData): Uint8Array {
if (input.children) {
if (input.children.length) {
let output = new Array<Uint8Array>();
for (let i = 0; i < input.children.length; i++) {
output.push(encode(input.children[i]));
Expand Down
15 changes: 12 additions & 3 deletions assembly/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RLPData>();
}
}
}