airgap-coin-lib
Version:
The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.
168 lines • 7.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var bigInt = require("../../dependencies/src/big-integer-1.6.45/BigInteger");
var bs58check = require("../../dependencies/src/bs58check-2.1.2/index");
var MichelsonList_1 = require("./types/michelson/generics/MichelsonList");
var MichelsonPair_1 = require("./types/michelson/generics/MichelsonPair");
var MichelsonBytes_1 = require("./types/michelson/primitives/MichelsonBytes");
var MichelsonInt_1 = require("./types/michelson/primitives/MichelsonInt");
var MichelsonString_1 = require("./types/michelson/primitives/MichelsonString");
var TezosUtils = /** @class */ (function () {
function TezosUtils() {
}
TezosUtils.parseAddress = function (bytes) {
var rawHexAddress = typeof bytes === 'string' ? bytes : bytes.toString('hex');
if (rawHexAddress.startsWith('0x')) {
rawHexAddress = rawHexAddress.slice(2);
}
var _a = this.splitAndReturnRest(rawHexAddress, 2), result = _a.result, rest = _a.rest;
var contractIdTag = result;
if (contractIdTag === '00') {
// tz address
return this.parseTzAddress(rest);
}
else if (contractIdTag === '01') {
// kt address
return this.prefixAndBase58CheckEncode(rest.slice(0, -2), this.tezosPrefixes.kt);
}
else {
throw new Error("address format not supported (" + rawHexAddress + ")");
}
};
TezosUtils.parseHex = function (rawHex) {
var hex;
if (typeof rawHex === 'string') {
hex = TezosUtils.hexStringToArray(rawHex);
}
else {
hex = rawHex;
}
var type = hex.shift();
switch (type) {
case '07': // prim
var primType = hex.shift();
if (primType === '07') {
// pair
return TezosUtils.parsePair(hex);
}
throw new Error('Prim type not supported');
case '00': // int
var intBytes = [];
var byte = void 0;
do {
byte = hex.shift();
if (byte === undefined) {
break;
}
intBytes.push(byte);
} while (parseInt(byte, 16) >= 127);
return MichelsonInt_1.MichelsonInt.from(TezosUtils.decodeSignedInt(intBytes.join('')));
case '01': // string
var stringLength = TezosUtils.hexToLength(hex.splice(0, 4));
return MichelsonString_1.MichelsonString.from(TezosUtils.hexToString(hex.splice(0, stringLength)));
case '05': // single arg prim
return TezosUtils.parseHex(hex);
case '02': // list
return TezosUtils.parseList(hex);
case '0a': // bytes
var bytesLength = TezosUtils.hexToLength(hex.splice(0, 4));
return MichelsonBytes_1.MichelsonBytes.from(hex.splice(0, bytesLength).join(''));
default:
throw new Error("Type not supported " + type);
}
};
TezosUtils.decodeSignedInt = function (hex) {
var positive = Buffer.from(hex.slice(0, 2), 'hex')[0] & 0x40 ? false : true;
var arr = Buffer.from(hex, 'hex').map(function (v, i) { return (i === 0 ? v & 0x3f : v & 0x7f); });
var n = bigInt.zero;
for (var i = arr.length - 1; i >= 0; i--) {
if (i === 0) {
n = n.or(arr[i]);
}
else {
n = n.or(bigInt(arr[i]).shiftLeft(7 * i - 1));
}
}
return positive ? n.toJSNumber() : n.negate().toJSNumber();
};
TezosUtils.hexStringToArray = function (hexString) {
if (hexString.startsWith('0x')) {
hexString = hexString.slice(2);
}
var hexBytes = hexString.match(/.{2}/g);
if (hexBytes === null) {
throw new Error('Cannot parse contract code');
}
return hexBytes;
};
TezosUtils.parsePair = function (hex) {
var first = TezosUtils.parseHex(hex);
var second = TezosUtils.parseHex(hex);
return MichelsonPair_1.MichelsonPair.from([first, second]);
};
TezosUtils.parseList = function (hex) {
var items = [];
var lengthBytes = TezosUtils.hexToLength(hex.splice(0, 4));
if (lengthBytes > 0) {
var listBytes = hex.splice(0, lengthBytes);
while (listBytes.length > 0) {
var item = TezosUtils.parseHex(listBytes);
items.push(item);
}
}
return MichelsonList_1.MichelsonList.from(items);
};
TezosUtils.hexToString = function (hex) {
return hex.map(function (byte) { return String.fromCharCode(parseInt(byte, 16)); }).join('');
};
TezosUtils.hexToLength = function (hex) {
var stringValue = hex.reduce(function (previous, next) {
if (next === '00') {
return previous;
}
return "" + previous + next;
}, '');
if (stringValue.length > 0) {
return parseInt(stringValue, 16);
}
return 0;
};
TezosUtils.splitAndReturnRest = function (payload, length) {
var result = payload.substr(0, length);
var rest = payload.substr(length, payload.length - length);
return { result: result, rest: rest };
};
TezosUtils.parseTzAddress = function (rawHexAddress) {
// tz1 address
var _a = this.splitAndReturnRest(rawHexAddress, 2), result = _a.result, rest = _a.rest;
var publicKeyHashTag = result;
switch (publicKeyHashTag) {
case '00':
return this.prefixAndBase58CheckEncode(rest, this.tezosPrefixes.tz1);
case '01':
return this.prefixAndBase58CheckEncode(rest, this.tezosPrefixes.tz2);
case '02':
return this.prefixAndBase58CheckEncode(rest, this.tezosPrefixes.tz3);
default:
throw new Error("address format not supported (" + rawHexAddress + ")");
}
};
TezosUtils.prefixAndBase58CheckEncode = function (hexStringPayload, tezosPrefix) {
var prefixHex = Buffer.from(tezosPrefix).toString('hex');
return bs58check.encode(Buffer.from(prefixHex + hexStringPayload, 'hex'));
};
// Tezos - We need to wrap these in Buffer due to non-compatible browser polyfills
TezosUtils.tezosPrefixes = {
tz1: Buffer.from(new Uint8Array([6, 161, 159])),
tz2: Buffer.from(new Uint8Array([6, 161, 161])),
tz3: Buffer.from(new Uint8Array([6, 161, 164])),
kt: Buffer.from(new Uint8Array([2, 90, 121])),
edpk: Buffer.from(new Uint8Array([13, 15, 37, 217])),
edsk: Buffer.from(new Uint8Array([43, 246, 78, 7])),
edsig: Buffer.from(new Uint8Array([9, 245, 205, 134, 18])),
branch: Buffer.from(new Uint8Array([1, 52]))
};
return TezosUtils;
}());
exports.TezosUtils = TezosUtils;
//# sourceMappingURL=TezosUtils.js.map