Skip to content

Commit

Permalink
Merge pull request #61 from LimeChain/fix/hex-encoding
Browse files Browse the repository at this point in the history
Fix bug hex conversion, import interfaces
  • Loading branch information
dastanbeksamatov authored Jan 3, 2021
2 parents 874d232 + 6d639d0 commit 3c7f3e4
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion assembly/Arrays/ByteArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class ByteArray extends AbstractArray<Byte, u8> {
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";
}
Expand Down
9 changes: 9 additions & 0 deletions assembly/__tests__/ScaleString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ describe("String", () => {
expect<string>(scaleString1.toString()).toStrictEqual("da");
});

it("should encode to hex", () => {
const scaleString = new ScaleString("as-scale-codec");
expect<string>(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<string>(scaleString1.toHexString()).toStrictEqual("0x546865203139363320496d70616c612066656174757265642072656374696c696e656172207374796c696e67207769746820616e20656e67696e652d7475726e656420616c756d696e756d2072656172207461696c6c696768742070616e656c20737572726f756e6465642062792061206368726f6d6520626f72646572206f6e205353206d6f64656c732e");
const scaleString2 = new ScaleString("A set of words that is complete in itself, typically containing a subject and predicate");
expect<string>(scaleString2.toHexString()).toStrictEqual("0x4120736574206f6620776f726473207468617420697320636f6d706c65746520696e20697473656c662c207479706963616c6c7920636f6e7461696e696e672061207375626a65637420616e6420707265646963617465");
})

itThrows("should throw on incorrect encoding", () => {
ScaleString.fromU8a([0x04]); // Encoded length = 1, actual data length = 0
});
Expand Down
2 changes: 1 addition & 1 deletion assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions assembly/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './Codec';
export * from './DecodedData';
export * from './UnwrappableCodec';
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit 3c7f3e4

Please # to comment.