butterjs-sdk
Version:
Butter Network SDK
125 lines (124 loc) • 4 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.asciiToString = exports.asciiToHex = exports.getHexAddress = exports.hexToDecimalArray = exports.validateToken = exports.verifyNearAccountId = exports.validateAndParseAddressByChainId = void 0;
const address_1 = require("@ethersproject/address");
const chains_1 = require("../constants/chains");
const near_api_js_1 = require("near-api-js");
/**
* Validates an address and returns the parsed (checksummed) version of that address
* @param address the unchecksummed hex address
*/
function validateAndParseAddressByChainId(address, chainId) {
if ((0, chains_1.IS_EVM)(chainId)) {
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)}`);
}
}
else if ((0, chains_1.IS_NEAR)(chainId)) {
return address;
}
else {
throw new Error(`${(0, chains_1.ID_TO_NETWORK_NAME)(chainId)} is not supported`);
}
}
exports.validateAndParseAddressByChainId = validateAndParseAddressByChainId;
/**
* Near account rules
* minimum length is 2
* maximum length is 64
* Account ID consists of Account ID parts separated by .
* Account ID part consists of lowercase alphanumeric symbols separated by either _ or -.
* Account ID that is 64 characters long and consists of lowercase hex characters is a specific implicit account ID.
* @param accountId
* @param chainId
*/
async function verifyNearAccountId(accountId, chainId) {
const connectionConfig = {
networkId: (0, chains_1.ID_TO_NEAR_NETWORK)(chainId),
nodeUrl: (0, chains_1.ID_TO_DEFAULT_RPC_URL)(chainId),
};
const near = await (0, near_api_js_1.connect)(connectionConfig);
const account = await near.account(accountId);
try {
return {
isValid: true,
state: await account.state(),
};
}
catch (e) {
// @ts-ignore
console.log(e.message);
return {
isValid: false,
// @ts-ignore
errMsg: e.message,
};
}
}
exports.verifyNearAccountId = verifyNearAccountId;
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 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 isAddress
*/
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;
function asciiToString(input) {
let ret = '';
for (let i = 0; i < input.length; i++) {
ret += String.fromCharCode(input[i]);
}
return ret;
}
exports.asciiToString = asciiToString;
;