airgap-coin-lib
Version:
The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.
551 lines (549 loc) • 21.5 kB
JavaScript
"use strict";
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 };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var bignumber_1 = require("../../dependencies/src/bignumber.js-9.0.0/bignumber");
var validate_1 = require("../../dependencies/src/validate.js-0.13.1/validate");
var EthereumProtocol_1 = require("../../protocols/ethereum/EthereumProtocol");
var KusamaProtocol_1 = require("../../protocols/substrate/implementations/KusamaProtocol");
var base64Check_1 = require("../../utils/base64Check");
var AeternityProtocol_1 = require("./../../protocols/aeternity/AeternityProtocol");
var BitcoinProtocol_1 = require("./../../protocols/bitcoin/BitcoinProtocol");
var TezosProtocol_1 = require("./../../protocols/tezos/TezosProtocol");
validate_1.validators.type = function (value, options, key, attributes) {
// allow empty values by default (needs to be checked by "presence" check)
if (value === null || typeof value === 'undefined') {
return null;
}
// allow defining object of any type using their constructor. --> options = {clazz: ClassName}
/*
if (typeof options === 'object' && options.clazz) {
return value instanceof options.clazz ? null : 'is not of type "' + options.clazz.name + '"'
}
*/
if (!validate_1.validators.type.checks[options]) {
throw new Error("Could not find validator for type " + options);
}
return validate_1.validators.type.checks[options](value) ? null : "is not of type \"" + options + "\"";
};
validate_1.validators.type.checks = {
Object: function (value) {
return validate_1.isObject(value) && !validate_1.isArray(value);
},
Array: validate_1.isArray,
Integer: validate_1.isInteger,
Number: validate_1.isNumber,
String: validate_1.isString,
Date: validate_1.isDate,
Boolean: function (value) {
return typeof value === 'boolean';
},
BigNumber: function (value) {
return bignumber_1.default.isBigNumber(value);
}
};
validate_1.validators.isHexStringWithPrefix = function (value) {
if (typeof value !== 'string') {
return 'is not hex string';
}
if (!value.startsWith('0x')) {
return 'is not hex string';
}
var hexWithoutPrefix = value.substr(2);
if (hexWithoutPrefix.length === 0) {
// For ethereum, "0x" is valid
return null;
}
return /[0-9A-F]/gi.test(hexWithoutPrefix) ? null : 'is not hex string';
};
validate_1.validators.isPublicKey = function (value) {
if (typeof value !== 'string') {
return 'is not a valid public key: should be of type string';
}
if (value.length !== 64) {
return 'is not a valid public key: wrong length';
}
return /[0-9A-F]/gi.test(value) ? null : 'is not a valid public key: invalid characters';
};
// ETHEREUM
validate_1.validators.isValidEthereumTransactionString = function (transaction) {
// console.log(binaryTransaction)
return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
var signedTx, protocol, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (transaction === null || typeof transaction === 'undefined') {
resolve('not a valid Ethereum transaction');
}
signedTx = {
accountIdentifier: '',
transaction: transaction
};
protocol = new EthereumProtocol_1.EthereumProtocol();
// allow empty values by default (needs to be checked by "presence" check)
if (transaction === null || typeof transaction === 'undefined') {
reject();
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, protocol.getTransactionDetailsFromSigned(signedTx)];
case 2:
_a.sent();
resolve();
return [3 /*break*/, 4];
case 3:
error_1 = _a.sent();
// console.log(error)
resolve('not a valid Ethereum transaction');
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
};
// BITCOIN
validate_1.validators.isValidBitcoinInput = function (ins) {
// if (!Array.isArray(ins)) {
// ins = [ins]
// }
if (!Array.isArray(ins)) {
return 'not an array';
}
for (var i = 0; i < ins.length; i++) {
var value = ins[i];
if (!value.hasOwnProperty('txId')) {
return 'doesn\'t have property txId ';
}
else {
var pattern = RegExp('^[a-fA-F0-9]{64}$');
if (!pattern.test(value.txId)) {
return 'not a valid txId';
}
}
if (!value.hasOwnProperty('value')) {
return 'doesn\'t have property value ';
}
else {
if (!bignumber_1.default.isBigNumber(value.value)) {
return 'value not a valid BigNumber';
}
}
if (!value.hasOwnProperty('vout')) {
return 'doesn\'t have property vout';
}
else {
if (typeof value.vout !== 'number') {
return 'vout is not a number';
}
else if (value.vout < 0) {
return 'vout is not a positive value';
}
}
if (!value.hasOwnProperty('address')) {
return 'doesn\'t have property address ';
}
else {
var pattern = RegExp(new BitcoinProtocol_1.BitcoinProtocol().addressValidationPattern);
if (!pattern.test(value.address)) {
return 'not a valid bitcoin address';
}
}
if (!value.hasOwnProperty('derivationPath')) {
return 'doesn\'t have property derivationPath';
}
else {
var protocol = new BitcoinProtocol_1.BitcoinProtocol();
try {
var mnemonic = 'spell device they juice trial skirt amazing boat badge steak usage february virus art survey';
protocol.getPublicKeyFromMnemonic(mnemonic, value.derivationPath);
}
catch (error) {
return 'invalid derivation path';
}
}
}
return null;
};
validate_1.validators.isValidBitcoinOutput = function (outs) {
// console.log(outs)
// if (!Array.isArray(outs)) {
// outs = [outs]
// }
if (!Array.isArray(outs)) {
return 'not an array';
}
for (var i = 0; i < outs.length; i++) {
var value = outs[i];
if (!value.hasOwnProperty('recipient')) {
return 'doesn\'t have property recipient';
}
else {
var pattern = RegExp(new BitcoinProtocol_1.BitcoinProtocol().addressValidationPattern);
if (!pattern.test(value.recipient)) {
return 'invalid Bitcoin address';
}
}
if (!value.hasOwnProperty('isChange')) {
return 'doesn\'t have property isChange ';
}
else {
if (typeof value.isChange !== 'boolean') {
return 'change is not a boolean';
}
}
if (!value.hasOwnProperty('value')) {
return 'doesn\'t have property value ';
}
else {
if (!bignumber_1.default.isBigNumber(value.value)) {
return 'value is not BigNumber';
}
}
}
return null;
};
validate_1.validators.isValidBitcoinFromArray = function (array) {
if (!Array.isArray(array)) {
return 'not an array of Bitcoin addresses';
}
for (var i = 0; i < array.length; i++) {
var address = array[i];
// const testpattern = RegExp(new BitcoinTestnetProtocol().addressValidationPattern) // TODO maybe don't use the testnetprotocol
var pattern = RegExp(new BitcoinProtocol_1.BitcoinProtocol().addressValidationPattern); // TODO maybe don't use the testnetprotocol
if (!pattern.test(address)) {
return 'not a valid bitcoin address';
}
}
return null;
};
validate_1.validators.isBitcoinAccount = function (accountIdentifier) {
if (accountIdentifier === null || typeof accountIdentifier === 'undefined') {
return null;
}
try {
var protocol = new BitcoinProtocol_1.BitcoinProtocol();
protocol.getAddressFromExtendedPublicKey(accountIdentifier, 0, 0);
return null;
}
catch (error) {
return 'not a valid Bitcoin account';
}
};
validate_1.validators.isValidBitcoinTxString = function (transaction) {
// allow empty values by default (needs to be checked by "presence" check)
if (transaction === null || typeof transaction === 'undefined') {
return null;
}
try {
var protocol = new BitcoinProtocol_1.BitcoinProtocol();
var bitcoinJSLib = protocol.options.config.bitcoinJSLib;
bitcoinJSLib.Transaction.fromHex(transaction);
return null;
}
catch (error) {
return 'is not a valid hex encoded Bitcoin transaction';
}
};
// AETERNITY
validate_1.validators.isMainNet = function (value) {
// allow empty values by default (needs to be checked by "presence" check)
if (value === null || typeof value === 'undefined') {
return null;
}
if (value !== 'ae_mainnet') {
return 'is not on mainnet';
}
return null;
};
validate_1.validators.isValidAeternityTx = function (transaction) {
// allow empty values by default (needs to be checked by "presence" check)
if (transaction === null || typeof transaction === 'undefined') {
return null;
}
if (typeof transaction === 'string' && !transaction.startsWith('tx_')) {
return 'invalid tx format';
}
else if (typeof transaction === 'string') {
try {
base64Check_1.default.decode(transaction.replace('tx_', ''));
return null;
}
catch (error) {
return "isn't base64 encoded";
}
}
else {
return "isn't a string";
}
};
validate_1.validators.isValidAeternityAccount = function (accountIdentifier) {
return new Promise(function (resolve) { return __awaiter(void 0, void 0, void 0, function () {
var protocol, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (accountIdentifier === null || typeof accountIdentifier === 'undefined') {
resolve();
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
protocol = new AeternityProtocol_1.AeternityProtocol();
return [4 /*yield*/, protocol.getTransactionsFromPublicKey(accountIdentifier, 1)];
case 2:
_a.sent();
resolve();
return [3 /*break*/, 4];
case 3:
error_2 = _a.sent();
resolve('not a valid Aeternity account');
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
};
// TEZOS
validate_1.validators.isValidTezosUnsignedTransaction = function (binaryTx) {
var rawTx = { binaryTransaction: binaryTx };
var unsignedTx = {
transaction: rawTx,
publicKey: '',
callbackURL: ''
};
return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
var protocol, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (binaryTx === null || typeof binaryTx === 'undefined') {
resolve('not a valid Tezos transaction');
}
protocol = new TezosProtocol_1.TezosProtocol();
// allow empty values by default (needs to be checked by "presence" check)
if (binaryTx === null || typeof binaryTx === 'undefined') {
reject();
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, protocol.getTransactionDetails(unsignedTx)];
case 2:
_a.sent();
resolve();
return [3 /*break*/, 4];
case 3:
error_3 = _a.sent();
// console.log(error)
resolve('not a valid Tezos transaction');
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
};
validate_1.validators.isValidTezosSignedTransaction = function (signedTransaction) {
var signedTx = {
accountIdentifier: '',
transaction: signedTransaction
};
return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
var protocol, error_4;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (signedTransaction === null || typeof signedTransaction === 'undefined') {
resolve('not a valid Tezos transaction');
}
protocol = new TezosProtocol_1.TezosProtocol();
// allow empty values by default (needs to be checked by "presence" check)
if (signedTransaction === null || typeof signedTransaction === 'undefined') {
reject();
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, protocol.getTransactionDetailsFromSigned(signedTx)];
case 2:
_a.sent();
resolve();
return [3 /*break*/, 4];
case 3:
error_4 = _a.sent();
// console.log(error)
resolve('not a valid Tezos transaction');
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
};
// SUBSTRATE
validate_1.validators.isValidSubstrateUnsignedTransaction = function (encoded) {
var unsignedTx = {
transaction: { encoded: encoded },
publicKey: '',
callbackURL: ''
};
return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
var protocol, error_5;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (encoded === null || typeof encoded === 'undefined') {
resolve('not a valid Substrate transaction');
}
protocol = new KusamaProtocol_1.KusamaProtocol();
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, protocol.getTransactionDetails(unsignedTx)];
case 2:
_a.sent();
resolve();
return [3 /*break*/, 4];
case 3:
error_5 = _a.sent();
resolve('not a valid Substrate transaction');
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
};
validate_1.validators.isValidSubstrateSignedTransaction = function (transaction) {
var signedTx = {
accountIdentifier: '',
transaction: transaction
};
return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
var protocol, error_6;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (transaction === null || typeof transaction === 'undefined') {
resolve('not a valid Substrate transaction');
}
protocol = new KusamaProtocol_1.KusamaProtocol();
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, protocol.getTransactionDetailsFromSigned(signedTx)];
case 2:
_a.sent();
resolve();
return [3 /*break*/, 4];
case 3:
error_6 = _a.sent();
resolve('not a valid Substrate transaction');
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
};
function validateSyncScheme(syncScheme) {
return __awaiter(this, void 0, void 0, function () {
var constraints;
return __generator(this, function (_a) {
constraints = {
version: {
presence: { allowEmpty: false },
numericality: { noStrings: true, onlyInteger: true, greaterThanOrEqualTo: 0 }
},
type: {
presence: { allowEmpty: false },
numericality: { noStrings: true, onlyInteger: true, greaterThanOrEqualTo: 0 }
},
protocol: {
presence: { allowEmpty: false },
type: 'String',
length: {
minimum: 1
}
},
payload: {
presence: { allowEmpty: false },
type: 'Array'
}
};
return [2 /*return*/, validate_1.validate(syncScheme, constraints)];
});
});
}
exports.validateSyncScheme = validateSyncScheme;
// tslint:disable-next-line
/*
function validateSerializationInput(from: string, fee: BigNumber, amount: BigNumber, publicKey: string, transaction: any) {
const constraints = {
from: {
presence: { allowEmpty: false },
type: 'String'
},
fee: {
presence: { allowEmpty: false },
type: 'BigNumber'
},
amount: {
presence: { allowEmpty: false },
type: 'BigNumber'
},
publicKey: {
presence: { allowEmpty: false },
type: 'String'
},
transaction: {
presence: { allowEmpty: false },
type: 'Array'
}
}
let test = validate(
{
from,
fee,
amount,
publicKey,
transaction
},
constraints
)
return test
}
*/
//# sourceMappingURL=validators.js.map