-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathethnamed.ls
More file actions
70 lines (70 loc) · 3.01 KB
/
ethnamed.ls
File metadata and controls
70 lines (70 loc) · 3.01 KB
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
require! {
\./addresses.json : { Ethnamed }
\./Ethnamed.abi.json : abi
\superagent : { post, get }
\solidity-sha3 : { sha3num }
\./config.json : { url }
\./get-verify-email-url.ls
}
get-contract-instance = (web3, abi, addr)->
new web3.eth.Contract(abi, addr)
extract-signature = (signature)->
sig = signature.slice 2
r = \0x + sig.slice 0, 64
s = \0x + sig.slice 64, 128
v = \0x + sig.slice 128, 130
{ v, r, s }
get-domain = (name)->
| name.index-of(\.) > -1 => name
| _ => "#{name}.ethnamed.io"
verify-email = (emailkey, cb)->
url = get-verify-email-url emailkey
err, data <- get url .end
return cb data.text if err?
cb null, JSON.parse(data.text)
get-access-key = ({ name, record }, cb)->
return cb "Name is required" if not name?
return cb "Record is required" if not record?
domain = get-domain name
err, data <- post url .set(\name, domain).set(\record, record).end
return cb data.text if err?
cb null, JSON.parse(data.text)
builder-setup-record = (web3, contract)-> ({ amount-ethers, name, record, owner }, cb)->
err, access-key <- get-access-key { name, record }
return cb "Owner address is not defined" if not owner?
return cb err if err?
return cb "Access keys not found" if not access-key?
{ signature, record, name, length, block-expiry } = access-key
{ v, r, s } = extract-signature signature
#console.log "\"#{length}\", \"#{name}\", \"#{record}\", \"#{block-expiry}\", \"#{owner}\", \"#{v}\", \"#{r}\", \"#{s}\""
transaction =
to: Ethnamed
gas: 500000
value: web3.utils.to-wei("#{amount-ethers}", \ether).to-string!
data: contract.methods.set-or-update-record(length, name, record, block-expiry, owner, v, r, s).encodeABI!
decoded-data: "setOrUpdateRecord(\"#{length}\", \"#{name}\", \"#{record}\", \"#{block-expiry}\", \"#{owner}\", \"#{v}\", \"#{r}\", \"#{s}\")"
#console.log transaction
web3.eth.send-transaction transaction, cb
builder-whois = (web3, contract)-> (record, cb)->
hash = sha3num record
contract.methods.whois hash .call cb
builder-verify-record = (web3, contract) -> (name, cb)->
return cb err if err?
contract.methods.resolve(get-domain(name)).call cb
builder-send-to = (web3, contract)-> ({ amount-ethers, name }, cb)->
return cb "Amount ETH is required" if not amount-ethers?
return cb "Name is required" if not name?
transaction =
to: Ethnamed
gas: 500000
value: web3.utils.to-wei("#{amount-ethers}", \ether).to-string!
data: contract.methods.send-to(name).encodeABI!
decoded-data: "sendTo(\"#{name}\")"
web3.eth.send-transaction transaction, cb
module.exports = (web3)->
contract = get-contract-instance web3, abi, Ethnamed
setup-record = builder-setup-record web3, contract
verify-record = builder-verify-record web3, contract
whois = builder-whois web3, contract
send-to = builder-send-to web3, contract
{ ...contract, setup-record, verify-record, whois, verify-email }