-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathens.js
49 lines (46 loc) · 1.82 KB
/
ens.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { BigNumber } from '@ethersproject/bignumber';
import { getAddress } from '@ethersproject/address';
import { hexZeroPad } from '@ethersproject/bytes';
const emptyAddress = '0x0000000000000000000000000000000000000000';
const SLIP44_MSB = 0x80000000;
export const convertEVMChainIdToCoinType = (chainId) => {
if (chainId >= SLIP44_MSB) {
throw Error(`chainId ${chainId} must be less than ${SLIP44_MSB}`);
}
// eslint-disable-next-line no-bitwise
return (SLIP44_MSB | chainId) >>> 0;
};
export async function getEVMChainAddress(provider, name, chainId) {
let address;
let coinType;
console.log('***getMulticoinAddres1', { name });
const resolver = await provider.getResolver(name);
console.log('***getMulticoinAddres2', { resolver });
if (resolver) {
const chainIdInt = parseInt(chainId, 16);
console.log('***getMulticoinAddres3');
if (chainIdInt === 1) {
coinType = 60;
} else {
coinType = convertEVMChainIdToCoinType(chainIdInt);
}
console.log('***getMulticoinAddres4', { chainId, chainIdInt, coinType });
const hexCoinType = BigNumber.from(coinType).toHexString();
console.log('***getMulticoinAddres5', { hexCoinType });
const encodedCoinType = hexZeroPad(hexCoinType, 32);
console.log('***getMulticoinAddres6', { encodedCoinType });
// 0xf1cb7e06 is address interface id
// https://docs.ens.domains/contract-api-reference/publicresolver#get-blockchain-address
const data = await resolver._fetchBytes('0xf1cb7e06', encodedCoinType);
console.log('***getMulticoinAddres7', { data });
if ([emptyAddress, '0x', null].includes(data)) {
console.log('***getMulticoinAddres8');
address = emptyAddress;
}
address = getAddress(data);
} else {
address = emptyAddress;
}
console.log('***getMulticoinAddres9', { address });
return address;
}