1
+ import { Address } from "viem"
2
+ import { encodeAddress } from "@polkadot/util-crypto" ;
3
+ import { ss58Address } from "@polkadot-labs/hdkd-helpers" ;
4
+ import { hexToU8a } from "@polkadot/util" ;
5
+ import { blake2AsU8a , decodeAddress } from "@polkadot/util-crypto" ;
6
+ import { Binary } from "polkadot-api" ;
7
+ import { SS58_PREFIX } from "./config"
8
+
9
+ export function toViemAddress ( address : string ) : Address {
10
+ let addressNoPrefix = address . replace ( "0x" , "" )
11
+ return `0x${ addressNoPrefix } `
12
+ }
13
+
14
+ export function convertH160ToSS58 ( ethAddress : string ) {
15
+ // get the public key
16
+ const hash = convertH160ToPublicKey ( ethAddress ) ;
17
+
18
+ // Convert the hash to SS58 format
19
+ const ss58Address = encodeAddress ( hash , SS58_PREFIX ) ;
20
+ return ss58Address ;
21
+ }
22
+
23
+ export function convertPublicKeyToSs58 ( publickey : Uint8Array ) {
24
+ return ss58Address ( publickey , SS58_PREFIX ) ;
25
+ }
26
+
27
+ export function convertH160ToPublicKey ( ethAddress : string ) {
28
+ const prefix = "evm:" ;
29
+ const prefixBytes = new TextEncoder ( ) . encode ( prefix ) ;
30
+ const addressBytes = hexToU8a (
31
+ ethAddress . startsWith ( "0x" ) ? ethAddress : `0x${ ethAddress } `
32
+ ) ;
33
+ const combined = new Uint8Array ( prefixBytes . length + addressBytes . length ) ;
34
+
35
+ // Concatenate prefix and Ethereum address
36
+ combined . set ( prefixBytes ) ;
37
+ combined . set ( addressBytes , prefixBytes . length ) ;
38
+
39
+ // Hash the combined data (the public key)
40
+ const hash = blake2AsU8a ( combined ) ;
41
+ return hash ;
42
+ }
43
+
44
+ export function ss58ToEthAddress ( ss58Address : string ) {
45
+ // Decode the SS58 address to a Uint8Array public key
46
+ const publicKey = decodeAddress ( ss58Address ) ;
47
+
48
+ // Take the first 20 bytes of the hashed public key for the Ethereum address
49
+ const ethereumAddressBytes = publicKey . slice ( 0 , 20 ) ;
50
+
51
+ // Convert the 20 bytes into an Ethereum H160 address format (Hex string)
52
+ const ethereumAddress = '0x' + Buffer . from ( ethereumAddressBytes ) . toString ( 'hex' ) ;
53
+
54
+ return ethereumAddress ;
55
+ }
56
+
57
+ export function ss58ToH160 ( ss58Address : string ) : Binary {
58
+ // Decode the SS58 address to a Uint8Array public key
59
+ const publicKey = decodeAddress ( ss58Address ) ;
60
+
61
+ // Take the first 20 bytes of the hashed public key for the Ethereum address
62
+ const ethereumAddressBytes = publicKey . slice ( 0 , 20 ) ;
63
+
64
+
65
+ return new Binary ( ethereumAddressBytes ) ;
66
+ }
67
+
68
+ export function ethAddressToH160 ( ethAddress : string ) : Binary {
69
+ // Decode the SS58 address to a Uint8Array public key
70
+ const publicKey = hexToU8a ( ethAddress ) ;
71
+
72
+ // Take the first 20 bytes of the hashed public key for the Ethereum address
73
+ // const ethereumAddressBytes = publicKey.slice(0, 20);
74
+
75
+
76
+ return new Binary ( publicKey ) ;
77
+ }
0 commit comments