-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#19 fix integration test - add example app improvements
- Loading branch information
Showing
28 changed files
with
417 additions
and
262 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
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,17 @@ | ||
|
||
import Foundation | ||
import Web3 | ||
|
||
extension EthereumTransaction { | ||
var description: String { | ||
return """ | ||
from: \(String(describing: from!.hex(eip55: true))) | ||
to: \(String(describing: to!.hex(eip55: true))), | ||
value: \(String(describing: value!.hex())), | ||
gasPrice: \(String(describing: gasPrice?.hex())), | ||
gas: \(String(describing: gas?.hex())), | ||
data: \(data.hex()), | ||
nonce: \(String(describing: nonce?.hex())) | ||
""" | ||
} | ||
} |
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,48 @@ | ||
import Web3 | ||
import Foundation | ||
import WalletConnectUtils | ||
import WalletConnect | ||
import CryptoSwift | ||
|
||
class Signer { | ||
static let privateKey: EthereumPrivateKey = try! EthereumPrivateKey(hexPrivateKey: "0xe56da0e170b5e09a8bb8f1b693392c7d56c3739a9c75740fbc558a2877868540") | ||
private init(){} | ||
static func signEth(request: Request) -> AnyCodable { | ||
let method = request.method | ||
if method == "personal_sign" { | ||
let params = try! request.params.get([String].self) | ||
let messageToSign = params[0] | ||
let signHash = signHash(messageToSign) | ||
let (v, r, s) = try! self.privateKey.sign(hash: signHash) | ||
let result = "0x" + r.toHexString() + s.toHexString() + String(v + 27, radix: 16) | ||
return AnyCodable(result) | ||
} else if method == "eth_signTypedData" { | ||
let params = try! request.params.get([String].self) | ||
print(params) | ||
let messageToSign = params[1] | ||
let signHash = signHash(messageToSign) | ||
let (v, r, s) = try! self.privateKey.sign(hash: signHash) | ||
let result = "0x" + r.toHexString() + s.toHexString() + String(v + 27, radix: 16) | ||
return AnyCodable(result) | ||
} else if method == "eth_sendTransaction" { | ||
let params = try! request.params.get([EthereumTransaction].self) | ||
var transaction = params[0] | ||
transaction.gas = EthereumQuantity(quantity: BigUInt("1234")) | ||
print(transaction.description) | ||
let signedTx = try! transaction.sign(with: self.privateKey, chainId: 4) | ||
let (r, s, v) = (signedTx.r, signedTx.s, signedTx.v) | ||
let result = r.hex() + s.hex().dropFirst(2) + String(v.quantity, radix: 16) | ||
return AnyCodable(result) | ||
} | ||
fatalError("not implemented") | ||
} | ||
|
||
private static func signHash(_ message: String) -> Bytes { | ||
let prefix = "\u{19}Ethereum Signed Message:\n" | ||
let messageData = Data(hex: message) | ||
let prefixData = (prefix + String(messageData.count)).data(using: .utf8)! | ||
let prefixedMessageData = prefixData + messageData | ||
let dataToHash: Bytes = .init(hex: prefixedMessageData.toHexString()) | ||
return SHA3(variant: .keccak256).calculate(for: dataToHash) | ||
} | ||
} |
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
Oops, something went wrong.