barterjs-sdk
Version:
Barter Network SDK
107 lines (106 loc) • 3.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.asciiToHex = exports.getHexAddress = exports.decimalArrayToHex = exports.hexToDecimalArray = exports.validateToken = exports.validateAndParseAddressByChainId = void 0;
const address_1 = require("@ethersproject/address");
const chains_1 = require("../constants/chains");
/**
* Validates an address and returns the parsed (checksummed) version of that address
* @param address the unchecksummed hex address
*/
function validateAndParseAddressByChainId(address, chainId) {
switch (chainId) {
case chains_1.ChainId.MAP:
case chains_1.ChainId.ETH_PRIV:
case chains_1.ChainId.BSC_TEST:
case chains_1.ChainId.MAP_TEST: {
try {
return (0, address_1.getAddress)(address);
}
catch (error) {
throw new Error(`${address} is not a valid address on ${(0, chains_1.ID_TO_NETWORK_NAME)(chainId)}`);
}
}
case chains_1.ChainId.NEAR_TESTNET: {
// address = address.toLowerCase();
// const words: string[] = address.split('.');
// if (words[words.length - 1] != 'testnet') {
// throw new Error(
// `${address} is not a valid address on ${ID_TO_NETWORK_NAME(chainId)}`
// );
// }
// if (!/^[a-zA-Z\\.]+$/.test(address)) {
// throw new Error(
// `${address} is not a valid address on ${ID_TO_NETWORK_NAME(chainId)}`
// );
// }
return address;
}
default: {
throw new Error(`${(0, chains_1.ID_TO_NETWORK_NAME)(chainId)} is not supported`);
}
}
}
exports.validateAndParseAddressByChainId = validateAndParseAddressByChainId;
function validateToken(token) {
return validateAndParseAddressByChainId(token.address, token.chainId);
}
exports.validateToken = validateToken;
/**
* convert hex format address to decimal array
* @param address
* @param chainId
*/
function hexToDecimalArray(address, chainId) {
address = validateAndParseAddressByChainId(address, chainId);
let ret = [];
for (let i = 2; i < address.length; i = i + 2) {
ret.push(parseInt(address.slice(i, i + 2), 16));
}
return ret;
}
exports.hexToDecimalArray = hexToDecimalArray;
function decimalArrayToHex(decimals) {
let ret = '';
for (let i = 0; i < decimals.length; i++) {
ret += String.fromCharCode(decimals[i]);
}
return ret;
}
exports.decimalArrayToHex = decimalArrayToHex;
function getHexAddress(address, chainId, isAddress) {
if ((0, chains_1.IS_EVM)(chainId)) {
return address;
}
else if ((0, chains_1.IS_NEAR)(chainId)) {
return address.startsWith('0x') ? address : asciiToHex(address, isAddress);
}
else {
throw new Error(`chain id: ${chainId} not supported`);
}
}
exports.getHexAddress = getHexAddress;
/**
* @param input
* @param hexLength
*/
function asciiToHex(input, isAddress) {
let hexArr = [];
for (let i = 0; i < input.length; i++) {
let hex = Number(input.charCodeAt(i)).toString(16);
hexArr.push(hex);
}
let res = hexArr.join('');
if (isAddress) {
if (res.length > 40) {
res = res.substring(0, 40);
}
else if (res.length < 40) {
let diff = 40 - res.length;
for (let i = 0; i < diff; i++) {
res = '0' + res;
}
}
}
return '0x' + res;
}
exports.asciiToHex = asciiToHex;