This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from zeriontech/170_remote_signing_classes
#170 — Implement unsigned and manually signed transaction bytes classes
- Loading branch information
Showing
6 changed files
with
257 additions
and
43 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
Example/Tests/TransactionBytes/EthManuallySignedTransactionBytesTests.swift
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,49 @@ | ||
// | ||
// This source file is part of the Web3Swift.io open source project | ||
// Copyright 2018 The Web3Swift Authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// EthManuallySignedTransactionBytesTests.swift | ||
// | ||
// Created by Vadim Koleoshkin on 24/07/2018 | ||
// | ||
|
||
import Nimble | ||
import Quick | ||
@testable import Web3Swift | ||
|
||
class EthManuallySignedTransactionBytesTests: XCTestCase { | ||
|
||
func testSignedTransactionIsEncodedCorrectly() { | ||
expect{ | ||
return try EthManuallyTransactionBytes( | ||
networkID: SimpleInteger( | ||
integer: 1 | ||
), | ||
transactionsCount: EthNumber( | ||
value: 130 | ||
), | ||
gasPrice: EthNumber( | ||
hex: "0x04A817C800" | ||
), | ||
gasEstimate: EthNumber( | ||
value: 21000 | ||
), | ||
recipientAddress: Alice().address(), | ||
weiAmount: EthNumber( | ||
value: 1 | ||
), | ||
contractCall: EmptyBytes(), | ||
r: BytesFromHexString(hex: "4566e88fcc334ce45530cd443177fc943573ef2234188df7e5fb58c93f9f1359"), | ||
s: BytesFromHexString(hex: "13d42bc12afce56c22b16d5208209e20dd3825bb9f4b706d080ac565597063bf"), | ||
v: BytesFromHexString(hex: "26") | ||
).value().toHexString() | ||
|
||
}.to( | ||
equal( | ||
"f86581828504a817c80082520894cd8ac90d9cc7e4c03430d58d2f3e87dae70b807e018026a04566e88fcc334ce45530cd443177fc943573ef2234188df7e5fb58c93f9f1359a013d42bc12afce56c22b16d5208209e20dd3825bb9f4b706d080ac565597063bf" | ||
), | ||
description: "The above transaction with a nonce of 130 signed with Bob's key on external device is expected to be correctly signed" | ||
) | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
Example/Tests/TransactionBytes/EthUnsignedTransactionBytesTests.swift
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,45 @@ | ||
// | ||
// This source file is part of the Web3Swift.io open source project | ||
// Copyright 2018 The Web3Swift Authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// EthUnsignedTransactionBytesTests.swift | ||
// | ||
// Created by Vadim Koleoshkin on 24/07/2018 | ||
// | ||
|
||
import Nimble | ||
import Quick | ||
@testable import Web3Swift | ||
|
||
class EthUnsignedTransactionBytesTests: XCTestCase { | ||
|
||
func testStaticParametersEncodedCorrectly() { | ||
expect{ | ||
return try EthUnsignedTransactionBytes( | ||
networkID: SimpleInteger( | ||
integer: 1 | ||
), | ||
transactionsCount: EthNumber( | ||
value: 128 | ||
), | ||
gasPrice: EthNumber( | ||
hex: "0x04A817C800" | ||
), | ||
gasEstimate: EthNumber( | ||
value: 21000 | ||
), | ||
recipientAddress: Alice().address(), | ||
weiAmount: EthNumber( | ||
hex: "0x01" | ||
), | ||
contractCall: EmptyBytes() | ||
).value().toHexString() | ||
}.to( | ||
equal( | ||
"e581808504a817c80082520894cd8ac90d9cc7e4c03430d58d2f3e87dae70b807e0180018080" | ||
), | ||
description: "Transaction parameters expected to encoded correctly" | ||
) | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
Web3Swift/TransactionBytes/EthManuallySignedTransactionBytes.swift
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,90 @@ | ||
// | ||
// This source file is part of the Web3Swift.io open source project | ||
// Copyright 2018 The Web3Swift Authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// EthManuallySignedTransactionBytes.swift | ||
// | ||
// Created by Vadim Koleoshkin on 24/07/2018 | ||
// | ||
|
||
import CryptoSwift | ||
import Foundation | ||
|
||
/** Remotly signed transaction bytes */ | ||
public final class EthManuallyTransactionBytes: BytesScalar { | ||
|
||
private let networkID: IntegerScalar | ||
private let transactionsCount: BytesScalar | ||
private let gasPrice: BytesScalar | ||
private let gasEstimate: BytesScalar | ||
private let recipientAddress: BytesScalar | ||
private let weiAmount: BytesScalar | ||
private let contractCall: BytesScalar | ||
private let r: BytesScalar | ||
private let s: BytesScalar | ||
private let v: BytesScalar | ||
|
||
/** | ||
Ctor | ||
|
||
- parameters: | ||
- networkID: id of a network where the transaction is to be deployed | ||
- transactionsCount: count of all transactions previously sent by the sender | ||
- gasPrice: gas price in Wei | ||
- gasEstimate: estimate for gas needed for transaction to be mined | ||
- recipientAddress: address of a recipient | ||
- weiAmount: amount to be sent in wei | ||
- contractCall: a bytes representation of the ABI call to the contract | ||
- r: bytes describe R point as defined in ecdsa | ||
- s: bytes describe S point as defined in ecdsa | ||
- v: bytes describe recovery point as defined in ecdsa and EIP-155 | ||
*/ | ||
public init( | ||
networkID: IntegerScalar, | ||
transactionsCount: BytesScalar, | ||
gasPrice: BytesScalar, | ||
gasEstimate: BytesScalar, | ||
recipientAddress: BytesScalar, | ||
weiAmount: BytesScalar, | ||
contractCall: BytesScalar, | ||
r: BytesScalar, | ||
s: BytesScalar, | ||
v: BytesScalar | ||
) { | ||
self.networkID = networkID | ||
self.transactionsCount = transactionsCount | ||
self.gasPrice = gasPrice | ||
self.gasEstimate = gasEstimate | ||
self.recipientAddress = recipientAddress | ||
self.weiAmount = weiAmount | ||
self.contractCall = contractCall | ||
self.r = r | ||
self.s = s | ||
self.v = v | ||
} | ||
|
||
/** | ||
- returns: | ||
signed transaction as `Data` that can be deployed for mining | ||
|
||
- throws: | ||
`DescribedError` if something went wrong | ||
*/ | ||
public func value() throws -> Data { | ||
return try SimpleRLP( | ||
rlps: [ | ||
EthRLP(number: transactionsCount), | ||
EthRLP(number: gasPrice), | ||
EthRLP(number: gasEstimate), | ||
SimpleRLP(bytes: recipientAddress), | ||
EthRLP(number: weiAmount), | ||
SimpleRLP(bytes: contractCall), | ||
SimpleRLP(bytes: v), | ||
SimpleRLP(bytes: r), | ||
SimpleRLP(bytes: s), | ||
] | ||
).value() | ||
} | ||
|
||
} |
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
65 changes: 65 additions & 0 deletions
65
Web3Swift/TransactionBytes/EthUnsignedTransactionBytes.swift
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,65 @@ | ||
// | ||
// This source file is part of the Web3Swift.io open source project | ||
// Copyright 2018 The Web3Swift Authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// EthUnsignedTransactionBytes.swift | ||
// | ||
// Created by Vadim Koleoshkin on 24/07/2018 | ||
// | ||
|
||
import Foundation | ||
|
||
/** Unsigned transaction bytes */ | ||
public final class EthUnsignedTransactionBytes: BytesScalar { | ||
|
||
private let origin: BytesScalar | ||
|
||
/** | ||
Ctor | ||
|
||
- parameters: | ||
- networkID: id of a network where the transaction is to be deployed | ||
- transactionsCount: count of all transactions previously sent by the sender | ||
- gasPrice: gas price in Wei | ||
- gasEstimate: estimate for gas needed for transaction to be mined | ||
- recipientAddress: address of a recipient | ||
- weiAmount: amount to be sent in wei | ||
- contractCall: a bytes representation of the ABI call to the contract | ||
*/ | ||
public init( | ||
networkID: IntegerScalar, | ||
transactionsCount: BytesScalar, | ||
gasPrice: BytesScalar, | ||
gasEstimate: BytesScalar, | ||
recipientAddress: BytesScalar, | ||
weiAmount: BytesScalar, | ||
contractCall: BytesScalar | ||
) { | ||
self.origin = EthManuallyTransactionBytes( | ||
networkID: networkID, | ||
transactionsCount: transactionsCount, | ||
gasPrice: gasPrice, | ||
gasEstimate: gasEstimate, | ||
recipientAddress: recipientAddress, | ||
weiAmount: weiAmount, | ||
contractCall: contractCall, | ||
r: EmptyBytes(), | ||
s: EmptyBytes(), | ||
v: EthNumber(value: networkID) | ||
) | ||
} | ||
|
||
|
||
/** | ||
- returns: | ||
unsigned transaction as `Data` | ||
|
||
- throws: | ||
`DescribedError` if something went wrong | ||
*/ | ||
public func value() throws -> Data { | ||
return try origin.value() | ||
} | ||
|
||
} |