mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 05:38:03 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
+2
@@ -0,0 +1,2 @@
|
||||
import type { HexString } from '@pezkuwi/util/types';
|
||||
export declare function ethereumEncode(addressOrPublic?: string | Uint8Array): HexString;
|
||||
@@ -0,0 +1,25 @@
|
||||
import { u8aToHex, u8aToU8a } from '@pezkuwi/util';
|
||||
import { keccakAsU8a } from '../keccak/index.js';
|
||||
import { secp256k1Expand } from '../secp256k1/index.js';
|
||||
function getH160(u8a) {
|
||||
if ([33, 65].includes(u8a.length)) {
|
||||
u8a = keccakAsU8a(secp256k1Expand(u8a));
|
||||
}
|
||||
return u8a.slice(-20);
|
||||
}
|
||||
export function ethereumEncode(addressOrPublic) {
|
||||
if (!addressOrPublic) {
|
||||
return '0x';
|
||||
}
|
||||
const u8aAddress = u8aToU8a(addressOrPublic);
|
||||
if (![20, 32, 33, 65].includes(u8aAddress.length)) {
|
||||
throw new Error(`Invalid address or publicKey provided, received ${u8aAddress.length} bytes input`);
|
||||
}
|
||||
const address = u8aToHex(getH160(u8aAddress), -1, false);
|
||||
const hash = u8aToHex(keccakAsU8a(address), -1, false);
|
||||
let result = '';
|
||||
for (let i = 0; i < 40; i++) {
|
||||
result = `${result}${parseInt(hash[i], 16) > 7 ? address[i].toUpperCase() : address[i]}`;
|
||||
}
|
||||
return `0x${result}`;
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export { ethereumEncode } from './encode.js';
|
||||
export { isEthereumAddress } from './isAddress.js';
|
||||
export { isEthereumChecksum } from './isChecksum.js';
|
||||
@@ -0,0 +1,3 @@
|
||||
export { ethereumEncode } from './encode.js';
|
||||
export { isEthereumAddress } from './isAddress.js';
|
||||
export { isEthereumChecksum } from './isChecksum.js';
|
||||
@@ -0,0 +1 @@
|
||||
export declare function isEthereumAddress(address?: string): boolean;
|
||||
@@ -0,0 +1,11 @@
|
||||
import { isHex } from '@pezkuwi/util';
|
||||
import { isEthereumChecksum } from './isChecksum.js';
|
||||
export function isEthereumAddress(address) {
|
||||
if (!address || address.length !== 42 || !isHex(address)) {
|
||||
return false;
|
||||
}
|
||||
else if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) {
|
||||
return true;
|
||||
}
|
||||
return isEthereumChecksum(address);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export declare function isEthereumChecksum(_address: string): boolean;
|
||||
@@ -0,0 +1,17 @@
|
||||
import { u8aToHex } from '@pezkuwi/util';
|
||||
import { keccakAsU8a } from '../keccak/index.js';
|
||||
function isInvalidChar(char, byte) {
|
||||
return char !== (byte > 7
|
||||
? char.toUpperCase()
|
||||
: char.toLowerCase());
|
||||
}
|
||||
export function isEthereumChecksum(_address) {
|
||||
const address = _address.replace('0x', '');
|
||||
const hash = u8aToHex(keccakAsU8a(address.toLowerCase()), -1, false);
|
||||
for (let i = 0; i < 40; i++) {
|
||||
if (isInvalidChar(address[i], parseInt(hash[i], 16))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user