|
| 1 | +import abi from './metadata.json'; |
| 2 | +import { ApiPromise, WsProvider } from '@polkadot/api'; |
| 3 | +import { Database } from './server'; |
| 4 | +import { ContractPromise } from '@polkadot/api-contract'; |
| 5 | +import type { WeightV2 } from '@polkadot/types/interfaces'; |
| 6 | +import { getCoderByCoinType } from "@ensdomains/address-encoder"; |
| 7 | +import { createDotAddressDecoder } from '@ensdomains/address-encoder/utils' |
| 8 | +import { hexlify } from 'ethers/lib/utils'; |
| 9 | + |
| 10 | +const AZERO_COIN_TYPE = 643; |
| 11 | +const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; |
| 12 | +const EMPTY_CONTENT_HASH = '0x'; |
| 13 | + |
| 14 | +export interface GasLimit { |
| 15 | + refTime: number, |
| 16 | + proofSize: number, |
| 17 | +} |
| 18 | + |
| 19 | +export class AzeroId implements Database { |
| 20 | + ttl: number; |
| 21 | + tldToContract: Map<string, ContractPromise>; |
| 22 | + maxGasLimit: WeightV2; |
| 23 | + |
| 24 | + constructor(ttl: number, tldToContract: Map<string, ContractPromise>, maxGasLimit: WeightV2) { |
| 25 | + this.ttl = ttl; |
| 26 | + this.tldToContract = tldToContract; |
| 27 | + this.maxGasLimit = maxGasLimit; |
| 28 | + } |
| 29 | + |
| 30 | + static async init(ttl: number, providerURL: string, tldToContractAddress: Map<string, string>, gasLimit: GasLimit) { |
| 31 | + const wsProvider = new WsProvider(providerURL); |
| 32 | + const api = await ApiPromise.create({ provider: wsProvider }); |
| 33 | + |
| 34 | + const tldToContract = new Map<string, ContractPromise>(); |
| 35 | + tldToContractAddress.forEach((addr, tld) => { |
| 36 | + tldToContract.set(tld, new ContractPromise(api, abi, addr)) |
| 37 | + }) |
| 38 | + |
| 39 | + const maxGasLimit = api.registry.createType('WeightV2', gasLimit) as WeightV2; |
| 40 | + |
| 41 | + return new AzeroId( |
| 42 | + ttl, |
| 43 | + tldToContract, |
| 44 | + maxGasLimit, |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + async addr(name: string, coinType: number) { |
| 49 | + coinType = Number(coinType); // convert BigNumber to number |
| 50 | + console.log("addr", name, coinType); |
| 51 | + |
| 52 | + let value; |
| 53 | + if (coinType == AZERO_COIN_TYPE) { |
| 54 | + value = await this.fetchA0ResolverAddress(name); |
| 55 | + } else { |
| 56 | + let alias = AzeroId.getAlias(""+coinType); |
| 57 | + if (alias !== undefined) { |
| 58 | + const serviceKey = "address." + alias; |
| 59 | + value = await this.fetchRecord(name, serviceKey); |
| 60 | + } |
| 61 | + if (value === undefined) { |
| 62 | + const serviceKey = "address." + coinType; |
| 63 | + value = await this.fetchRecord(name, serviceKey); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (value === undefined) { |
| 68 | + value = coinType == 60? ZERO_ADDRESS:'0x'; |
| 69 | + } else { |
| 70 | + value = AzeroId.encodeAddress(value, coinType); |
| 71 | + } |
| 72 | + |
| 73 | + return { addr: value, ttl: this.ttl }; |
| 74 | + } |
| 75 | + |
| 76 | + async text(name: string, key: string) { |
| 77 | + console.log("text", name, key); |
| 78 | + const value = await this.fetchRecord(name, key) || ''; |
| 79 | + return { value, ttl: this.ttl }; |
| 80 | + } |
| 81 | + |
| 82 | + contenthash(name: string) { |
| 83 | + console.log("contenthash", name); |
| 84 | + return { contenthash: EMPTY_CONTENT_HASH, ttl: this.ttl }; |
| 85 | + } |
| 86 | + |
| 87 | + private async fetchRecord(domain: string, key: string) { |
| 88 | + let {name, contract} = this.processName(domain); |
| 89 | + const resp: any = await contract.query.getRecord( |
| 90 | + '', |
| 91 | + { |
| 92 | + gasLimit: this.maxGasLimit |
| 93 | + }, |
| 94 | + name, |
| 95 | + key |
| 96 | + ); |
| 97 | + |
| 98 | + return resp.output?.toHuman().Ok.Ok; |
| 99 | + } |
| 100 | + |
| 101 | + private async fetchA0ResolverAddress(domain: string) { |
| 102 | + let {name, contract} = this.processName(domain); |
| 103 | + const resp: any = await contract.query.getAddress( |
| 104 | + '', |
| 105 | + { |
| 106 | + gasLimit: this.maxGasLimit |
| 107 | + }, |
| 108 | + name |
| 109 | + ); |
| 110 | + |
| 111 | + return resp.output?.toHuman().Ok.Ok; |
| 112 | + } |
| 113 | + |
| 114 | + private processName(domain: string) { |
| 115 | + const labels = domain.split('.'); |
| 116 | + console.log("Labels:", labels); |
| 117 | + |
| 118 | + const name = labels.shift() || ''; |
| 119 | + const tld = labels.join('.'); |
| 120 | + const contract = this.tldToContract.get(tld); |
| 121 | + |
| 122 | + if (contract === undefined) { |
| 123 | + throw new Error(`TLD (.${tld}) not supported`); |
| 124 | + } |
| 125 | + |
| 126 | + return {name, contract}; |
| 127 | + } |
| 128 | + |
| 129 | + static getAlias(coinType: string) { |
| 130 | + const alias = new Map<string, string>([ |
| 131 | + ['0', 'btc'], |
| 132 | + ['60', 'eth'], |
| 133 | + ['354', 'dot'], |
| 134 | + ['434', 'ksm'], |
| 135 | + ['501', 'sol'], |
| 136 | + ]); |
| 137 | + |
| 138 | + return alias.get(coinType); |
| 139 | + } |
| 140 | + |
| 141 | + static encodeAddress(addr: string, coinType: number) { |
| 142 | + const isEvmCoinType = (c: number) => { |
| 143 | + return c == 60 || (c & 0x80000000)!=0 |
| 144 | + } |
| 145 | + |
| 146 | + if (coinType == AZERO_COIN_TYPE) { |
| 147 | + const azeroCoder = createDotAddressDecoder(42); |
| 148 | + return hexlify(azeroCoder(addr)); |
| 149 | + } |
| 150 | + if (isEvmCoinType(coinType) && !addr.startsWith('0x')) { |
| 151 | + addr = '0x' + addr; |
| 152 | + } |
| 153 | + |
| 154 | + try { |
| 155 | + const coder = getCoderByCoinType(coinType); |
| 156 | + return hexlify(coder.decode(addr)); |
| 157 | + } catch { |
| 158 | + return addr; |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments