/
dnsregistrar.js
120 lines (111 loc) · 3.62 KB
/
dnsregistrar.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const DnsProve = require('@ensdomains/dnsprovejs');
const artifact = require('../build/contracts/DNSRegistrar.json');
const Web3 = require('web3');
const abi = artifact.abi;
var packet = require('dns-packet');
class Claim {
constructor({ oracle, registrar, result, textDomain, encodedName }) {
this.oracle = oracle;
this.registrar = registrar;
this.result = result;
this.textDomain = textDomain;
this.encodedName = encodedName;
}
/**
* returns `Oracle <https://dnsprovejs.readthedocs.io/en/latest/libraries.html#oracle>`_ object
*/
getOracle() {
return this.oracle;
}
/**
* returns `DnsResult <https://dnsprovejs.readthedocs.io/en/latest/libraries.html#dnsresult>`_ object
*/
getResult() {
return this.result;
}
/**
* returns owner ETH address from the DNS record.
*/
getOwner() {
return this.result.results[this.result.results.length - 1].rrs[0].data[0]
.toString()
.split('=')[1];
}
async getProven() {
return await this.oracle.getProven(this.result);
}
async submitAll(params = {}) {
await this.oracle.submitAll(this.result, params);
}
/**
* Either submit or delete proof depending on the state of DNSRecord and DNSSEC Oracle
*
* - it deletes an entry if DNS record returns NSEC/NSEC3(Next Secure)
* - it submits proof to DNSSEC Oracle contract if not all proofs are in the contract
* - it submits proof to DNSSEC Oracle contract and claims via DNSRegistrar contract if not all proofs are in the DNSSEC Oracle contract
*
* @param {Object} params - optional parameter to send to smart contract, such as from, gas, etc
*/
async submit(params = {}) {
var result = this.result;
if (result.nsec) {
let proofs = this.result.proofs;
await this.oracle.deleteProof(
'TXT',
this.textDomain,
proofs[proofs.length - 1],
proofs[proofs.length - 2],
params
);
// Anyone can put empty byte if no record on DNSSEC Oracle
await this.registrar.methods.claim(this.encodedName, '0x').send(params);
} else if (result.found) {
let data = await this.oracle.getAllProofs(result, params);
let allProven = await this.oracle.allProven(result);
if (allProven) {
await this.registrar.methods
.claim(this.encodedName, data[1])
.send(params);
} else {
await this.registrar.methods
.proveAndClaim(this.encodedName, data[0], data[1])
.send(params);
}
} else {
throw 'Nothing to prove';
}
}
}
class DNSRegistrar {
constructor(provider, registrarAddress) {
let web3 = new Web3(provider);
this.registrar = new web3.eth.Contract(abi, registrarAddress);
this.dnsprover = new DnsProve(provider);
}
/**
* returns a claim object which allows you to claim
* the ownership of a given name on ENS by submitting the proof
* into DNSSEC oracle as well as claiming the name via the registrar
* @param {string} name - name of the domain you want to claim
*/
async claim(name) {
// for caching purpose.
if (!this.oracleAddress) {
this.oracleAddress = (await this.registrar.methods
.oracle()
.call()).toLowerCase();
}
let encodedName = '0x' + packet.name.encode(name).toString('hex');
let textDomain = '_ens.' + name;
let result = await this.dnsprover.lookup('TXT', textDomain);
let oracle = await this.dnsprover.getOracle(this.oracleAddress);
return new Claim({
oracle: oracle,
result: result,
registrar: this.registrar,
textDomain: textDomain,
encodedName: encodedName
});
}
}
module.exports = DNSRegistrar;