aion-web3-avm-abi
Advanced tools
Comparing version 1.2.6-beta.1 to 1.2.6-beta.2
{ | ||
"name": "aion-web3-avm-abi", | ||
"namespace": "aion", | ||
"version": "1.2.6-beta.1", | ||
"version": "1.2.6-beta.2", | ||
"description": "Web3 module encode and decode AVM in/output.", | ||
@@ -16,3 +16,3 @@ "repository": "https://github.com/aionnetwork/aion_web3/tree/avm-support-update/packages/web3-avm-abi", | ||
}, | ||
"gitHead": "3036fcf251ea9470887775635243742a0eabe99b" | ||
"gitHead": "26d0739d1a736a34a936744d12da2ec2a668dfd7" | ||
} |
"use strict"; | ||
/** | ||
*@module abi-errors | ||
*@memberof avm-abi | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -3,0 +7,0 @@ //import { version } from "./_version"; |
@@ -0,1 +1,4 @@ | ||
/** | ||
*@module coder-utils | ||
*/ | ||
var BN = require('bn.js'); | ||
@@ -128,2 +131,6 @@ var aionLib = require('aion-lib'); | ||
function bigNumberifyhex(val) { | ||
return new BN(val,16); | ||
} | ||
function concat(objects) { | ||
@@ -323,2 +330,3 @@ var arrays = []; | ||
bigNumberify: bigNumberify, | ||
bigNumberifyhex: bigNumberifyhex, | ||
getAddress: getAddress, | ||
@@ -325,0 +333,0 @@ concat: concat, |
@@ -28,2 +28,3 @@ /* | ||
const TagNull = 0x32; | ||
@@ -498,2 +499,3 @@ | ||
encodeValue(writer, value) { | ||
writer.writeByte(this.coder.tag); | ||
@@ -511,2 +513,78 @@ writer.writeLength(value.length); | ||
// Used to handle BigInteger Data Types | ||
class BigIntegerCoder extends Coder { | ||
constructor(type, byteCount, tag, localName) { | ||
super(type, tag, localName); | ||
this.byteCount = byteCount; | ||
} | ||
decode(reader, array=null) { | ||
if(array === null) { | ||
let tag = reader.readByte(); | ||
if (tag !== this.tag) { | ||
this._throwError("invalid tag"); | ||
} | ||
} | ||
let length = reader.readByte(); | ||
//let length = reader.readLength(); | ||
/*if (tag !== this.tag) { | ||
this._throwError("invalid tag"); | ||
}*/ | ||
let value = utils.bigNumberify(reader.readBytes(length)); | ||
return value; | ||
} | ||
encode(writer, value, array) { | ||
if (value == null) { | ||
this._throwError("cannot be null"); | ||
} | ||
let bytes = utils.arrayify(utils.bigNumberify(value)); | ||
writer.writeByte(this.tag); | ||
writer.writeByte(bytes.length); | ||
writer.writeBytes(bytes); | ||
} | ||
} | ||
class BigIntegerArrayCoder extends BigIntegerCoder { | ||
constructor(type, byteCount, tag, localName) { | ||
super(type, byteCount, tag, localName); | ||
} | ||
decode(reader) { | ||
let arrtag = reader.readByte(); | ||
let result = []; | ||
if(arrtag === TagNull) { | ||
reader.readByte(2); | ||
return result; | ||
} else if (arrtag !== this.tag) { | ||
throw new Error("invalid child tag"); | ||
} | ||
let tag = reader.readByte(); | ||
let length = reader.readLength(); | ||
//let length1 = reader.readByte(); | ||
for (let i = 0; i < length; i++) { | ||
result.push(super.decode(reader)); | ||
} | ||
return result; | ||
} | ||
encode(writer, value) { | ||
if(!Array.isArray(value)) { | ||
this._throwError("has to be an array"); | ||
} | ||
writer.writeByte(this.tag); | ||
writer.writeByte(0x23); | ||
writer.writeLength(value.length); | ||
value.forEach((value) => { | ||
super.encode(writer, value, true); | ||
}); | ||
} | ||
} | ||
module.exports = { | ||
@@ -530,3 +608,7 @@ Reader: Reader, | ||
FloatArrayCoder: FloatArrayCoder, | ||
BooleanArrayCoder: BooleanArrayCoder | ||
BooleanArrayCoder: BooleanArrayCoder, | ||
BigIntegerCoder: BigIntegerCoder, | ||
BigIntegerArrayCoder: BigIntegerArrayCoder | ||
} |
@@ -23,5 +23,10 @@ /* | ||
*/ | ||
/** | ||
* | ||
* @namespace avm-abi | ||
* @memberof Avm | ||
*/ | ||
"use strict"; | ||
var _ = require('underscore'); | ||
let utils = require('./coder-utils'); | ||
@@ -34,2 +39,3 @@ let codec = require('./coder'); | ||
if(!this) { return new Error('missing "new" keyword'); } | ||
this.coder_utils = utils; | ||
} | ||
@@ -39,11 +45,12 @@ | ||
getCoder(param) { | ||
let comps = param.trim().split(" ").map((comp) => comp.trim()); | ||
let comps = param.trim().split(" ").map((comp) => comp.trim()); | ||
param = comps[0]; | ||
let localName = comps[1] || null; | ||
// Checks for a 2D Array and if so, builds the inner-most coder | ||
// for the arrays on the inside | ||
if(param.substring(param.length - 4) === "[][]") { | ||
let coder = param.substring(0, param.length - 2); | ||
if(!coder.includes("string") && !coder.includes("address")) { | ||
@@ -101,3 +108,8 @@ return new codec.ArrayCoder(this.getCoder(coder), localName); | ||
return new codec.ArrayCoder(new codec.AddressCoder("address"), localName); | ||
//implimentation for BigInt | ||
case "biginteger": | ||
return new codec.BigIntegerCoder("biginteger", null, 0x23, localName); | ||
case "biginteger[]": | ||
return new codec.BigIntegerArrayCoder("biginteger[]", null, 0x31, localName); | ||
} | ||
@@ -139,2 +151,3 @@ throw new Error("unknown - " + param); | ||
encode(types, values) { | ||
if (types.length !== values.length) { | ||
@@ -145,3 +158,2 @@ return new Error("types/values length mismatch"); | ||
let coders = types.map((type) => this.getCoder(type)); | ||
let writer = this.getWriter(); | ||
@@ -155,3 +167,2 @@ coders.forEach((coder, index) => { | ||
}); | ||
return writer._data; | ||
@@ -179,3 +190,2 @@ } | ||
if(type.substring(type.length - 2) === "[]") array = true; | ||
return coder.decode(reader, array); | ||
@@ -188,4 +198,64 @@ } | ||
} | ||
/** | ||
* Decodes events non- and indexed parameters. | ||
* | ||
* @method getEvents | ||
* @param {Object} inputs | ||
* @param {String} data | ||
* @param {Array} topics | ||
* @return {Array} byte array | ||
*/ | ||
getEvents(inputs, data, topics) { | ||
var _this = this; | ||
topics = _.isArray(topics) ? topics : [topics]; | ||
data = data || ''; | ||
var notIndexedInputs = []; | ||
var indexedParams = []; | ||
var topicCount = 0; | ||
// TODO check for anonymous logs? | ||
inputs.forEach(function (input, i) { | ||
if (input.indexed) { | ||
indexedParams[i] = (['bool', 'int', 'uint', 'address', 'fixed', 'ufixed'].find(function (staticType) { | ||
return input.type.indexOf(staticType) !== -1; | ||
})) ? _this.decodeParameter(input.type, topics[topicCount]) : topics[topicCount]; | ||
topicCount++; | ||
} else { | ||
notIndexedInputs[i] = input; | ||
} | ||
}); | ||
var nonIndexedData = data; | ||
var notIndexedParams = (nonIndexedData) ? this.decodeParameters(notIndexedInputs, nonIndexedData) : []; | ||
var returnValue = new Result(); | ||
returnValue.__length__ = 0; | ||
inputs.forEach(function (res, i) { | ||
returnValue[i] = (res.type === 'string') ? '' : null; | ||
if (typeof notIndexedParams[i] !== 'undefined') { | ||
returnValue[i] = notIndexedParams[i]; | ||
} | ||
if (typeof indexedParams[i] !== 'undefined') { | ||
returnValue[i] = indexedParams[i]; | ||
} | ||
if (res.name) { | ||
returnValue[res.name] = returnValue[i]; | ||
} | ||
returnValue.__length__++; | ||
}); | ||
return returnValue; | ||
}; | ||
} | ||
module.exports = ABICoder; |
@@ -18,3 +18,3 @@ "use strict"; | ||
var validBaseTypes = [ | ||
"Address" ,"address", "boolean", "byte", "char", "double", "float", "int", "long", "short", "String" | ||
"Address" ,"address", "boolean", "byte", "char", "double", "float", "int", "long", "short", "String","BigInteger" | ||
]; | ||
@@ -27,2 +27,16 @@ function checkIdentifier(value) { | ||
} | ||
function checkParamIdentifier(value) { | ||
if (!value.match(/^[a-z_][a-z0-9_]*(\[\])?(\[\])?$/i)) { | ||
errors.throwArgumentError("invalid identifier", "value", value); | ||
} | ||
return value; | ||
} | ||
function clinitTypes(str){ | ||
var types = []; | ||
str = str.replace(/\s+/g,''); | ||
str = str.substring(str.indexOf("(")); | ||
str = str.substring(1,str.length-1); | ||
types = str.split(","); | ||
return types; | ||
} | ||
function checkType(value) { | ||
@@ -47,9 +61,7 @@ var throwError = function () { | ||
} | ||
properties_1.defineReadOnly(this, "name", checkIdentifier(name)); | ||
properties_1.defineReadOnly(this, "name", checkParamIdentifier(name)); | ||
properties_1.defineReadOnly(this, "type", checkType(type)); | ||
} | ||
ParamType.fromString = function (value) { | ||
//console.log("This is ParamType.fromString: "+value); | ||
var comps = value.trim().replace(/\[\s*\]/g, "[]").replace(/\s+/g, " ").split(" "); | ||
//console.log("comps: ["+comps.length+"] "+comps[0]+" "+comps[1]); | ||
if (comps.length >= 2) { | ||
@@ -113,3 +125,3 @@ errors.throwArgumentError("invalid param type", "value", value); | ||
var Interface = /** @class */ (function () { | ||
function Interface(constructorGuard, version, name, functions) { | ||
function Interface(constructorGuard, version, name, functions,clinit) { | ||
errors.checkNew(this, Interface); | ||
@@ -119,8 +131,14 @@ if (constructorGuard !== _constructorGuard) { | ||
} | ||
if (version !== "0.0") { | ||
//TODO:improve the following | ||
if ((version !== "0.0")&&(version !== "1")) { | ||
errors.throwArgumentError("unsupported version", "version", version); | ||
} | ||
properties_1.defineReadOnly(this, "version", version); | ||
name = name.split(".").map(function (comp) { return checkIdentifier(comp.trim()); }).join("."); | ||
properties_1.defineReadOnly(this, "name", name); | ||
properties_1.defineReadOnly(this, "init", clinit); | ||
properties_1.defineReadOnly(this, "functions", functions); | ||
@@ -130,5 +148,8 @@ Object.freeze(this.functions); | ||
Interface.fromString = function (abi) { | ||
var flag_str = null; | ||
if (typeof (abi) === "string") { | ||
flag_str = abi; | ||
abi = abi.split("\n"); | ||
} | ||
var version = null; | ||
@@ -145,2 +166,6 @@ var name = null; | ||
version = line; | ||
if(version==="0.0" && flag_str.match(/(BigInteger)(\[\])?/)){ | ||
throw new Error("BigIntegertype is not available for this version"); | ||
} | ||
return; | ||
@@ -159,5 +184,7 @@ } | ||
functions.push(FunctionFragment.fromString(line)); | ||
}); | ||
return new Interface(_constructorGuard, version, name, functions); | ||
; | ||
var ClinitArr = clinitTypes(Clinit); | ||
return new Interface(_constructorGuard, version, name, functions, ClinitArr); | ||
}; | ||
@@ -164,0 +191,0 @@ return Interface; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86471
1449