Skip to content

Commit

Permalink
Merge pull request #34 from matterinc/develop
Browse files Browse the repository at this point in the history
Fix ethereum address parsing, add readme
  • Loading branch information
shamatar authored Sep 13, 2018
2 parents f1f2e09 + 474d549 commit 94e6e5b
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 41 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,20 @@ Here's a few use cases of our library:
Create keystore and account with password.

```
//TODO
let keystore = try! EthereumKeystoreV3(password: "changeme"); // generates a private key internally if node "privateKey" parameter supplied
let account = keystore!.addresses![0]
print(account)
let data = try! keystore!.serialize() // internally serializes to JSON
print(try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions(rawValue:0)))
let key = try! keystore!.UNSAFE_getPrivateKeyData(password: "changeme", account: account) // you should rarely use this and expose a key manually
```

### Initializing Ethereum address
```
let coldWalletAddress = EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B")
let constractAddress = EthereumAddress("0x45245bc59219eeaaf6cd3f382e078a461ff9de7b")
let constractAddress = EthereumAddress("0x45245bc59219eeaaf6cd3f382e078a461ff9de7b", ignoreChecksum: true)
```
Ethereum addresses are checksum checked if they are not lowercased and always length checked

Ethereum addresses are checksum checked if they are not lowercased or uppercased and always length checked

### Setting options

Expand Down
12 changes: 6 additions & 6 deletions web3swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@
MODULEMAP_PRIVATE_FILE = "";
OTHER_LDFLAGS = "$(inherited)";
PRECOMPS_INCLUDE_HEADERS_FROM_BUILT_PRODUCTS_DIR = YES;
PRODUCT_BUNDLE_IDENTIFIER = "org.bankexfoundation.web3swift-iOS";
PRODUCT_BUNDLE_IDENTIFIER = "io.thematter.web3swift-iOS";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SDKROOT = iphoneos;
SKIP_INSTALL = NO;
Expand Down Expand Up @@ -1537,7 +1537,7 @@
MODULEMAP_PRIVATE_FILE = "";
OTHER_LDFLAGS = "$(inherited)";
PRECOMPS_INCLUDE_HEADERS_FROM_BUILT_PRODUCTS_DIR = YES;
PRODUCT_BUNDLE_IDENTIFIER = "org.bankexfoundation.web3swift-iOS";
PRODUCT_BUNDLE_IDENTIFIER = "io.thematter.web3swift-iOS";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SDKROOT = iphoneos;
SKIP_INSTALL = NO;
Expand Down Expand Up @@ -1613,7 +1613,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.11;
PRODUCT_BUNDLE_IDENTIFIER = "org.bankexfoundation.web3swift-macOS";
PRODUCT_BUNDLE_IDENTIFIER = "io.thematter.web3swift-macOS";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SDKROOT = macosx;
SKIP_INSTALL = YES;
Expand All @@ -1639,7 +1639,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.11;
PRODUCT_BUNDLE_IDENTIFIER = "org.bankexfoundation.web3swift-macOS";
PRODUCT_BUNDLE_IDENTIFIER = "io.thematter.web3swift-macOS";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SDKROOT = macosx;
SKIP_INSTALL = YES;
Expand All @@ -1658,7 +1658,7 @@
DEVELOPMENT_TEAM = 62V9CKQN89;
INFOPLIST_FILE = "web3swift-macOS_Tests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.bankexfoundation.web3swift-macOS-Tests";
PRODUCT_BUNDLE_IDENTIFIER = "io.thematter.web3swift-macOS-Tests";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_VERSION = 4.0;
Expand All @@ -1676,7 +1676,7 @@
DEVELOPMENT_TEAM = 62V9CKQN89;
INFOPLIST_FILE = "web3swift-macOS_Tests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.bankexfoundation.web3swift-macOS-Tests";
PRODUCT_BUNDLE_IDENTIFIER = "io.thematter.web3swift-macOS-Tests";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_VERSION = 4.0;
Expand Down
14 changes: 7 additions & 7 deletions web3swift/HookedFunctions/Classes/Web3+BrowserFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ extension web3.BrowserFunctions {
return addresses[0]
}

public func personalSign(_ personalMessage: String, account: String, password: String = "BANKEXFOUNDATION") -> String? {
public func personalSign(_ personalMessage: String, account: String, password: String = "web3swift") -> String? {
return self.sign(personalMessage, account: account, password: password)
}

public func sign(_ personalMessage: String, account: String, password: String = "BANKEXFOUNDATION") -> String? {
public func sign(_ personalMessage: String, account: String, password: String = "web3swift") -> String? {
guard let data = Data.fromHex(personalMessage) else {return nil}
return self.sign(data, account: account, password: password)
}

public func sign(_ personalMessage: Data, account: String, password: String = "BANKEXFOUNDATION") -> String? {
public func sign(_ personalMessage: Data, account: String, password: String = "web3swift") -> String? {
do {
guard let keystoreManager = self.web3.provider.attachedKeystoreManager else {return nil}

Expand Down Expand Up @@ -74,13 +74,13 @@ extension web3.BrowserFunctions {
}


public func sendTransaction(_ transactionJSON: [String: Any], password: String = "BANKEXFOUNDATION") -> [String:Any]? {
public func sendTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") -> [String:Any]? {
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return nil}
guard let options = Web3Options.fromJSON(transactionJSON) else {return nil}
return self.sendTransaction(transaction, options: options, password: password)
}

public func sendTransaction(_ transaction: EthereumTransaction, options: Web3Options, password: String = "BANKEXFOUNDATION") -> [String:Any]? {
public func sendTransaction(_ transaction: EthereumTransaction, options: Web3Options, password: String = "web3swift") -> [String:Any]? {
let result = self.web3.eth.sendTransaction(transaction, options: options, password: password)
switch result {
case .failure(_):
Expand Down Expand Up @@ -129,13 +129,13 @@ extension web3.BrowserFunctions {
return (transaction, options)
}

public func signTransaction(_ transactionJSON: [String: Any], password: String = "BANKEXFOUNDATION") -> String? {
public func signTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") -> String? {
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return nil}
guard let options = Web3Options.fromJSON(transactionJSON) else {return nil}
return self.signTransaction(transaction, options: options, password: password)
}

public func signTransaction(_ trans: EthereumTransaction, options: Web3Options, password: String = "BANKEXFOUNDATION") -> String? {
public func signTransaction(_ trans: EthereumTransaction, options: Web3Options, password: String = "web3swift") -> String? {
do {
var transaction = trans
guard let from = options.from else {return nil}
Expand Down
6 changes: 3 additions & 3 deletions web3swift/HookedFunctions/Classes/Web3+Wallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension web3.Web3Wallet {
}
}

public func signTX(transaction:inout EthereumTransaction, account: EthereumAddress, password: String = "BANKEXFOUNDATION") -> Result<Bool, Web3Error> {
public func signTX(transaction:inout EthereumTransaction, account: EthereumAddress, password: String = "web3swift") -> Result<Bool, Web3Error> {
do {
guard let keystoreManager = self.web3.provider.attachedKeystoreManager else {
return Result.failure(Web3Error.walletError)
Expand All @@ -50,15 +50,15 @@ extension web3.Web3Wallet {
}
}

public func signPersonalMessage(_ personalMessage: String, account: EthereumAddress, password: String = "BANKEXFOUNDATION") -> Result<Data, Web3Error> {
public func signPersonalMessage(_ personalMessage: String, account: EthereumAddress, password: String = "web3swift") -> Result<Data, Web3Error> {
guard let data = Data.fromHex(personalMessage) else
{
return Result.failure(Web3Error.dataError)
}
return self.signPersonalMessage(data, account: account, password: password)
}

public func signPersonalMessage(_ personalMessage: Data, account: EthereumAddress, password: String = "BANKEXFOUNDATION") -> Result<Data, Web3Error> {
public func signPersonalMessage(_ personalMessage: Data, account: EthereumAddress, password: String = "web3swift") -> Result<Data, Web3Error> {
do {
guard let keystoreManager = self.web3.provider.attachedKeystoreManager else
{
Expand Down
12 changes: 6 additions & 6 deletions web3swift/KeystoreManager/Classes/BIP32Keystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,27 @@ public class BIP32Keystore: AbstractKeystore {
rootPrefix = keystoreParams!.rootPath!
}

public convenience init? (mnemonics: String, password: String = "BANKEXFOUNDATION", mnemonicsPassword: String = "", language: BIP39Language = BIP39Language.english, prefixPath: String = HDNode.defaultPathMetamaskPrefix) throws {
public convenience init? (mnemonics: String, password: String = "web3swift", mnemonicsPassword: String = "", language: BIP39Language = BIP39Language.english, prefixPath: String = HDNode.defaultPathMetamaskPrefix) throws {
guard var seed = BIP39.seedFromMmemonics(mnemonics, password: mnemonicsPassword, language: language) else {throw AbstractKeystoreError.noEntropyError}
defer{ Data.zero(&seed) }
try self.init(seed: seed, password: password, prefixPath: prefixPath)
}

public init? (seed: Data, password: String = "BANKEXFOUNDATION", prefixPath: String = HDNode.defaultPathMetamaskPrefix) throws {
public init? (seed: Data, password: String = "web3swift", prefixPath: String = HDNode.defaultPathMetamaskPrefix) throws {
guard let prefixNode = HDNode(seed: seed)?.derive(path: prefixPath, derivePrivateKey: true) else {return nil}
self.rootPrefix = prefixPath
try createNewAccount(parentNode: prefixNode, password: password)
}

public func createNewChildAccount(password: String = "BANKEXFOUNDATION") throws {
public func createNewChildAccount(password: String = "web3swift") throws {
guard let decryptedRootNode = try? self.getPrefixNodeData(password), decryptedRootNode != nil else {throw AbstractKeystoreError.encryptionError("Failed to decrypt a keystore")}
guard let rootNode = HDNode(decryptedRootNode!) else {throw AbstractKeystoreError.encryptionError("Failed to deserialize a root node")}
let prefixPath = self.rootPrefix
guard rootNode.depth == prefixPath.components(separatedBy: "/").count - 1 else {throw AbstractKeystoreError.encryptionError("Derivation depth mismatch")}
try createNewAccount(parentNode: rootNode, password: password)
}

public func createNewAccount(parentNode: HDNode, password: String = "BANKEXFOUNDATION", aesMode: String = "aes-128-cbc") throws {
public func createNewAccount(parentNode: HDNode, password: String = "web3swift", aesMode: String = "aes-128-cbc") throws {
var newIndex = UInt32(0)
for (p, _) in paths {
guard let idx = UInt32(p.components(separatedBy: "/").last!) else {continue}
Expand All @@ -112,7 +112,7 @@ public class BIP32Keystore: AbstractKeystore {
try encryptDataToStorage(password, data: serializedRootNode, aesMode: aesMode)
}

public func createNewCustomChildAccount(password: String = "BANKEXFOUNDATION", path: String) throws {
public func createNewCustomChildAccount(password: String = "web3swift", path: String) throws {
guard let decryptedRootNode = try? self.getPrefixNodeData(password), decryptedRootNode != nil else {throw AbstractKeystoreError.encryptionError("Failed to decrypt a keystore")}
guard let rootNode = HDNode(decryptedRootNode!) else {throw AbstractKeystoreError.encryptionError("Failed to deserialize a root node")}
let prefixPath = self.rootPrefix
Expand Down Expand Up @@ -268,7 +268,7 @@ public class BIP32Keystore: AbstractKeystore {
return data
}

public func serializeRootNodeToString(password: String = "BANKEXFOUNDATION") throws -> String {
public func serializeRootNodeToString(password: String = "web3swift") throws -> String {
guard let decryptedRootNode = try? self.getPrefixNodeData(password), decryptedRootNode != nil else {throw AbstractKeystoreError.encryptionError("Failed to decrypt a keystore")}
guard let rootNode = HDNode(decryptedRootNode!) else {throw AbstractKeystoreError.encryptionError("Failed to deserialize a root node")}
guard let string = rootNode.serializeToString(serializePublic: false) else {throw AbstractKeystoreError.encryptionError("Failed to deserialize a root node")}
Expand Down
4 changes: 2 additions & 2 deletions web3swift/KeystoreManager/Classes/EthereumKeystoreV3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ public class EthereumKeystoreV3: AbstractKeystore {
}
}

public init? (password: String = "BANKEXFOUNDATION", aesMode: String = "aes-128-cbc") throws {
public init? (password: String = "web3swift", aesMode: String = "aes-128-cbc") throws {
guard var newPrivateKey = SECP256K1.generatePrivateKey() else {return nil}
defer {Data.zero(&newPrivateKey)}
try encryptDataToStorage(password, keyData: newPrivateKey, aesMode: aesMode)
}

public init? (privateKey: Data, password: String = "BANKEXFOUNDATION", aesMode: String = "aes-128-cbc") throws {
public init? (privateKey: Data, password: String = "web3swift", aesMode: String = "aes-128-cbc") throws {
guard privateKey.count == 32 else {return nil}
guard SECP256K1.verifyPrivateKey(privateKey: privateKey) else {return nil}
try encryptDataToStorage(password, keyData: privateKey, aesMode: aesMode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import PromiseKit

extension web3.Eth {

func sendTransactionPromise(_ transaction: EthereumTransaction, options: Web3Options, password:String = "BANKEXFOUNDATION") -> Promise<TransactionSendingResult> {
func sendTransactionPromise(_ transaction: EthereumTransaction, options: Web3Options, password:String = "web3swift") -> Promise<TransactionSendingResult> {
// print(transaction)
var assembledTransaction : EthereumTransaction = transaction.mergedWithOptions(options)
let queue = web3.requestDispatcher.queue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import PromiseKit

extension web3.Personal {

func signPersonalMessagePromise(message: Data, from: EthereumAddress, password:String = "BANKEXFOUNDATION") -> Promise<Data> {
func signPersonalMessagePromise(message: Data, from: EthereumAddress, password:String = "web3swift") -> Promise<Data> {
let queue = web3.requestDispatcher.queue
do {
if self.web3.provider.attachedKeystoreManager == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import BigInt
import PromiseKit

extension web3.Personal {
func unlockAccountPromise(account: EthereumAddress, password:String = "BANKEXFOUNDATION", seconds: UInt64 = 300) -> Promise<Bool> {
func unlockAccountPromise(account: EthereumAddress, password:String = "web3swift", seconds: UInt64 = 300) -> Promise<Bool> {
let addr = account.address
return unlockAccountPromise(account: addr, password: password, seconds: seconds)
}


func unlockAccountPromise(account: String, password:String = "BANKEXFOUNDATION", seconds: UInt64 = 300) -> Promise<Bool> {
func unlockAccountPromise(account: String, password:String = "web3swift", seconds: UInt64 = 300) -> Promise<Bool> {
let queue = web3.requestDispatcher.queue
do {
if self.web3.provider.attachedKeystoreManager == nil {
Expand Down
2 changes: 1 addition & 1 deletion web3swift/Web3/Classes/Web3+Eth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension web3.Eth {
/// This function is synchronous!
///
/// Returns the Result object that indicates either success of failure.
public func sendTransaction(_ transaction: EthereumTransaction, options: Web3Options, password:String = "BANKEXFOUNDATION") -> Result<TransactionSendingResult, Web3Error> {
public func sendTransaction(_ transaction: EthereumTransaction, options: Web3Options, password:String = "web3swift") -> Result<TransactionSendingResult, Web3Error> {
do {
let result = try self.sendTransactionPromise(transaction, options: options, password: password).wait()
return Result(result)
Expand Down
4 changes: 2 additions & 2 deletions web3swift/Web3/Classes/Web3+Personal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension web3.Personal {
- important: This call is synchronous
*/
public func signPersonalMessage(message: Data, from: EthereumAddress, password:String = "BANKEXFOUNDATION") -> Result<Data, Web3Error> {
public func signPersonalMessage(message: Data, from: EthereumAddress, password:String = "web3swift") -> Result<Data, Web3Error> {
do {
let result = try self.signPersonalMessagePromise(message: message, from: from, password: password).wait()
return Result(result)
Expand All @@ -49,7 +49,7 @@ extension web3.Personal {
- important: This call is synchronous. Does nothing if private keys are stored locally.
*/
public func unlockAccount(account: EthereumAddress, password:String = "BANKEXFOUNDATION", seconds: UInt64 = 300) -> Result<Bool, Web3Error> {
public func unlockAccount(account: EthereumAddress, password:String = "web3swift", seconds: UInt64 = 300) -> Result<Bool, Web3Error> {
do {
let result = try self.unlockAccountPromise(account: account).wait()
return Result(result)
Expand Down
4 changes: 2 additions & 2 deletions web3swift/Web3/Classes/Web3+TransactionIntermediate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension web3.web3contract {
- important: This call is synchronous
*/
public func send(password: String = "BANKEXFOUNDATION", options: Web3Options? = nil, onBlock: String = "pending") -> Result<TransactionSendingResult, Web3Error> {
public func send(password: String = "web3swift", options: Web3Options? = nil, onBlock: String = "pending") -> Result<TransactionSendingResult, Web3Error> {
do {
let result = try self.sendPromise(password: password, options: options, onBlock: onBlock).wait()
return Result(result)
Expand Down Expand Up @@ -199,7 +199,7 @@ extension web3.web3contract.TransactionIntermediate {
return returnPromise
}

public func sendPromise(password:String = "BANKEXFOUNDATION", options: Web3Options? = nil, onBlock: String = "pending") -> Promise<TransactionSendingResult>{
public func sendPromise(password:String = "web3swift", options: Web3Options? = nil, onBlock: String = "pending") -> Promise<TransactionSendingResult>{
let queue = self.web3.requestDispatcher.queue
return self.assemblePromise(options: options, onBlock: onBlock).then(on: queue) { transaction throws -> Promise<TransactionSendingResult> in
guard let mergedOptions = Web3Options.merge(self.options, with: options) else {
Expand Down
Loading

0 comments on commit 94e6e5b

Please sign in to comment.