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