-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
126 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
node_modules | ||
build | ||
dist | ||
_build | ||
_static | ||
_templates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
******************* | ||
Introduction | ||
******************* | ||
|
||
In addition to `DNSRegistrar` Truffle based artifact which you can call the smart contract directly, we provide a javascript wrapper which looks up DNS record, extract a proof, submit the proof via DNSSec Oracle, and register to ENS via DNSRegistrar using the proof (or delete if the entry does not exist). | ||
|
||
Example | ||
|
||
.. code-block:: javascript | ||
var DNSRegistrarJs = require('@ensdomains/dnsregistrar'); | ||
dnsregistrar = new DNSRegistrarJs(provider, dnsregistraraddress); | ||
dnsregistrar.claim('foo.test').then((claim)=>{ | ||
claim.submit({from:account}); | ||
}) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
******************* | ||
Libraries | ||
******************* | ||
|
||
DnsRegistrar | ||
============ | ||
|
||
`DnsRegistrar` lookups a domainname for a given DNS type and returns a claim which you use to submit proof into or delete proof from DNSSEC Oracle as well as register the domain to ENS through the DNSRegistrar contract | ||
|
||
.. autofunction:: DNSRegistrar#claim | ||
|
||
|
||
.. autofunction:: Claim#getOwner | ||
.. autofunction:: Claim#submit | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sphinx-js==2.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,100 @@ | ||
const DnsProve = require('@ensdomains/dnsprovejs'); | ||
const dns = require('./dns.js'); | ||
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, proveResult, textDomain, encodeName}){ | ||
this.oracle = oracle | ||
this.registrar = registrar | ||
this.proveResult = proveResult | ||
this.textDomain = textDomain | ||
this.encodeName = encodeName | ||
} | ||
|
||
/** | ||
* returns owner ETH address from the DNS record. | ||
*/ | ||
getOwner(){ | ||
return this.proveResult.results[this.proveResult.results.length -1].rrs[0].data[0].toString().split('=')[1]; | ||
} | ||
|
||
async getProven(){ | ||
return await this.oracle.getProven(this.proveResult) | ||
} | ||
|
||
async submitAll(params = {}){ | ||
await this.oracle.submitAll(this.proveResult, params); | ||
} | ||
|
||
/** | ||
* submit functions 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 = {}){ | ||
if(this.proveResult.nsec){ | ||
let proofs = this.proveResult.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(proveResult.found){ | ||
let data = await this.oracle.getAllProofs(proveResult, params); | ||
let allProven = await this.oracle.allProven(proveResult) | ||
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); | ||
} | ||
|
||
/** | ||
* Claim function returns a claim object with functions which allow 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 = dns.hexEncodeName(name + '.'); | ||
let encodedName = '0x' + packet.name.encode(name).toString('hex'); | ||
let textDomain = '_ens.' + name; | ||
let proveResult = await this.dnsprover.lookup('TXT', textDomain); | ||
let oracle = await this.dnsprover.getOracle(this.oracleAddress); | ||
return { | ||
return new Claim({ | ||
oracle:oracle, | ||
result:proveResult, | ||
found:proveResult.found, | ||
nsec:proveResult.nsec, | ||
getProven: async () => { | ||
return await oracle.getProven(proveResult) | ||
}, | ||
getOwner: () => { | ||
return proveResult.results[proveResult.results.length -1].rrs[0].data[0].toString().split('=')[1]; | ||
}, | ||
submitAll: async (params = {}) => { | ||
await oracle.submitAll(proveResult, params); | ||
}, | ||
submit: async (params = {}) => { | ||
if(proveResult.nsec){ | ||
let proofs = proveResult.proofs; | ||
await oracle.deleteProof( | ||
'TXT', 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(encodedName, '0x').send(params); | ||
}else if(proveResult.found){ | ||
let data = await oracle.getAllProofs(proveResult, params); | ||
let allProven = await oracle.allProven(proveResult) | ||
if(allProven){ | ||
await this.registrar.methods | ||
.claim(encodedName, data[1]) | ||
.send(params) | ||
}else{ | ||
await this.registrar.methods | ||
.proveAndClaim(encodedName, data[0], data[1]) | ||
.send(params) | ||
} | ||
}else{ | ||
throw("Nothing to prove") | ||
} | ||
} | ||
} | ||
proveResult:proveResult, | ||
registrar:this.registrar, | ||
textDomain: textDomain, | ||
encodedName: encodedName | ||
}) | ||
} | ||
} | ||
module.exports = DNSRegistrar; |