blockchainethereumblockchain-walleterc20erc721walletxdaidappdecentralizederc1155erc875iosswifttokens
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
50 lines
1.2 KiB
7 years ago
|
// Copyright SIX DAY LLC. All rights reserved.
|
||
|
|
||
|
import BigInt
|
||
|
import CryptoSwift
|
||
|
|
||
|
protocol Signer {
|
||
|
func hash(transaction: SignTransaction) -> Data
|
||
|
}
|
||
|
|
||
|
struct EIP155Signer: Signer {
|
||
|
let chainId: BigInt
|
||
|
|
||
|
init(chainId: BigInt) {
|
||
|
self.chainId = chainId
|
||
|
}
|
||
|
|
||
|
func hash(transaction: SignTransaction) -> Data {
|
||
|
return rlpHash([
|
||
|
transaction.nonce,
|
||
|
transaction.gasPrice,
|
||
|
transaction.gasLimit,
|
||
|
Data(hex: transaction.address.address),
|
||
|
transaction.value,
|
||
|
transaction.data,
|
||
|
transaction.chainID, 0, 0,
|
||
|
])!
|
||
|
}
|
||
|
}
|
||
|
|
||
|
struct HomesteadSigner: Signer {
|
||
|
func hash(transaction: SignTransaction) -> Data {
|
||
|
return rlpHash([
|
||
|
transaction.nonce,
|
||
|
transaction.gasPrice,
|
||
|
transaction.gasLimit,
|
||
|
Data(hex: transaction.address.address),
|
||
|
transaction.value,
|
||
|
transaction.data,
|
||
|
])!
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func rlpHash(_ element: Any) -> Data? {
|
||
|
let sha3 = SHA3(variant: .keccak256)
|
||
|
guard let data = RLP.encode(element) else {
|
||
|
return nil
|
||
|
}
|
||
|
return Data(bytes: sha3.calculate(for: data.bytes))
|
||
|
}
|