mirror of
https://github.com/pezkuwichain/pezkuwi-api.git
synced 2026-11-20 04:56:12 +00:00
- @pezkuwi/papi-utils (rebrand of @polkadot-api/utils) - @pezkuwi/bizinikiwi-bindings (rebrand of @polkadot-api/substrate-bindings) - @pezkuwi/metadata-builders (rebrand of @polkadot-api/metadata-builders) - @pezkuwi/merkleize-metadata (rebrand of @polkadot-api/merkleize-metadata) All @polkadot-api references replaced with @pezkuwi equivalents.
38 lines
1.3 KiB
JavaScript
38 lines
1.3 KiB
JavaScript
import { fromHex, toHex } from '@pezkuwi/papi-utils';
|
|
import { Bytes, createCodec, createDecoder } from 'scale-ts';
|
|
import '@noble/hashes/blake2.js';
|
|
import '@noble/hashes/blake3.js';
|
|
import { Keccak256 } from '../../hashes/keccak.mjs';
|
|
import { textEncoder } from './fixed-str.mjs';
|
|
|
|
const getFormattedAddress = (hexAddress) => {
|
|
const nonChecksum = hexAddress.slice(2);
|
|
const hashedAddress = toHex(Keccak256(textEncoder.encode(nonChecksum))).slice(
|
|
2
|
|
);
|
|
const result = new Array(40);
|
|
for (let i = 0; i < 40; i++) {
|
|
const checksumVal = parseInt(hashedAddress[i], 16);
|
|
const char = nonChecksum[i];
|
|
result[i] = checksumVal > 7 ? char.toUpperCase() : char;
|
|
}
|
|
return `0x${result.join("")}`;
|
|
};
|
|
const bytes20Dec = Bytes(20)[1];
|
|
const ethAccount = createCodec(
|
|
(input) => {
|
|
const bytes = fromHex(input);
|
|
if (bytes.length !== 20)
|
|
throw new Error(`Invalid length found on EthAddress(${input})`);
|
|
const hexAddress = toHex(bytes);
|
|
if (input === hexAddress || input === hexAddress.toUpperCase()) return bytes;
|
|
if (getFormattedAddress(hexAddress) !== input)
|
|
throw new Error(`Invalid checksum found on EthAddress(${input})`);
|
|
return bytes;
|
|
},
|
|
createDecoder((bytes) => getFormattedAddress(toHex(bytes20Dec(bytes))))
|
|
);
|
|
|
|
export { ethAccount };
|
|
//# sourceMappingURL=ethAccount.mjs.map
|