UNPKG

zodor-protocol-web3

Version:

SDK to interact with zodor protocol

147 lines (146 loc) 6.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.purchaseSignature = exports.getRpcUrl = exports.getRpcProvider = exports.getContractAddresses = exports.deploymentSignature = exports.createTransferSignature = exports.TransferFromSignature = exports.POLYGON_AMOY_RPC_URL = exports.AVAX_SUBNET_TESTNET_RPC_URL = exports.AVAX_SUBNET_RPC_URL = void 0; var _ethers = require("ethers"); var _types = require("./types"); function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } var AVAX_SUBNET_TESTNET_RPC_URL = exports.AVAX_SUBNET_TESTNET_RPC_URL = ''; var AVAX_SUBNET_RPC_URL = exports.AVAX_SUBNET_RPC_URL = ''; var POLYGON_AMOY_RPC_URL = exports.POLYGON_AMOY_RPC_URL = 'https://polygon-amoy-bor-rpc.publicnode.com'; var getContractAddresses = network => { switch (network) { case _types.Network.AvaxSubnet: return { IdentityRegistryContractAddress: '', ClaimRegistryContractAddress: '', KeyManagerContractAddress: '' }; case _types.Network.polygonAmoy: return { IdentityRegistryContractAddress: '0x42840a505aEf1F68d5d80683Ac8b79eF758c415C', ClaimRegistryContractAddress: '0xDda04dc55F81EeE4903d820000619C382476977e' }; default: return { IdentityRegistryContractAddress: '', ClaimRegistryContractAddress: '', KeyManagerContractAddress: '' }; } }; exports.getContractAddresses = getContractAddresses; var getRpcProvider = (network, customRpcUrl) => { var rpcUrl = customRpcUrl || getRpcUrl(network); return new _ethers.JsonRpcProvider(rpcUrl); }; exports.getRpcProvider = getRpcProvider; var getRpcUrl = network => { switch (network) { case _types.Network.AvaxSubnet: return AVAX_SUBNET_RPC_URL; case _types.Network.polygonAmoy: return POLYGON_AMOY_RPC_URL; default: return AVAX_SUBNET_TESTNET_RPC_URL; } }; exports.getRpcUrl = getRpcUrl; var createTransferSignature = exports.createTransferSignature = /*#__PURE__*/function () { var _ref = _asyncToGenerator(function* (options) { var { trustedCallerPk, from, amount, to, balance } = options; var deadline = parseInt(((new Date().getTime() + 3 * 60 * 1000) / 1000).toString()); if (!trustedCallerPk) throw new Error('Trusted Caller not provided'); if (!from || !amount || !to || !balance) throw new Error('Invalid Inputs'); var wallet = new _ethers.Wallet(trustedCallerPk); var eAmountPacked = _ethers.ethers.solidityPackedKeccak256(['bytes'], [amount.value]); var eBalancePacked = _ethers.ethers.solidityPackedKeccak256(['bytes'], [balance.value]); var transferMessage = _ethers.ethers.solidityPackedKeccak256(['address', 'address', 'bytes32', 'bytes32', 'uint256'], [from, to, eBalancePacked, eAmountPacked, deadline]); var signature = yield wallet.signMessage(_ethers.ethers.getBytes(transferMessage)); return { signature, deadline }; }); return function createTransferSignature(_x) { return _ref.apply(this, arguments); }; }(); var TransferFromSignature = exports.TransferFromSignature = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(function* (options) { var { trustedCallerPk, from, amount, to, balance, allowance } = options; if (!trustedCallerPk) throw new Error('Trusted Caller not provided'); if (!from || !amount || !to || !balance || !allowance) throw new Error('Invalid Inputs'); var wallet = new _ethers.Wallet(trustedCallerPk); var deadline = parseInt(((new Date().getTime() + 3 * 60 * 1000) / 1000).toString()); var eAllowancePacked = _ethers.ethers.solidityPackedKeccak256(['bytes'], [allowance.value]); var eBalancePacked = _ethers.ethers.solidityPackedKeccak256(['bytes'], [balance.value]); var eAmountPacked = _ethers.ethers.solidityPackedKeccak256(['bytes'], [amount.value]); var transferMessage = _ethers.ethers.solidityPackedKeccak256(['address', 'address', 'bytes32', 'bytes32', 'bytes32', 'uint256'], [from, to, eAllowancePacked, eBalancePacked, eAmountPacked, deadline]); var signature = yield wallet.signMessage(_ethers.ethers.getBytes(transferMessage)); return { signature, deadline }; }); return function TransferFromSignature(_x2) { return _ref2.apply(this, arguments); }; }(); var deploymentSignature = exports.deploymentSignature = /*#__PURE__*/function () { var _ref3 = _asyncToGenerator(function* (options) { var { supply, encodedSupply, trustedCallerPk } = options; if (!trustedCallerPk) throw new Error('Trusted Caller not provided'); if (!supply || !encodedSupply) throw new Error('Invalid Inputs'); var wallet = new _ethers.Wallet(trustedCallerPk); var eTotalSupplyPacked = _ethers.ethers.solidityPackedKeccak256(['bytes'], [encodedSupply.value]); var message = _ethers.ethers.solidityPackedKeccak256(['uint256', 'bytes32'], [supply, eTotalSupplyPacked]); var signature = yield wallet.signMessage(_ethers.ethers.getBytes(message)); return signature; }); return function deploymentSignature(_x3) { return _ref3.apply(this, arguments); }; }(); var purchaseSignature = exports.purchaseSignature = /*#__PURE__*/function () { var _ref4 = _asyncToGenerator(function* (options) { var { trustedCallerPk, treasury, account, transferSignature, merc20Amount, usdAmount, tokenAddress } = options; if (!trustedCallerPk) throw new Error('Trusted Caller not provided'); if (!treasury || !account || !transferSignature) throw new Error('Invalid Inputs'); var wallet = new _ethers.Wallet(trustedCallerPk); var eTransferUnit = _ethers.ethers.solidityPackedKeccak256(['bytes'], [merc20Amount.value]); var purchaseMessage = _ethers.ethers.solidityPackedKeccak256(['address', 'address', 'address', 'uint256', 'bytes32'], [treasury, account, tokenAddress, usdAmount, eTransferUnit]); var signature = yield wallet.signMessage(_ethers.ethers.getBytes(purchaseMessage)); return signature; }); return function purchaseSignature(_x4) { return _ref4.apply(this, arguments); }; }();