@@ -32,13 +32,16 @@ public struct ENS {
32
32
return contract!
33
33
} ( )
34
34
35
+ private lazy var defaultOptions : Web3Options = {
36
+ return Web3Options . defaultOptions ( )
37
+ } ( )
38
+
35
39
36
40
//MARK: - Returns resolver for the given domain
37
41
mutating func resolver( forDomain domain: String ) -> Result < ResolverENS , Web3Error > {
38
42
guard let nameHash = NameHash . nameHash ( domain) else { return Result . failure ( Web3Error . dataError) }
39
- let options = Web3Options . defaultOptions ( )
40
- guard let transaction = self . registryContract. method ( " resolver " , parameters: [ nameHash as AnyObject ] , options: options) else { return Result . failure ( Web3Error . transactionSerializationError) }
41
- let result = transaction. call ( options: options)
43
+ guard let transaction = self . registryContract. method ( " resolver " , parameters: [ nameHash as AnyObject ] , options: defaultOptions) else { return Result . failure ( Web3Error . transactionSerializationError) }
44
+ let result = transaction. call ( options: defaultOptions)
42
45
switch result {
43
46
case . success( let res) :
44
47
guard let resolverAddress = res [ " 0 " ] as? EthereumAddress else { return Result . failure ( Web3Error . dataError) }
@@ -48,6 +51,94 @@ public struct ENS {
48
51
}
49
52
}
50
53
54
+ //Returns node's owner address
55
+ mutating func owner( node: String ) -> Result < EthereumAddress , Web3Error > {
56
+ guard let nameHash = NameHash . nameHash ( node) else { return Result . failure ( Web3Error . dataError) }
57
+ guard let transaction = self . registryContract. method ( " owner " , parameters: [ nameHash as AnyObject ] , options: defaultOptions) else { return Result . failure ( Web3Error . transactionSerializationError) }
58
+ let result = transaction. call ( options: defaultOptions)
59
+ switch result {
60
+ case . success( let value) :
61
+ guard let address = value [ " 0 " ] as? EthereumAddress else { return Result . failure ( Web3Error . dataError) }
62
+ return Result ( address)
63
+ case . failure( let error) :
64
+ return Result . failure ( error)
65
+ }
66
+ }
67
+
68
+ //Untested
69
+ mutating func ttl( node: String ) -> Result < BigUInt , Web3Error > {
70
+ guard let nameHash = NameHash . nameHash ( node) else { return Result . failure ( Web3Error . dataError) }
71
+ guard let transaction = self . registryContract. method ( " ttl " , parameters: [ nameHash as AnyObject ] , options: defaultOptions) else { return Result . failure ( Web3Error . transactionSerializationError) }
72
+ let result = transaction. call ( options: defaultOptions)
73
+ switch result {
74
+ case . success( let value) :
75
+ guard let ans = value [ " 0 " ] as? BigUInt else { return Result . failure ( Web3Error . dataError) }
76
+ return Result ( ans)
77
+ case . failure( let error) :
78
+ return Result . failure ( error)
79
+ }
80
+ }
81
+
82
+ // function setOwner(bytes32 node, address owner);
83
+ mutating func setOwner( node: String , owner: EthereumAddress , options: Web3Options , password: String ? = nil ) -> Result < TransactionSendingResult , Web3Error > {
84
+ let options = getOptions ( options)
85
+ guard let nameHash = NameHash . nameHash ( node) else { return Result . failure ( Web3Error . dataError) }
86
+ guard let transaction = self . registryContract. method ( " setOwner " , parameters: [ nameHash, owner] as [ AnyObject ] , options: options) else { return Result . failure ( Web3Error . transactionSerializationError) }
87
+ let result = password == nil ? transaction. send ( ) : transaction. send ( password: password!, options: options)
88
+ switch result {
89
+ case . failure( let error) :
90
+ return Result . failure ( error)
91
+ case . success( let value) :
92
+ return Result ( value)
93
+ }
94
+ }
95
+
96
+ // function setSubnodeOwner(bytes32 node, bytes32 label, address owner);
97
+ mutating func setSubnodeOwner( node: String , label: String , owner: EthereumAddress , options: Web3Options , password: String ? = nil ) -> Result < TransactionSendingResult , Web3Error > {
98
+ let options = getOptions ( options)
99
+ guard let nameHash = NameHash . nameHash ( node) else { return Result . failure ( Web3Error . dataError) }
100
+ guard let transaction = self . registryContract. method ( " setSubnodeOwner " , parameters: [ nameHash, label, owner] as [ AnyObject ] , options: options) else { return Result . failure ( Web3Error . transactionSerializationError) }
101
+ let result = password == nil ? transaction. send ( ) : transaction. send ( password: password!, options: options)
102
+ switch result {
103
+ case . success( let value) :
104
+ return Result ( value)
105
+ case . failure( let error) :
106
+ return Result . failure ( error)
107
+ }
108
+ }
109
+
110
+ // function setResolver(bytes32 node, address resolver);
111
+ mutating func setResolver( node: String , resolver: EthereumAddress , options: Web3Options , password: String ? = nil ) -> Result < TransactionSendingResult , Web3Error > {
112
+ let options = getOptions ( options)
113
+ guard let nameHash = NameHash . nameHash ( node) else { return Result . failure ( Web3Error . dataError) }
114
+ guard let transaction = self . registryContract. method ( " setResolver " , parameters: [ nameHash, resolver] as [ AnyObject ] , options: options) else { return Result . failure ( Web3Error . transactionSerializationError) }
115
+ let result = password == nil ? transaction. send ( ) : transaction. send ( password: password!, options: options)
116
+ switch result {
117
+ case . success( let value) :
118
+ return Result ( value)
119
+ case . failure( let error) :
120
+ return Result . failure ( error)
121
+ }
122
+
123
+ }
124
+
125
+ // function setTTL(bytes32 node, uint64 ttl);
126
+ mutating func setTTL( node: String , ttl: BigUInt , options: Web3Options , password: String ? = nil ) -> Result < TransactionSendingResult , Web3Error > {
127
+ let options = getOptions ( options)
128
+ guard let nameHash = NameHash . nameHash ( node) else { return Result . failure ( Web3Error . dataError) }
129
+ guard let transaction = self . registryContract. method ( " setTTL " , parameters: [ nameHash, ttl] as [ AnyObject ] , options: options) else { return Result . failure ( Web3Error . transactionSerializationError) }
130
+ let result = password == nil ? transaction. send ( ) : transaction. send ( password: password!, options: options)
131
+ switch result {
132
+ case . failure( let error) :
133
+ return Result . failure ( error)
134
+ case . success( let value) :
135
+ return Result ( value)
136
+ }
137
+ }
138
+
139
+ //MARK: - Convenience methods
140
+
141
+
51
142
public mutating func getAddress( _ domain: String ) -> Result < EthereumAddress , Web3Error > {
52
143
let resolver = self . resolver ( forDomain: domain)
53
144
switch resolver {
@@ -56,13 +147,7 @@ public struct ENS {
56
147
switch isAddrSupports{
57
148
case . success( let isSupported) :
58
149
if isSupported {
59
- let result = resolver. addr ( forDomain: domain)
60
- switch result {
61
- case . success( let address) :
62
- return Result ( address)
63
- case . failure( let error) :
64
- return Result . failure ( error)
65
- }
150
+ return resolver. addr ( forDomain: domain)
66
151
} else {
67
152
return Result . failure ( Web3Error . dataError)
68
153
}
@@ -74,148 +159,92 @@ public struct ENS {
74
159
}
75
160
}
76
161
77
- /*
78
- TODO:
79
- */
80
- //
81
- // public func setAddress(domain: String, address: EthereumAddress, options: Web3Options? = nil) {
82
- //
83
- // }
84
- //
85
- // public func getPubkey(domain: String) -> Result<[String: String], Web3Error> {
86
- //
87
- // }
88
- //
89
- // public func setPubkey(domain: String, x: String, y: String, options: Web3Options? = nil) {
90
- //
91
- // }
92
- //
93
- // public func getContent(domain: String) -> Result<String, Web3Error> {
94
- //
95
- // }
96
- //
97
- // public func setContent(domain: String, hash: String, optioins: Web3Options? = nil) {
98
- //
99
- // }
100
- //
101
- // public func getMultihash(domain: String) -> String {
102
- //
103
- // }
104
- }
105
-
106
- public struct ResolverENS {
107
- let web3 : web3
108
- let resolverAddress : EthereumAddress
109
162
110
- public enum InterfaceName {
111
- case addr
112
- case name
113
- case ABI
114
- case pubkey
115
-
116
- func hash( ) -> String {
117
- switch self {
118
- case . ABI:
119
- return " 0x2203ab56 "
120
- case . addr:
121
- return " 0x3b3b57de "
122
- case . name:
123
- return " 0x691f3431 "
124
- case . pubkey:
125
- return " 0xc8690233 "
163
+ public mutating func setAddress( domain: String , address: EthereumAddress , options: Web3Options , password: String ? = nil ) -> Result < TransactionSendingResult , Web3Error > {
164
+ let resolver = self . resolver ( forDomain: domain)
165
+ switch resolver {
166
+ case . success( var resolver) :
167
+ let isSetAddrSupported = resolver. supportsInterface ( interfaceID: ResolverENS . InterfaceName. setAddr. hash ( ) )
168
+ switch isSetAddrSupported {
169
+ case . success( let value) :
170
+ if value {
171
+ return resolver. setAddr ( node: domain, address: address, options: options, password: password)
172
+ } else {
173
+ return Result . failure ( Web3Error . dataError)
174
+ }
175
+ case . failure( let error) :
176
+ return Result . failure ( error)
126
177
}
127
- }
128
- }
129
-
130
- private lazy var resolverContract : web3 . web3contract = {
131
- let contract = self . web3. contract ( Web3 . Utils. resolverABI, at: self . resolverAddress, abiVersion: 2 )
132
- precondition ( contract != nil )
133
- return contract!
134
- } ( )
135
-
136
- init ( web3: web3 , resolverAddress: EthereumAddress ) {
137
- self . web3 = web3
138
- self . resolverAddress = resolverAddress
139
- }
140
-
141
- mutating public func supportsInterface( interfaceID: Data ) -> Result < Bool , Web3Error > {
142
- return supportsInterface ( interfaceID: interfaceID. toHexString ( ) )
143
- }
144
-
145
- //MARK: - returns true if the contract supports given interface
146
- mutating public func supportsInterface( interfaceID: String ) -> Result < Bool , Web3Error > {
147
- let options = Web3Options . defaultOptions ( )
148
- guard let transaction = self . resolverContract. method ( " supportsInterface " , parameters: [ interfaceID as AnyObject ] , options: options) else { return Result . failure ( Web3Error . transactionSerializationError) }
149
- let result = transaction. call ( options: options)
150
- switch result {
151
- case . success( let res) :
152
- guard let supports = res [ " 0 " ] as? Bool else { return Result . failure ( Web3Error . dataError) }
153
- return Result ( supports)
154
178
case . failure( let error) :
155
179
return Result . failure ( error)
156
180
}
157
181
}
158
-
159
- //MARK: - returns address for the given domain at given resolver
160
- mutating public func addr( forDomain domain: String ) -> Result < EthereumAddress , Web3Error > {
161
- guard let nameHash = NameHash . nameHash ( domain) else { return Result . failure ( Web3Error . dataError) }
162
- let options = Web3Options . defaultOptions ( )
163
- guard let transaction = self . resolverContract. method ( " addr " , parameters: [ nameHash as AnyObject ] , options: options) else { return Result . failure ( Web3Error . dataError) }
164
- let result = transaction. call ( options: options)
165
- switch result {
166
- case . success( let res) :
167
- return Result ( res [ " 0 " ] as! EthereumAddress )
182
+
183
+ public mutating func getPubkey( domain: String ) -> Result < Point , Web3Error > {
184
+ let resolver = self . resolver ( forDomain: domain)
185
+ switch resolver {
186
+ case . success( var resolver) :
187
+ let isPubkeySupports = resolver. supportsInterface ( interfaceID: ResolverENS . InterfaceName. pubkey. hash ( ) )
188
+ switch isPubkeySupports {
189
+ case . success( let value) :
190
+ if value {
191
+ return resolver. pubkey ( node: domain)
192
+ } else {
193
+ return Result . failure ( Web3Error . dataError)
194
+ }
195
+ case . failure( let error) :
196
+ return Result . failure ( error)
197
+ }
198
+
168
199
case . failure( let error) :
169
200
return Result . failure ( error)
170
201
}
171
202
}
172
-
173
- //MARK: - returns corresponding ENS to the requested node
174
- mutating public func name( node: String ) -> Result < String , Web3Error > {
175
- let options = Web3Options . defaultOptions ( )
176
- guard let transaction = self . resolverContract. method ( " name " , parameters: [ node. lowercased ( ) as AnyObject ] , options: options) else { return Result . failure ( Web3Error . transactionSerializationError) }
177
- let result = transaction. call ( options: options)
178
- switch result {
179
- case . success( let res) :
180
- return Result ( res [ " 0 " ] as! String )
203
+
204
+ mutating public func setPubkey( domain: String , x: String , y: String , options: Web3Options , password: String ? = nil ) -> Result < TransactionSendingResult , Web3Error > {
205
+ let resolver = self . resolver ( forDomain: domain)
206
+ switch resolver {
207
+ case . success( var value) :
208
+ return value. setPubkey ( node: domain, x: x, y: y, options: options, password: password)
181
209
case . failure( let error) :
182
210
return Result . failure ( error)
183
211
}
184
-
185
212
}
186
213
187
- //MARK: - returns ABI in the requested encodings
188
- mutating public func ABI( node: String , contentType: BigUInt ) -> Result < ( BigUInt , Data ) , Web3Error > {
189
- let options = Web3Options . defaultOptions ( )
190
- guard let transaction = self . resolverContract. method ( " ABI " , parameters: [ node, contentType] as [ AnyObject ] , options: options) else { return Result . failure ( Web3Error . transactionSerializationError) }
191
- let result = transaction. call ( options: options)
192
- switch result {
193
- case . success( let res) :
194
- guard let encoding = res [ " 0 " ] as? BigUInt else { return Result . failure ( Web3Error . dataError) }
195
- guard let data = res [ " 1 " ] as? Data else { return Result . failure ( Web3Error . dataError) }
196
- return Result ( ( encoding, data) )
214
+ mutating public func getContent( domain: String ) -> Result < String , Web3Error > {
215
+ let resolver = self . resolver ( forDomain: domain)
216
+ switch resolver {
217
+ case . success( var value) :
218
+ return value. content ( node: domain)
197
219
case . failure( let error) :
198
220
return Result . failure ( error)
199
221
}
200
222
}
201
223
202
- mutating public func pubkey( node: String ) -> Result < Point , Web3Error > {
203
- let options = Web3Options . defaultOptions ( )
204
- guard let transaction = self . resolverContract. method ( " pubkey " , parameters: [ node as AnyObject ] , options: options) else { return Result . failure ( Web3Error . transactionSerializationError) }
205
- let result = transaction. call ( options: options)
206
- switch result {
207
- case . success( let value) :
208
- guard let x = value [ " x " ] as? String else { return Result . failure ( Web3Error . dataError) }
209
- guard let y = value [ " y " ] as? String else { return Result . failure ( Web3Error . dataError) }
210
- return Result ( Point ( x: x, y: y) )
224
+
225
+ mutating public func setContent( domain: String , hash: String , options: Web3Options , password: String ? = nil ) -> Result < TransactionSendingResult , Web3Error > {
226
+ let resolver = self . resolver ( forDomain: domain)
227
+ switch resolver {
228
+ case . success( var value) :
229
+ return value. setContent ( node: domain, hash: hash, options: options, password: password)
211
230
case . failure( let error) :
212
231
return Result . failure ( error)
213
232
}
214
233
}
215
- }
216
-
217
- public struct Point {
218
- let x : String
219
- let y : String
234
+
235
+ /*
236
+ TODO:
237
+ */
238
+
239
+ //
240
+ // public func getMultihash(domain: String) -> String {
241
+ //
242
+ // }
243
+
244
+ private func getOptions( _ options: Web3Options ) -> Web3Options {
245
+ var options = options
246
+ options. to = self . ensContractAddress
247
+ return options
248
+ }
220
249
}
221
250
0 commit comments