Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added reverse registrar #165

Merged
merged 1 commit into from
Apr 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions web3swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
3A3F59BF225931BE0075C9E7 /* ENSResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = B219DC162154F3EE0035BF94 /* ENSResolver.swift */; };
3A52CCB22264A6D4001C8D7A /* NonceMiddleware.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81ED4EA72190D922003E932E /* NonceMiddleware.swift */; };
3A52CCB32264A6DC001C8D7A /* NonceMiddleware.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81ED4EA72190D922003E932E /* NonceMiddleware.swift */; };
3A52CCB72264B712001C8D7A /* BaseRegistrar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCB62264B712001C8D7A /* BaseRegistrar.swift */; };
3A52CCB82264B712001C8D7A /* BaseRegistrar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCB62264B712001C8D7A /* BaseRegistrar.swift */; };
3A52CCB72264B712001C8D7A /* ENSBaseRegistrar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCB62264B712001C8D7A /* ENSBaseRegistrar.swift */; };
3A52CCB82264B712001C8D7A /* ENSBaseRegistrar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCB62264B712001C8D7A /* ENSBaseRegistrar.swift */; };
3A52CCBA2264B728001C8D7A /* ETHRegistrarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCB92264B728001C8D7A /* ETHRegistrarController.swift */; };
3A52CCBB2264B728001C8D7A /* ETHRegistrarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCB92264B728001C8D7A /* ETHRegistrarController.swift */; };
3A52CCBD226747EB001C8D7A /* PublicKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCBC226747EB001C8D7A /* PublicKey.swift */; };
3A52CCBE226747EB001C8D7A /* PublicKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCBC226747EB001C8D7A /* PublicKey.swift */; };
3A52CCC022675CFC001C8D7A /* ENSRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCBF22675CFC001C8D7A /* ENSRegistry.swift */; };
3A52CCC122675CFC001C8D7A /* ENSRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCBF22675CFC001C8D7A /* ENSRegistry.swift */; };
3AD7D0AD226B5AA900E0F98D /* ENSReverseRegistrar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AD7D0AC226B5AA900E0F98D /* ENSReverseRegistrar.swift */; };
3AD7D0AE226B5AA900E0F98D /* ENSReverseRegistrar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AD7D0AC226B5AA900E0F98D /* ENSReverseRegistrar.swift */; };
3AE8913C2256286C00D08E99 /* Web3+ERC1155.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE8911C2256286B00D08E99 /* Web3+ERC1155.swift */; };
3AE8913D2256286C00D08E99 /* Web3+ERC1155.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE8911C2256286B00D08E99 /* Web3+ERC1155.swift */; };
3AE8913E2256286C00D08E99 /* Web3+ERC1376.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE8911E2256286B00D08E99 /* Web3+ERC1376.swift */; };
Expand Down Expand Up @@ -268,10 +270,11 @@
2B8FEFF3962166E1BEADC886 /* Pods_web3swift_ios.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_web3swift_ios.framework; sourceTree = BUILT_PRODUCTS_DIR; };
342700493511FEB189700D13 /* Pods-web3swift-iOS_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-web3swift-iOS_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-web3swift-iOS_Tests/Pods-web3swift-iOS_Tests.debug.xcconfig"; sourceTree = "<group>"; };
391A0D2EF42488E5C8AB2F71 /* Pods_web3swift_osx_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_web3swift_osx_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3A52CCB62264B712001C8D7A /* BaseRegistrar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseRegistrar.swift; sourceTree = "<group>"; };
3A52CCB62264B712001C8D7A /* ENSBaseRegistrar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ENSBaseRegistrar.swift; sourceTree = "<group>"; };
3A52CCB92264B728001C8D7A /* ETHRegistrarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ETHRegistrarController.swift; sourceTree = "<group>"; };
3A52CCBC226747EB001C8D7A /* PublicKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PublicKey.swift; sourceTree = "<group>"; };
3A52CCBF22675CFC001C8D7A /* ENSRegistry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ENSRegistry.swift; sourceTree = "<group>"; };
3AD7D0AC226B5AA900E0F98D /* ENSReverseRegistrar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ENSReverseRegistrar.swift; sourceTree = "<group>"; };
3AE8911C2256286B00D08E99 /* Web3+ERC1155.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Web3+ERC1155.swift"; sourceTree = "<group>"; };
3AE8911E2256286B00D08E99 /* Web3+ERC1376.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Web3+ERC1376.swift"; sourceTree = "<group>"; };
3AE891202256286B00D08E99 /* Web3+ERC888.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Web3+ERC888.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -537,7 +540,8 @@
3A52CCBC226747EB001C8D7A /* PublicKey.swift */,
B219DC162154F3EE0035BF94 /* ENSResolver.swift */,
3A52CCBF22675CFC001C8D7A /* ENSRegistry.swift */,
3A52CCB62264B712001C8D7A /* BaseRegistrar.swift */,
3AD7D0AC226B5AA900E0F98D /* ENSReverseRegistrar.swift */,
3A52CCB62264B712001C8D7A /* ENSBaseRegistrar.swift */,
3A52CCB92264B728001C8D7A /* ETHRegistrarController.swift */,
);
path = ENS;
Expand Down Expand Up @@ -1193,6 +1197,7 @@
3AE891642256620200D08E99 /* Web3+WebsocketProvider.swift in Sources */,
81195AB020D7FF8500ABC6B1 /* Promise+Web3+Contract+GetIndexedEvents.swift in Sources */,
3A52CCBD226747EB001C8D7A /* PublicKey.swift in Sources */,
3AD7D0AD226B5AA900E0F98D /* ENSReverseRegistrar.swift in Sources */,
81EB1E4B208173D7003BD47F /* Web3+Personal.swift in Sources */,
81909D1821862D5A007D2AE5 /* Web3+Eventloop.swift in Sources */,
81A1824B20D7DF1B0016741F /* Promise+Web3+Personal+UnlockAccount.swift in Sources */,
Expand Down Expand Up @@ -1268,7 +1273,7 @@
81A1822B20D67A1B0016741F /* Promise+Web3+Eth+GetTransactionDetails.swift in Sources */,
815630042007BC8F00A0EC2F /* BIP39+WordLists.swift in Sources */,
8113D2CE1FD7E1590074282C /* EthereumTransaction.swift in Sources */,
3A52CCB72264B712001C8D7A /* BaseRegistrar.swift in Sources */,
3A52CCB72264B712001C8D7A /* ENSBaseRegistrar.swift in Sources */,
3AE8914E2256286C00D08E99 /* Web3+ERC721.swift in Sources */,
81A1821F20D676BC0016741F /* Promise+Web3+Eth+GetTransactionCount.swift in Sources */,
81A1821520D5A2700016741F /* Promise+Web3+Eth+GetBalance.swift in Sources */,
Expand Down Expand Up @@ -1322,6 +1327,7 @@
3AE891652256620200D08E99 /* Web3+WebsocketProvider.swift in Sources */,
81909D1921862D5A007D2AE5 /* Web3+Eventloop.swift in Sources */,
3A52CCBE226747EB001C8D7A /* PublicKey.swift in Sources */,
3AD7D0AE226B5AA900E0F98D /* ENSReverseRegistrar.swift in Sources */,
8125F06A20499AC300A0F2FE /* BloomFilter.swift in Sources */,
81C0FD062044A8D100D82FAF /* TransactionSigner.swift in Sources */,
41948123203630530065A83B /* Web3+JSONRPC.swift in Sources */,
Expand Down Expand Up @@ -1397,7 +1403,7 @@
81909D1321862D17007D2AE5 /* Web3+ReadingTransaction.swift in Sources */,
4194813F203630530065A83B /* Dictionary+Extension.swift in Sources */,
41948140203630530065A83B /* Array+Extension.swift in Sources */,
3A52CCB82264B712001C8D7A /* BaseRegistrar.swift in Sources */,
3A52CCB82264B712001C8D7A /* ENSBaseRegistrar.swift in Sources */,
3AE8914F2256286C00D08E99 /* Web3+ERC721.swift in Sources */,
81A1822020D676BC0016741F /* Promise+Web3+Eth+GetTransactionCount.swift in Sources */,
81A1821620D5A2700016741F /* Promise+Web3+Eth+GetBalance.swift in Sources */,
Expand Down
15 changes: 14 additions & 1 deletion web3swift/Utils/ENS/ENS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ENS {
var resolver: Resolver? = nil
var baseRegistrar: BaseRegistrar? = nil
var registrarController: ETHRegistrarController? = nil
var reverseRegistrar: ReverseRegistrar? = nil

init?(web3: web3) {
self.web3 = web3
Expand Down Expand Up @@ -62,11 +63,23 @@ public class ENS {
self.registrarController = registrarController
}

func setReverseRegistrar(_ reverseRegistrar: ReverseRegistrar) throws {
guard reverseRegistrar.web3.provider.url == self.web3.provider.url else {
throw Web3Error.processingError(desc: "Registrar controller should use same provider as ENS")
}
self.reverseRegistrar = reverseRegistrar
}

func setReverseRegistrar(withAddress address: EthereumAddress) {
let reverseRegistrar = ReverseRegistrar(web3: web3, address: address)
self.reverseRegistrar = reverseRegistrar
}

lazy var defaultOptions: TransactionOptions = {
return TransactionOptions.defaultOptions
}()

//MARK: - Convenience resolver methods
//MARK: - Convenience public resolver methods
public func getAddress(forNode node: String) throws -> EthereumAddress {
guard let resolver = try? self.registry.getResolver(forDomain: node) else {
throw Web3Error.processingError(desc: "Failed to get resolver for domain")
Expand Down
68 changes: 68 additions & 0 deletions web3swift/Utils/ENS/ENSReverseRegistrar.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// ENSReverseRegistrar.swift
// web3swift
//
// Created by Anton on 20/04/2019.
// Copyright © 2019 The Matter Inc. All rights reserved.
//

import Foundation
import BigInt
import EthereumAddress

public extension ENS {
class ReverseRegistrar {
let web3: web3
let address: EthereumAddress

lazy var contract: web3.web3contract = {
let contract = self.web3.contract(Web3.Utils.reverseRegistrarABI, at: self.address, abiVersion: 2)
precondition(contract != nil)
return contract!
}()

lazy var defaultOptions: TransactionOptions = {
return TransactionOptions.defaultOptions
}()

init(web3: web3, address: EthereumAddress) {
self.web3 = web3
self.address = address
}

public func claimAddress(from: EthereumAddress, owner: EthereumAddress) throws -> WriteTransaction {
defaultOptions.from = from
defaultOptions.to = self.address
guard let transaction = self.contract.write("claim", parameters: [owner as AnyObject], extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError}
return transaction
}

public func claimAddressWithResolver(from: EthereumAddress, owner: EthereumAddress, resolver: EthereumAddress) throws -> WriteTransaction {
defaultOptions.from = from
defaultOptions.to = self.address
guard let transaction = self.contract.write("claimWithResolver", parameters: [owner, resolver] as [AnyObject], extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError}
return transaction
}

public func setName(from: EthereumAddress, name: String) throws -> WriteTransaction {
defaultOptions.from = from
defaultOptions.to = self.address
guard let transaction = self.contract.write("setName", parameters: [name] as [AnyObject], extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError}
return transaction
}

public func getReverseRecordName(address: EthereumAddress) throws -> Data {
guard let transaction = self.contract.read("node", parameters: [address] as [AnyObject], extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError}
guard let result = try? transaction.call(transactionOptions: defaultOptions) else {throw Web3Error.processingError(desc: "Can't call transaction")}
guard let name = result["0"] as? Data else {throw Web3Error.processingError(desc: "Can't get answer")}
return name
}

public func getDefaultResolver() throws -> EthereumAddress {
guard let transaction = self.contract.read("defaultResolver", parameters: [] as [AnyObject], extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError}
guard let result = try? transaction.call(transactionOptions: defaultOptions) else {throw Web3Error.processingError(desc: "Can't call transaction")}
guard let address = result["0"] as? EthereumAddress else {throw Web3Error.processingError(desc: "Can't get answer")}
return address
}
}
}
142 changes: 142 additions & 0 deletions web3swift/Web3/Web3+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4742,6 +4742,148 @@ extension Web3.Utils {
"type": "event"
}
]
"""

public static var reverseRegistrarABI = """
[
{
"constant": false,
"inputs": [
{
"name": "owner",
"type": "address"
},
{
"name": "resolver",
"type": "address"
}
],
"name": "claimWithResolver",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "owner",
"type": "address"
}
],
"name": "claim",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "ens",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "ADDR_REVERSE_NODE",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "defaultResolver",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "addr",
"type": "address"
}
],
"name": "node",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "pure",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "name",
"type": "string"
}
],
"name": "setName",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"name": "ensAddr",
"type": "address"
},
{
"name": "resolverAddr",
"type": "address"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
}
]
"""

//function setAddr(bytes32 node, address addr)
Expand Down