Skip to content

Commit

Permalink
Prepare for publish
Browse files Browse the repository at this point in the history
  • Loading branch information
buuni committed Sep 30, 2020
1 parent f38f1ee commit ea3a2df
Show file tree
Hide file tree
Showing 13 changed files with 363 additions and 282 deletions.
8 changes: 2 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ google-token.json
*.csv
users.yaml
*.xml
/.idea/*
db-backups
/.idea
.idea
build
docker-compose.dev.yaml

./.backup

/.env

hh-tampermonkey/widget/.cache
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 120,
"trailingComma": "all",
"singleQuote": true
}
35 changes: 32 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
{
"name": "lcs",
"version": "0.0.1",
"main": "build/lib/index.js",
"description": "Implementation of the LCS (Libra Canonical Serialization) protocol",
"types": "build/lib/index.d.ts",
"author": "Igor Demko <[email protected]>",
"license": "MIT",
"keywords": [
"dfinance",
"dfi",
"move",
"libra",
"smartcontract",
"xfi",
"lcs",
"lcs-serialization",
"lcs-deserialization"
],
"scripts": {
"serve": "nodemon src/index.ts"
"serve": "nodemon src/example.ts",
"build": "tsc",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "tslint -p tsconfig.json",
"prepare": "yarn run build",
"prepublishOnly": "yarn run lint",
"preversion": "yarn run lint",
"version": "yarn run format && git add -A src",
"postversion": "git push && git push --tags"
},
"files": ["build/**/*"],
"dependencies": {
"bignumber.js": "^9.0.0",
"int64-buffer": "^0.99.1007",
"leb": "^0.3.0",
"uleb128": "^1.0.1",
"xregexp": "^4.3.0"
},
"devDependencies": {
"@babel/types": "^7.11.5",
"@types/node": "^14.10.1",
"@types/xregexp": "^4.3.0",
"nodemon": "^2.0.4",
"prettier": "^2.1.2",
"ts-node": "^9.0.0",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0",
"typescript": "^4.0.2"
}
}
55 changes: 6 additions & 49 deletions src/common/BufferUtil.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,6 @@
import { stringLiteral } from "@babel/types"
import { Buffer } from "buffer"

export class BufferUtil {
public static fromHex(source: string): Uint8Array {
const data = source.match(/.{1,2}/g)!.map(x => parseInt(x, 16))
return new Uint8Array(data)
}

public static fromBase64(source: string): Uint8Array {
return Uint8Array.from(Buffer.from(source, 'base64'))
}

public static fromString(source: string): Uint8Array {
const buffer = new ArrayBuffer(source.length)
const view = new DataView(buffer)
for(let i=0; i< source.length; i++) {
view.setUint8(i,source.charCodeAt(i))
}
return new Uint8Array(buffer)
}

public static toString(source: Uint8Array): string {
const data: string[] = []
source.forEach( x => {
data.push(String.fromCharCode(x))
})
return data.join('')
}

public static toHex(sources:Uint8Array): string {
const data:string[] = []
sources.forEach(x => {
data.push(x.toString(16).padStart(2, '0'))
})
return data.join('')
}

public static toBase64(sources:Uint8Array): string {
return Buffer.from(sources).toString('base64')
}

public static concat(a:Uint8Array, b:Uint8Array): Uint8Array {
const c = new Uint8Array(a.length + b.length)
c.set(a)
c.set(b, a.length)
return c
}
}
export class BufferUtil {
public static fromHex(source: string): Uint8Array {
const data = source.match(/.{1,2}/g)!.map((x) => parseInt(x, 16));
return new Uint8Array(data);
}
}
55 changes: 7 additions & 48 deletions src/common/CursorBuffer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import BigNumber from 'bignumber.js';
const leb = require('leb')
// @ts-ignore
import leb from 'leb';

/**
* A wrapper around byte buffers to perform cursor reading on bytes
* of different sizes
*
*/
export default class CursorBuffer {
public dataView: DataView;
private dataView: DataView;
private readonly littleEndian: boolean;
private bytePosition: number;

Expand All @@ -17,31 +13,18 @@ export default class CursorBuffer {
this.bytePosition = 0;
}

/**
* Reads 1 byte
*
*/
public read8(): number {
const value = this.dataView.getUint8(this.bytePosition);
this.bytePosition += 1;
return value;
}

/**
* Reads 4 bytes
*
*/
public read32(): number {
const value = this.dataView.getUint32(this.bytePosition, this.littleEndian);
this.bytePosition += 4;
return value;
}

/**
* Reads 8 bytes
*
*
*/
public read64(): BigNumber {
const firstPart = this.read32();
const secondPart = this.read32();
Expand Down Expand Up @@ -72,47 +55,23 @@ export default class CursorBuffer {
return value;
}

/**
* Read bool as 1 byte
*
*/
public readBool(): boolean {
const value = this.dataView.getUint8(this.bytePosition);
this.bytePosition += 1;
if(value !== 0 && value !== 1) {
if (value !== 0 && value !== 1) {
throw new Error(`bool must be 0 or 1, found ${value}`);
}
return value !== 0;
}

public readULEB(): BigNumber {
const startPosition = this.bytePosition + this.dataView.byteOffset;
const ar: Uint8Array = new Uint8Array(this.dataView.buffer, startPosition, 128)
const buf = Buffer.from(ar)
const res: { value: Buffer, nextIndex: number } = leb.decodeUIntBuffer(buf, 0)
const ar: Uint8Array = new Uint8Array(this.dataView.buffer, startPosition, 128);
const buf = Buffer.from(ar);
const res: { value: Buffer; nextIndex: number } = leb.decodeUIntBuffer(buf, 0);

this.bytePosition += res.nextIndex;

return new BigNumber(`0x${res.value.toString('hex')}`, 16);
}

public write(payload: Uint8Array) {
const buffer = toBuffer(this.dataView.buffer)
const newBuffer = Buffer.concat([buffer, Buffer.from(payload)]);

this.dataView = new DataView(
newBuffer.buffer,
0,
newBuffer.length
);
}
}

function toBuffer(ab: ArrayBuffer): Buffer {
const buf: Buffer = Buffer.alloc(ab.byteLength);
const view = new Uint8Array(ab);
for (let i = 0; i < buf.length; ++i) {
buf[i] = view[i];
}
return buf;
}
32 changes: 15 additions & 17 deletions src/index.ts → src/example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {BufferUtil} from './common/BufferUtil';
import LCS from './lib'
import CursorBuffer from "./common/CursorBuffer";
import { BufferUtil } from './common/BufferUtil';
import LCS from './lib';
import CursorBuffer from './common/CursorBuffer';

// 4d010000000000000000000000000000 03 646669 db4b0ed53d2fd0a74ce8f0d106e7ab144eb0fbab 00
// u128 vector payee metadata
Expand All @@ -12,33 +12,31 @@ const buffer = Buffer.from(BufferUtil.fromHex(CDPData));

LCS.registerType('string', {
code: (cursor: CursorBuffer, value: any, options?: any) => {
const charCodes = value
.split('')
.map((char: string) => char.charCodeAt(0))
const charCodes = value.split('').map((char: string) => char.charCodeAt(0));

return LCS.serialize(charCodes, 'vector<u8>', options)
return LCS.serialize(charCodes, 'vector<u8>', options);
},
decode: (cursor: CursorBuffer, options?: any) => {
const vector: any = LCS.deserialize(cursor, 'vector<u8>', options)
const vector: any = LCS.deserialize(cursor, 'vector<u8>', options);

return vector.map((charCode: number) => String.fromCharCode(charCode)).join('')
}
})
return vector.map((charCode: number) => String.fromCharCode(charCode)).join('');
},
});

LCS.registerType('SentPaymentEvent', {
amount: 'u128',
denom: 'string',
payee: 'address',
metadata: 'vector<u8>'
})
metadata: 'vector<u8>',
});

LCS.registerType('OfferCreatedEvent', {
offered_amount: 'u128',
margin_call_at: 'u8',
collateral_multiplier: 'u8',
lender: 'address'
})
lender: 'address',
});

const decoded = LCS.deserialize(buffer, 'OfferCreatedEvent')
const decoded = LCS.deserialize(buffer, 'OfferCreatedEvent');
// tslint:disable-next-line
console.log(decoded);

10 changes: 5 additions & 5 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import LCS from './lcs'
import types from './types'
import LCS from './lcs';
import types from './types';

Object.keys(types).forEach((typeName: string) => {
// @ts-ignore
LCS.registerType(typeName, types[typeName])
})
LCS.registerType(typeName, types[typeName]);
});

export default LCS
export default LCS;
8 changes: 4 additions & 4 deletions src/lib/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import CursorBuffer from "../common/CursorBuffer";
import CursorBuffer from '../common/CursorBuffer';

export interface TypeInterface {
decode: (cursor: CursorBuffer, options?: any) => any,
code: (cursor: CursorBuffer, value: any, options?: any) => any
decode: (cursor: CursorBuffer, options?: any) => any;
code: (cursor: CursorBuffer, value: any, options?: any) => any;
}

export interface RegisterTypeInterface {
[key: string]: TypeInterface | string
[key: string]: TypeInterface | string;
}
Loading

0 comments on commit ea3a2df

Please sign in to comment.