This repository has been archived by the owner on May 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Add transaction signing #7
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright © 2017-2018 Trust. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
import BigInt | ||
|
||
protocol Signer { | ||
func hash(transaction: Transaction) -> Data | ||
func values(transaction: Transaction, signature: Data) -> (r: BigInt, s: BigInt, v: BigInt) | ||
} | ||
|
||
struct EIP155Signer: Signer { | ||
let chainID: BigInt | ||
|
||
init(chainID: BigInt) { | ||
self.chainID = chainID | ||
} | ||
|
||
func hash(transaction: Transaction) -> Data { | ||
return rlpHash([ | ||
transaction.nonce, | ||
transaction.gasPrice, | ||
transaction.gasLimit, | ||
transaction.to.data, | ||
transaction.amount, | ||
transaction.payload ?? Data(), | ||
chainID, 0, 0, | ||
] as [Any])! | ||
} | ||
|
||
func values(transaction: Transaction, signature: Data) -> (r: BigInt, s: BigInt, v: BigInt) { | ||
let (r, s, v) = HomesteadSigner().values(transaction: transaction, signature: signature) | ||
let newV: BigInt | ||
if chainID != 0 { | ||
newV = BigInt(signature[64]) + 35 + chainID + chainID | ||
} else { | ||
newV = v | ||
} | ||
return (r, s, newV) | ||
} | ||
} | ||
|
||
struct HomesteadSigner: Signer { | ||
func hash(transaction: Transaction) -> Data { | ||
return rlpHash([ | ||
transaction.nonce, | ||
transaction.gasPrice, | ||
transaction.gasLimit, | ||
transaction.to.data, | ||
transaction.amount, | ||
transaction.payload ?? Data(), | ||
])! | ||
} | ||
|
||
func values(transaction: Transaction, signature: Data) -> (r: BigInt, s: BigInt, v: BigInt) { | ||
precondition(signature.count == 65, "Wrong size for signature") | ||
let r = BigInt(sign: .plus, magnitude: BigUInt(Data(signature[..<32]))) | ||
let s = BigInt(sign: .plus, magnitude: BigUInt(Data(signature[32..<64]))) | ||
let v = BigInt(sign: .plus, magnitude: BigUInt(Data(bytes: [signature[64] + 27]))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember there was something where we wanted to make |
||
return (r, s, v) | ||
} | ||
} | ||
|
||
func rlpHash(_ element: Any) -> Data? { | ||
guard let data = RLP.encode(element) else { | ||
return nil | ||
} | ||
return EthereumCrypto.hash(data) | ||
} |
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,50 @@ | ||
// Copyright © 2017-2018 Trust. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
import BigInt | ||
import TrustCore | ||
import XCTest | ||
|
||
class TransactionSigningTests: XCTestCase { | ||
func testEIP155SignHash() { | ||
let address = Address(string: "0x3535353535353535353535353535353535353535")! | ||
var transaction = Transaction(gasPrice: 20000000000, gasLimit: 21000, to: address) | ||
transaction.nonce = 9 | ||
transaction.amount = BigInt("1000000000000000000") | ||
|
||
transaction.sign { hash in | ||
XCTAssertEqual(hash.hexString, "daf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53") | ||
return Data(repeating: 0, count: 65) | ||
} | ||
} | ||
|
||
func testHomesteadSignHash() { | ||
let address = Address(string: "0x3535353535353535353535353535353535353535")! | ||
var transaction = Transaction(gasPrice: 20000000000, gasLimit: 21000, to: address) | ||
transaction.nonce = 9 | ||
transaction.amount = BigInt("1000000000000000000") | ||
|
||
transaction.sign(chainID: 0) { hash in | ||
XCTAssertEqual(hash.hexString, "f9e36c28c8cb35adba138005c02ab7aa7fbcd891f3139cb2eeed052a51cd2713") | ||
return Data(repeating: 0, count: 65) | ||
} | ||
} | ||
|
||
func testSignTransaction() { | ||
var transaction = Transaction(gasPrice: 20000000000, gasLimit: 21000, to: Address(string: "0x3535353535353535353535353535353535353535")!) | ||
transaction.nonce = 9 | ||
transaction.amount = BigInt("1000000000000000000") | ||
|
||
transaction.sign { hash in | ||
XCTAssertEqual(hash.hexString, "daf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53") | ||
return Data(hexString: "28ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa63627667cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d8300")! | ||
} | ||
|
||
XCTAssertEqual(transaction.v, BigInt(37)) | ||
XCTAssertEqual(transaction.r, BigInt("18515461264373351373200002665853028612451056578545711640558177340181847433846")) | ||
XCTAssertEqual(transaction.s, BigInt("46948507304638947509940763649030358759909902576025900602547168820602576006531")) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I would ditch
HomesteadSigner
at all. everyone useEIP155
for this stuff.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, this is based on geth. I don't think it's harmful to have.