Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
lovelycs committed Mar 1, 2019
2 parents dd2a81e + e013cd5 commit 4520270
Show file tree
Hide file tree
Showing 36 changed files with 2,080 additions and 181 deletions.
22 changes: 17 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"name": "@vite/vitejs",
"version": "1.1.3",
"version": "1.1.5-10",
"description": "",
"scripts": {
"postinstall": "lerna bootstrap",
"build": "yarn run postinstall && lerna run build",
"rpc": "node test/bin/startRPC",
"test:rpc": "cross-env NODE_ENV=test nyc mocha --compilers ts:ts-node/register -r tsconfig-paths/register test/RPC/index.js --no-timeouts",
"test": "yarn run build && cross-env NODE_ENV=test nyc mocha --compilers ts:ts-node/register -r tsconfig-paths/register test/index.js --no-timeouts",
"test:only": "cross-env NODE_ENV=test nyc mocha --compilers ts:ts-node/register -r tsconfig-paths/register test/index.js --no-timeouts",
"test": "yarn run build && yarn run test:only",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint ./ --cache",
"lint:all": "eslint ./",
"prerelease-alpha": "npm run build && npm version prerelease",
"prepublish-alpha": "npm run build && npm version prerelease",
"publish-alpha": "npm publish --access-public --tag alpha",
"publish": "npm dist-tag add @vite/vitejs@alpha latest"
"pub": "npm dist-tag add @vite/vitejs@alpha latest"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -74,13 +75,24 @@
},
"nyc": {
"require": [
"ts-node/register",
"@babel/register"
],
"extension": [
".js",
".ts"
],
"include": [
"packages/**/src/*"
],
"exclude": [
"*.d.ts"
],
"reporter": [
"html",
"text"
],
"sourceMap": false,
"instrument": false
"instrument": true
}
}
23 changes: 23 additions & 0 deletions packages/abi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@vite/vitejs-abi",
"version": "1.1.0-1",
"main": "dist/index.js",
"typings": "src/index.d.ts",
"scripts": {
"prebuild": "rm -rf ./dist",
"build": "yarn run prebuild && tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vitelabs/vite.js.git"
},
"license": "ISC",
"bugs": {
"url": "https://github.com/vitelabs/vite.js/issues"
},
"homepage": "https://github.com/vitelabs/vite.js#readme",
"dependencies": {
"@vite/vitejs-utils": "^1.1.0-1",
"@vite/vitejs-privtoaddr": "^1.1.0-1"
}
}
154 changes: 154 additions & 0 deletions packages/abi/src/coder/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// address bool gid number

const BigNumber = require('bn.js');
import { getHexAddrFromAddr, getAddrFromHexAddr } from '@vite/vitejs-privtoaddr';
import { tools } from '@vite/vitejs-utils';

const { getRawTokenid, getTokenIdFromRaw } = tools;



export function encode(typeObj, params) {
const Bytes_Data = getBytesData(typeObj.type, params);
return encodeBytesData(typeObj, Bytes_Data);
}

export function encodeBytesData(typeObj, Bytes_Data) {
const Actual_Byte_Len = typeObj.actualByteLen;
if (Actual_Byte_Len < Bytes_Data.length) {
throw lengthError(typeObj, Bytes_Data.length);
}

const Byte_Len = typeObj.byteLength;
const Offset = Byte_Len - Bytes_Data.length;
if (Offset < 0) {
throw lengthError(typeObj, Bytes_Data.length);
}

let result = new Uint8Array(Byte_Len);
result.set(Bytes_Data, typeObj.type === 'bytes' ? 0 : Offset);

return {
result: Buffer.from(result).toString('hex'),
typeObj
}
}

export function decodeToHexData(typeObj, params) {
if ( typeof params !== 'string' || !/^[0-9a-fA-F]+$/.test(params) ) {
throw '[Error] decode, params should be hex-string.';
}

const Byte_Len = typeObj.byteLength;
const _params = params.substring(0, Byte_Len*2);
const Data_Len = _params.length / 2;

if (Byte_Len !== Data_Len) {
throw lengthError(typeObj, Data_Len);
}

const Actual_Byte_Len = typeObj.actualByteLen;
const Offset = Byte_Len - Actual_Byte_Len;
if (Data_Len < Offset) {
throw lengthError(typeObj, Actual_Byte_Len);
}

return {
result: typeObj.type === 'bytes' ? _params.substring(0, _params.length - Offset * 2) : _params.substring(Offset * 2),
params: params.substring(Data_Len * 2)
};
}


export function decode(typeObj, params) {
let res = decodeToHexData(typeObj, params);

return {
result: getRawData(typeObj.type, res.result),
params: res.params
}
}



function getRawData(type, params) {
switch (type) {
case 'address':
return showAddr(params);
case 'bool':
return showNumber(!!params ? '1' : '0');
case 'number':
return showNumber(params);
case 'gid':
return params;
case 'tokenId':
return showTokenId(params);
}
}

function getBytesData(type, params) {
switch (type) {
case 'address':
return formatAddr(params);
case 'bool':
return formatNumber(!!params ? '1' : '0');
case 'number':
return formatNumber(params);
case 'gid':
return formatGid(params);
case 'tokenId':
return fomatTokenId(params);
}
}

function formatAddr(address) {
let addr = getAddrFromHexAddr(address);
if (!addr) {
throw `[Error] Illegal address. ${address}`;
}
return Buffer.from(addr, 'hex');
}

function formatGid(gid) {
if (!gid || !/^[0-9a-fA-F]+$/.test(gid) || gid.length !== 20 ) {
throw `[Error] Illegal gid. ${gid}`;
}
return Buffer.from(gid, 'hex');
}

function formatNumber(params) {
return new BigNumber(params).toArray();
}

function fomatTokenId(tokenId) {
let rawTokenId = getRawTokenid(tokenId);
if (!rawTokenId) {
throw `[Error] Illegal tokenId. ${tokenId}`;
}
return Buffer.from(rawTokenId, 'hex');
}

function showAddr(address) {
let addr = getHexAddrFromAddr(address);
if (!addr) {
throw `[Error] Illegal address. ${address}`;
}
return addr;
}

function showNumber(str) {
return new BigNumber(str, 16).toString();
}

function showTokenId(rawTokenId) {
let tokenId = getTokenIdFromRaw(rawTokenId);
if (!tokenId) {
throw `[Error] Illegal tokenId. ${rawTokenId}`;
}
return tokenId;
}


function lengthError(typeObj, length) {
return `[Error] Illegal length. ${JSON.stringify(typeObj)}, data length: ${length}`;
}
98 changes: 98 additions & 0 deletions packages/abi/src/coder/dynamic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { encode as commonEncode, encodeBytesData, decode as commonDecode, decodeToHexData } from "./common";


export function encode(typeObj, params) {
const Bytes_Data = getBytesData(typeObj.type, params);

if (typeObj.byteLength) {
return encodeBytesData(typeObj, Bytes_Data);
}

let result = dynamicEncode(Bytes_Data);
if (typeObj.type === 'bytes') {
let dataLength = 32 * Math.ceil(Bytes_Data.length / 32);
result = commonEncode({
type: 'number', byteLength: 32
}, dataLength).result + result
}

return { result, typeObj }
}

export function decode(typeObj, params) {
const Type = typeObj.type;

if (typeObj.byteLength) {
let decodeRes = decodeToHexData(typeObj, params);
return {
result: getRawData(typeObj.type, decodeRes.result),
params: decodeRes.params
};
}

let _params = Type === 'bytes' ? params.substring(64) : params;
let res = dynamicDecode(_params);

return {
result: getRawData(Type, res.result),
params: res.params
};
}



function getRawData(type, params) {
if (type === 'string') {
return Buffer.from(params, 'hex').toString('utf8');
}
return params;
}

function getBytesData(type, params) {
if ( typeof params !== 'string' ) {
throw '[Error] Illegal params. Should be String';
}

let isHex = /^0x[0-9a-fA-F]+$/.test(params) && params.length % 2 === 0;
let isCommonHex = /^[0-9a-fA-F]+$/.test(params) && params.length % 2 === 0;

if (type === 'bytes' && !isCommonHex && !isHex) {
throw '[Error] Illegal params. Should be hex-string.';
}

if (isHex) {
return Buffer.from(params.substring(2), 'hex');
}
if (isCommonHex) {
return Buffer.from(params, 'hex');
}
return Buffer.from(params, 'utf8');
}

function dynamicEncode(bytesData) {
const Str_Len = bytesData.length;
const Data_Length = 32 * Math.ceil(Str_Len / 32);

let bytesLen = commonEncode({
type: 'number', byteLength: 32
}, Str_Len).result;

let len = bytesLen.length/2 + Data_Length;
let arr = new Uint8Array(len);

arr.set(Buffer.from(bytesLen, 'hex'));
arr.set(bytesData, bytesLen.length / 2);
return Buffer.from(arr).toString('hex');
}

function dynamicDecode(params) {
const Str_Len = commonDecode({
type: 'number', byteLength: 32
}, params.substring(0, 64)).result;
const Data_Length = 32 * Math.ceil(Str_Len / 32);

return {
result: params.substring(64, 64 + Str_Len * 2),
params: params.substring(64 + Data_Length * 2)
};
}

0 comments on commit 4520270

Please sign in to comment.