Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update APIs to use MobileCoinRng instead of RngSeed #197

Merged
merged 3 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions Sources/MobileCoinClient+Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,58 +211,51 @@ extension MobileCoinClient {

@available(*, deprecated, message:
"""
Use the new `prepareTransaction(...)` that accepts a 32-byte rngSeed.
Use the `prepareTransaction(...)` that accepts a MobileCoinRng instead of RngSeed

```
public func prepareTransaction(
to recipient: PublicAddress,
memoType: MemoType = .recoverable,
amount: Amount,
fee: UInt64,
rngSeed: RngSeed,
rng: MobileCoinRng,
completion: @escaping (
Result<PendingSinglePayloadTransaction, TransactionPreparationError>
) -> Void
) {
```

this function creates a 32-byte seed by combining the data from 4 calls to rng.next()
""")
public func prepareTransaction(
to recipient: PublicAddress,
memoType: MemoType = .recoverable,
amount: Amount,
fee: UInt64,
rng: MobileCoinRng,
rngSeed: RngSeed,
completion: @escaping (
Result<PendingSinglePayloadTransaction, TransactionPreparationError>
) -> Void
) {
guard let rngSeed = rng.generateRngSeed() else {
completion(.failure(
TransactionPreparationError.invalidInput("Could not create 32 byte RNG seed")))
return
}
prepareTransaction(
to: recipient,
memoType: memoType,
amount: amount,
fee: fee,
rngSeed: rngSeed,
rng: MobileCoinChaCha20Rng(rngSeed: rngSeed),
completion: completion)
}

@available(*, deprecated, message:
"""
Use the new `prepareTransaction(...)` that accepts a 32-byte rngSeed.
Use the `prepareTransaction(...)` that accepts a MobileCoinRng instead of an RngSeed

```
public func prepareTransaction(
to recipient: PublicAddress,
memoType: MemoType = .recoverable,
amount: Amount,
feeLevel: FeeLevel = .minimum,
rngSeed: RngSeed,
rng: MobileCoinRng,
completion: @escaping (
Result<PendingSinglePayloadTransaction, TransactionPreparationError>
) -> Void
Expand All @@ -276,22 +269,17 @@ extension MobileCoinClient {
memoType: MemoType = .recoverable,
amount: Amount,
feeLevel: FeeLevel = .minimum,
rng: MobileCoinRng,
rngSeed: RngSeed,
completion: @escaping (
Result<PendingSinglePayloadTransaction, TransactionPreparationError>
) -> Void
) {
guard let rngSeed = rng.generateRngSeed() else {
completion(.failure(
TransactionPreparationError.invalidInput("Could not create 32-byte RNG seed")))
return
}
prepareTransaction(
to: recipient,
memoType: memoType,
amount: amount,
feeLevel: feeLevel,
rngSeed: rngSeed,
rng: MobileCoinChaCha20Rng(rngSeed: rngSeed),
completion: completion)
}

Expand Down
18 changes: 14 additions & 4 deletions Sources/MobileCoinClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public final class MobileCoinClient {
memoType: memoType,
amount: amount,
fee: fee,
rngSeed: RngSeed(),
rng: MobileCoinChaCha20Rng(),
completion: completion)
}

Expand All @@ -212,11 +212,16 @@ public final class MobileCoinClient {
memoType: MemoType = .recoverable,
amount: Amount,
fee: UInt64,
rngSeed: RngSeed,
rng: MobileCoinRng,
completion: @escaping (
Result<PendingSinglePayloadTransaction, TransactionPreparationError>
) -> Void
) {
guard let rngSeed = rng.generateRngSeed() else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure this is correct, if you only use the RNG to create a seed which you then pass around to see our internal RNG, that defeats the purpose of letting them pass in their own RNG class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android uses the input Rng to seed the RNG for output idempotence. So here, we are doing the same as Android with respect to the output transaction creation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well then, doesnt seem to be as flexible as I initially thought, but if its the same as android thats correct

completion(.failure(
TransactionPreparationError.invalidInput("Could not create 32 byte RNG seed")))
return
}
Account.TransactionOperations(
account: accountLock,
fogMerkleProofService: serviceProvider.fogMerkleProofService,
Expand Down Expand Up @@ -252,7 +257,7 @@ public final class MobileCoinClient {
memoType: memoType,
amount: amount,
feeLevel: feeLevel,
rngSeed: RngSeed(),
rng: MobileCoinChaCha20Rng(),
completion: completion)
}

Expand All @@ -261,11 +266,16 @@ public final class MobileCoinClient {
memoType: MemoType = .recoverable,
amount: Amount,
feeLevel: FeeLevel = .minimum,
rngSeed: RngSeed,
rng: MobileCoinRng,
completion: @escaping (
Result<PendingSinglePayloadTransaction, TransactionPreparationError>
) -> Void
) {
guard let rngSeed = rng.generateRngSeed() else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same feedback here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same answer as above :)

completion(.failure(
TransactionPreparationError.invalidInput("Could not create 32-byte RNG seed")))
return
}
Account.TransactionOperations(
account: accountLock,
fogMerkleProofService: serviceProvider.fogMerkleProofService,
Expand Down
60 changes: 54 additions & 6 deletions Tests/Integration/Transaction/TransactionIdempotenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,62 @@ enum IdempotenceTestError: Error {

class TransactionIdempotenceTests: XCTestCase {

func testIdempotenceSubmissionFailure() throws {
func testAndroidIdempotenceOutputPublicKeyMatch() throws {
let description = "Testing idempotence submission failure"
try testSupportedProtocols(description: description) {
try idempotenceSubmissionFailure(transportProtocol: $0, expectation: $1)
try androidIdempotenceOutputPublicKeyMatch(transportProtocol: $0, expectation: $1)
}
}

func idempotenceSubmissionFailure(
func androidIdempotenceOutputPublicKeyMatch(
transportProtocol: TransportProtocol,
expectation expect: XCTestExpectation
) throws {
let rngSeedHex =
"676f746f2068747470733a2f2f6275792e6d6f62696c65636f696e2e636f6d00"
let recipientAddressHex = """
0a220a2048d9e6aa836d7aa57f7a9da9709603d8f5071faff4908c86ed829e68c0129f63\
12220a207ee52b7741a8b9f3701ab716fa361483b03ddcaeecb64ba64355a3411c87d43d
"""
let androidTxOutputPublicKeyHex =
"d47d4f6525cee846a47106e92de3e63e5b3cb677b8ae4df7efd667e2bad15719"

let client = try IntegrationTestFixtures.createMobileCoinClient(
accountIndex: 1,
using: transportProtocol)
let rngSeed = try XCTUnwrap(RngSeed(try XCTUnwrap(Data(hexEncoded: rngSeedHex))))
let recipientAddressData = try XCTUnwrap(Data(hexEncoded: recipientAddressHex))
let recipient = try XCTUnwrap(PublicAddress(serializedData: recipientAddressData))
let androidTxOutputPublicKey = try XCTUnwrap(Data(hexEncoded: androidTxOutputPublicKeyHex))
let amt = Amount(mob: 100)

client.updateBalances {
guard $0.successOrFulfill(expectation: expect) != nil else { return }
client.prepareTransaction(
to: recipient,
memoType: .unused,
amount: amt,
fee: IntegrationTestFixtures.fee,
rng: MobileCoinChaCha20Rng(rngSeed: rngSeed)
) { result in
guard let payload = result.successOrFulfill(expectation: expect) else {
XCTFail("Invalid payload from prepareTransaction")
return
}
XCTAssertEqual(payload.payloadTxOutContext.txOutPublicKey, androidTxOutputPublicKey)
expect.fulfill()
}
}
}

func testIdempotenceDoubleSubmissionFailure() throws {
let description = "Testing idempotence submission failure"
try testSupportedProtocols(description: description) {
try idempotenceDoubleSubmissionFailure(transportProtocol: $0, expectation: $1)
}
}

func idempotenceDoubleSubmissionFailure(
transportProtocol: TransportProtocol,
expectation expect: XCTestExpectation
) throws {
Expand Down Expand Up @@ -77,7 +125,7 @@ class TransactionIdempotenceTests: XCTestCase {
memoType: .unused,
amount: amt,
fee: IntegrationTestFixtures.fee,
rngSeed: rngSeed
rng: MobileCoinChaCha20Rng(rngSeed: rngSeed)
) { result in
guard let payload = result.successOrFulfill(expectation: expect) else {
return
Expand Down Expand Up @@ -145,7 +193,7 @@ class TransactionIdempotenceTests: XCTestCase {
memoType: .unused,
amount: amt,
fee: IntegrationTestFixtures.fee,
rngSeed: rngSeed
rng: MobileCoinChaCha20Rng(rngSeed: rngSeed)
) { result in
guard let transaction1 = result.successOrFulfill(expectation: expect) else {
return
Expand All @@ -155,7 +203,7 @@ class TransactionIdempotenceTests: XCTestCase {
memoType: .unused,
amount: amt,
fee: IntegrationTestFixtures.fee,
rngSeed: rngSeed
rng: MobileCoinChaCha20Rng(rngSeed: rngSeed)
) {
guard let transaction2 = $0.successOrFulfill(expectation: expect) else {
return
Expand Down