@paradeum/burrow
Version:
TypeScript library that calls a Hyperledger Burrow server over GRPC.
242 lines • 12.6 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
exports.SolidityFunction = exports.DEFAULT_GAS = void 0;
var utils = __importStar(require("../utils/utils"));
var coder = __importStar(require("ethereumjs-abi"));
var convert = __importStar(require("../utils/convert"));
var grpc = __importStar(require("grpc"));
var SHA3 = __importStar(require("../utils/sha3"));
var payload_pb_1 = require("../../../proto/payload_pb");
exports.DEFAULT_GAS = 1111111111;
function fnSignature(abi) {
var name = utils.transformToFullName(abi);
return SHA3.str(name).slice(0, 8);
}
var types = function (args) { return args.map(function (arg) { return arg.type; }); };
function txPayload(data, account, address, codeHash, meta) {
var input = new payload_pb_1.TxInput();
input.setAddress(Buffer.from(account, 'hex'));
input.setAmount(0);
var payload = new payload_pb_1.CallTx();
payload.setInput(input);
if (address)
payload.setAddress(Buffer.from(address, 'hex'));
payload.setGaslimit(exports.DEFAULT_GAS);
payload.setFee(0);
payload.setData(Buffer.from(data, 'hex'));
if (codeHash && meta) {
var contractMeta = new payload_pb_1.ContractMeta();
contractMeta.setCodehash(codeHash);
contractMeta.setMeta(meta);
payload.addContractmeta(contractMeta);
}
return payload;
}
var encodeF = function (abi, args, bytecode) {
var abiInputs;
if (abi) {
abiInputs = types(abi.inputs);
args = convert.burrowToAbi(abiInputs, args); // If abi is passed, convert values accordingly
}
// If bytecode provided then this is a creation call, bytecode goes first
if (bytecode) {
var data = bytecode;
if (abi)
data += convert.bytesTB(coder.rawEncode(abiInputs, args));
return data;
}
else {
return fnSignature(abi) + convert.bytesTB(coder.rawEncode(abiInputs, args));
}
};
var decodeF = function (abi, output) {
if (!output)
return;
var outputs = abi.outputs;
var outputTypes = types(outputs);
// Decode raw bytes to arguments
var raw = convert.abiToBurrow(outputTypes, coder.rawDecode(outputTypes, Buffer.from(output)));
var result = { raw: raw.slice() };
result.values = outputs.reduce(function (acc, current) {
var value = raw.shift();
if (current.name) {
acc[current.name] = value;
}
return acc;
}, {});
return result;
};
exports.SolidityFunction = function (abi, contract) {
var isConstructor = (abi == null || abi.type === 'constructor');
var name;
var displayName;
var typeName;
if (!isConstructor) {
name = utils.transformToFullName(abi);
displayName = utils.extractDisplayName(name);
typeName = utils.extractTypeName(name);
}
// It might seem weird to include copies of the functions in here and above
// My reason is the code above can be used "functionally" whereas this version
// Uses implicit attributes of this object.
// I want to keep them separate in the case that we want to move all the functional
// components together and maybe even... write tests for them (gasp!)
var encode = function () {
var args = Array.prototype.slice.call(arguments);
return encodeF(abi, args, isConstructor ? this.code : null);
};
var decode = function (data) {
return decodeF(abi, data);
};
var call = function (isSim, handler, address) {
var args = [];
for (var _i = 3; _i < arguments.length; _i++) {
args[_i - 3] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var self, P, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
handler = handler || function (result) { return result; };
address = address || this.address;
if (isConstructor) {
address = null;
}
;
self = this;
P = new Promise(function (resolve, reject) {
if (address == null && !isConstructor)
reject(new Error('Address not provided to call'));
if (abi != null && abi.inputs.length !== args.length)
reject(new Error('Insufficient arguments passed to function: ' + (isConstructor ? 'constructor' : name)));
// Post process the return
var post = function (error, result) {
if (error)
return reject(error);
// Handle execution reversions
if (result.hasException()) {
// Decode error message if there is one otherwise default
if (result.getResult().getReturn().length === 0) {
error = new Error('Execution Reverted');
}
else {
// Strip first 4 bytes(function signature) the decode as a string
error = new Error(coder.rawDecode(['string'], Buffer.from(result.getResult().getReturn_asU8().slice(4)))[0]);
}
error.code = grpc.status.ABORTED;
return reject(error);
}
// Meta Data (address, caller, height, etc)
var returnObj = {
contractAddress: Buffer.from(result.getReceipt().getContractaddress_asU8()).toString('hex').toUpperCase(),
height: result.getHeader().getHeight(),
index: result.getHeader().getIndex(),
hash: Buffer.from(result.getHeader().getTxhash_asU8()).toString('hex').toUpperCase(),
type: result.getHeader().getTxtype(),
result: result.getResult().toObject(),
tx: result.getEnvelope().toObject(),
caller: convert.recApply(convert.bytesTB, result.getEnvelope().getSignatoriesList().map(function (sig) { return sig.getAddress_asU8(); }))
};
// Unpack return arguments
if (!isConstructor) {
try {
var _a = decodeF(abi, result.getResult().getReturn_asU8()), raw = _a.raw, values = _a.values;
returnObj.raw = raw;
returnObj.values = values;
}
catch (e) {
return reject(e);
}
}
return resolve(returnObj);
};
// Decide if to make a "call" or a "transaction"
// For the moment we need to use the burrowtoweb3 function to prefix bytes with 0x
// otherwise the coder will give an error with bignumber not a number
// TODO investigate if other libs or an updated lib will fix this
// let data = encodeF(abi, utils.burrowToWeb3(args), isCon ? self.code : null)
var data = encodeF(abi, args, isConstructor ? self.code : null);
//add codeHash and metaData by kuang
var codeHash;
if (contract.deployedBytecode) {
var deployedBytecode = Buffer.from(contract.deployedBytecode, 'hex');
codeHash = Buffer.from(SHA3.buffer(deployedBytecode), 'hex');
}
var metaData = JSON.stringify({
Abi: contract.abi
});
var payload = txPayload(data, contract.burrow.account, address, codeHash, metaData);
if (isSim) {
contract.burrow.pipe.call(payload, post);
}
else {
contract.burrow.pipe.transact(payload, post);
}
});
return [4 /*yield*/, P];
case 1:
result = _a.sent();
return [2 /*return*/, handler(result)];
}
});
});
};
return { displayName: displayName, typeName: typeName, call: call, encode: encode, decode: decode };
};
//# sourceMappingURL=function.js.map