zodor-protocol-web3
Version:
SDK to interact with zodor protocol
104 lines • 5.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.purchaseSignature = exports.deploymentSignature = exports.TransferFromSignature = exports.createTransferSignature = exports.getRpcUrl = exports.getRpcProvider = exports.getContractAddresses = exports.POLYGON_AMOY_RPC_URL = exports.AVAX_SUBNET_RPC_URL = exports.AVAX_SUBNET_TESTNET_RPC_URL = void 0;
const ethers_1 = require("ethers");
const types_1 = require("./types");
exports.AVAX_SUBNET_TESTNET_RPC_URL = '';
exports.AVAX_SUBNET_RPC_URL = '';
exports.POLYGON_AMOY_RPC_URL = 'https://polygon-amoy-bor-rpc.publicnode.com';
const getContractAddresses = (network) => {
switch (network) {
case types_1.Network.AvaxSubnet:
return {
IdentityRegistryContractAddress: '',
ClaimRegistryContractAddress: '',
KeyManagerContractAddress: '',
};
case types_1.Network.polygonAmoy:
return {
IdentityRegistryContractAddress: '0x42840a505aEf1F68d5d80683Ac8b79eF758c415C',
ClaimRegistryContractAddress: '0xDda04dc55F81EeE4903d820000619C382476977e',
};
default:
return {
IdentityRegistryContractAddress: '',
ClaimRegistryContractAddress: '',
KeyManagerContractAddress: '',
};
}
};
exports.getContractAddresses = getContractAddresses;
const getRpcProvider = (network, customRpcUrl) => {
const rpcUrl = customRpcUrl || (0, exports.getRpcUrl)(network);
return new ethers_1.JsonRpcProvider(rpcUrl);
};
exports.getRpcProvider = getRpcProvider;
const getRpcUrl = (network) => {
switch (network) {
case types_1.Network.AvaxSubnet:
return exports.AVAX_SUBNET_RPC_URL;
case types_1.Network.polygonAmoy:
return exports.POLYGON_AMOY_RPC_URL;
default:
return exports.AVAX_SUBNET_TESTNET_RPC_URL;
}
};
exports.getRpcUrl = getRpcUrl;
const createTransferSignature = async (options) => {
const { trustedCallerPk, from, amount, to, balance } = options;
const 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');
const wallet = new ethers_1.Wallet(trustedCallerPk);
const eAmountPacked = ethers_1.ethers.solidityPackedKeccak256(['bytes'], [amount.value]);
const eBalancePacked = ethers_1.ethers.solidityPackedKeccak256(['bytes'], [balance.value]);
const transferMessage = ethers_1.ethers.solidityPackedKeccak256(['address', 'address', 'bytes32', 'bytes32', 'uint256'], [from, to, eBalancePacked, eAmountPacked, deadline]);
const signature = await wallet.signMessage(ethers_1.ethers.getBytes(transferMessage));
return { signature, deadline };
};
exports.createTransferSignature = createTransferSignature;
const TransferFromSignature = async (options) => {
const { 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');
const wallet = new ethers_1.Wallet(trustedCallerPk);
const deadline = parseInt(((new Date().getTime() + 3 * 60 * 1000) / 1000).toString());
const eAllowancePacked = ethers_1.ethers.solidityPackedKeccak256(['bytes'], [allowance.value]);
const eBalancePacked = ethers_1.ethers.solidityPackedKeccak256(['bytes'], [balance.value]);
const eAmountPacked = ethers_1.ethers.solidityPackedKeccak256(['bytes'], [amount.value]);
const transferMessage = ethers_1.ethers.solidityPackedKeccak256(['address', 'address', 'bytes32', 'bytes32', 'bytes32', 'uint256'], [from, to, eAllowancePacked, eBalancePacked, eAmountPacked, deadline]);
const signature = await wallet.signMessage(ethers_1.ethers.getBytes(transferMessage));
return { signature, deadline };
};
exports.TransferFromSignature = TransferFromSignature;
const deploymentSignature = async (options) => {
const { supply, encodedSupply, trustedCallerPk } = options;
if (!trustedCallerPk)
throw new Error('Trusted Caller not provided');
if (!supply || !encodedSupply)
throw new Error('Invalid Inputs');
const wallet = new ethers_1.Wallet(trustedCallerPk);
const eTotalSupplyPacked = ethers_1.ethers.solidityPackedKeccak256(['bytes'], [encodedSupply.value]);
const message = ethers_1.ethers.solidityPackedKeccak256(['uint256', 'bytes32'], [supply, eTotalSupplyPacked]);
const signature = await wallet.signMessage(ethers_1.ethers.getBytes(message));
return signature;
};
exports.deploymentSignature = deploymentSignature;
const purchaseSignature = async (options) => {
const { 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');
const wallet = new ethers_1.Wallet(trustedCallerPk);
const eTransferUnit = ethers_1.ethers.solidityPackedKeccak256(['bytes'], [merc20Amount.value]);
const purchaseMessage = ethers_1.ethers.solidityPackedKeccak256(['address', 'address', 'address', 'uint256', 'bytes32'], [treasury, account, tokenAddress, usdAmount, eTransferUnit]);
const signature = await wallet.signMessage(ethers_1.ethers.getBytes(purchaseMessage));
return signature;
};
exports.purchaseSignature = purchaseSignature;
//# sourceMappingURL=utils.js.map