Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
setDefaultAccount(account) {
if (!this.isEnable || !account || !this.isConnect) {
return null;
}
const isAddress = validation.isAddress(account.address);
if (!isAddress) {
throw new Error('input param is not address type');
}
this.defaultAccount = {
base16: toChecksumAddress(account.address),
base58: encodeBase58(account.address),
bech32: toBech32Address(account.address)
};
dispatchEvent(onAddressListing);
}
export const toBech32Address = (address: string): string => {
if (!validation.isAddress(address)) {
throw new Error('Invalid address format.');
}
const addrBz = convertBits(
Buffer.from(address.replace('0x', ''), 'hex'),
8,
5,
);
if (addrBz === null) {
throw new Error('Could not convert byte Buffer to 5-bit Buffer');
}
return encode(HRP, addrBz);
};
export const toChecksumAddress = (address: string): string => {
if (!validation.isAddress(address)) {
throw new Error(`${address} is not a valid base 16 address`);
}
address = address.toLowerCase().replace('0x', '');
const hash = hashjs
.sha256()
.update(address, 'hex')
.digest('hex');
const v = new BN(hash, 'hex', 'be');
let ret = '0x';
for (let i = 0; i < address.length; i++) {
if ('0123456789'.indexOf(address[i]) !== -1) {
ret += address[i];
} else {
ret += v.and(new BN(2).pow(new BN(255 - 6 * i))).gte(new BN(1))
const decoded = fromBech32Address(this.raw, 'tzil').toLowerCase();
this.bytes20 = decoded.startsWith('0x') ? decoded.substring(2) : decoded;
return;
}
if (
bech32Bool === true &&
validation.isAddress(fromBech32Address(this.raw))
) {
this.addressType = AddressType.bech32;
const decoded = fromBech32Address(this.raw).toLowerCase();
this.bytes20 = decoded.startsWith('0x') ? decoded.substring(2) : decoded;
return;
}
if (base58Bool === true && validation.isAddress(decodeBase58(this.raw))) {
this.addressType = AddressType.base58;
const decoded = decodeBase58(this.raw).toLowerCase();
this.bytes20 = decoded.startsWith('0x') ? decoded.substring(2) : decoded;
return;
}
throw new Error('unknown address');
}
}
private getAddressType() {
const addrBool = validation.isAddress(this.raw);
const base58Bool = validation.isBase58(this.raw);
const bech32Bool = validation.isBech32(this.raw);
const bech32TestNetBool = validation.isBech32TestNet(this.raw);
const checksumBool = isValidChecksumAddress(this.raw);
if (addrBool === true && checksumBool === false) {
this.addressType = AddressType.bytes20;
this.bytes20 = this.raw.startsWith('0x')
? this.raw.substring(2)
: this.raw;
return;
}
if (addrBool === true && checksumBool === true) {
this.addressType = AddressType.checkSum;
this.bytes20 = this.raw.toLowerCase().substring(2);
export const isValidChecksumAddress = (address: string): boolean => {
return (
validation.isAddress(address.replace('0x', '')) &&
toChecksumAddress(address) === address
);
};
export function toAccountFormat(address) {
const isAddress = validation.isAddress(address)
if (!isAddress) {
throw new Error('input param is not address type')
}
return {
base16: toChecksumAddress(address),
bech32: toBech32Address(address)
}
}
return;
}
if (
bech32TestNetBool === true &&
validation.isAddress(fromBech32Address(this.raw, 'tzil'))
) {
this.addressType = AddressType.bech32;
const decoded = fromBech32Address(this.raw, 'tzil').toLowerCase();
this.bytes20 = decoded.startsWith('0x') ? decoded.substring(2) : decoded;
return;
}
if (
bech32Bool === true &&
validation.isAddress(fromBech32Address(this.raw))
) {
this.addressType = AddressType.bech32;
const decoded = fromBech32Address(this.raw).toLowerCase();
this.bytes20 = decoded.startsWith('0x') ? decoded.substring(2) : decoded;
return;
}
if (base58Bool === true && validation.isAddress(decodeBase58(this.raw))) {
this.addressType = AddressType.base58;
const decoded = decodeBase58(this.raw).toLowerCase();
this.bytes20 = decoded.startsWith('0x') ? decoded.substring(2) : decoded;
return;
}
throw new Error('unknown address');
}
}