-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
2,080 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; | ||
} |
Oops, something went wrong.