Skip to content

Commit

Permalink
feat(transaction): getSignatures (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyLB authored Oct 16, 2023
1 parent 80047d5 commit d7a193c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Sources/Hedera/ChunkedTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ public class ChunkedTransaction: Transaction {
return super.addSignatureSigner(signer)
}

public final func getAllSignatures() throws -> [[AccountId: [PublicKey: Data]]] {
let sources = try self.makeSources()

self.sources = sources

return try sources.chunks.map(Transaction.getSignaturesAtOffset)
}

public final override func execute(_ client: Client, _ timeout: TimeInterval? = nil) async throws
-> TransactionResponse
{
Expand Down
36 changes: 35 additions & 1 deletion Sources/Hedera/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class Transaction: ValidateChecksums {
}

internal private(set) final var signers: [Signer] = []
internal private(set) final var sources: TransactionSources?
internal final var sources: TransactionSources?
public private(set) final var isFrozen: Bool = false

private final var `operator`: Operator?
Expand Down Expand Up @@ -259,6 +259,40 @@ public class Transaction: ValidateChecksums {
return self
}

public final func getSignatures() throws -> [AccountId: [PublicKey: Data]] {
let sources = try self.makeSources()

self.sources = sources

precondition(sources.chunksCount == 1, "called `getSignatures` on a chunked transaction with chunks")

return try Transaction.getSignaturesAtOffset(chunk: sources.chunks.first!)
}

/// Get the Signatures for this transaction
///
internal static func getSignaturesAtOffset(chunk: SourceChunk) throws -> [AccountId: [PublicKey:
Data]]
{
let signaturesPerNode = try zip(chunk.nodeIds, chunk.signedTransactions).lazy.map { nodeId, tx in
let sigs = try tx.sigMap.sigPair.lazy.map { sigPair in
let key = try PublicKey.fromBytes(sigPair.pubKeyPrefix)
let value: Data
switch sigPair.signature {
case .ed25519(let data),
.ecdsaSecp256K1(let data):
value = data
default: value = Data()
}
return (key, value)
}

return (nodeId, Dictionary(uniqueKeysWithValues: sigs))
}

return Dictionary(uniqueKeysWithValues: signaturesPerNode)
}

internal final func signWithSigner(_ signer: Signer) {
guard !signers.contains(where: { $0.publicKey == signer.publicKey }) else {
return
Expand Down
63 changes: 63 additions & 0 deletions Tests/HederaTests/SignatureTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* ‌
* Hedera Swift SDK
* ​
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
* ​
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ‍
*/

import HederaProtobufs
import SnapshotTesting
import XCTest

@testable import Hedera

internal final class SignatureTests: XCTestCase {
internal static var firstPrivateKey: PrivateKey =
"302e020100300506032b657004220420e40d4241d093b22910c78135e0501b137cd9205bbb9c0153c5adf2c65e7dc95a"
internal static var secondPrivateKey: PrivateKey =
"302e020100300506032b657004220420e40d4241d093b22910c78135e0501b137cd9205bbb9c0153c5adf2c65e7dc85b"

internal func testGetSignatures() throws {
let new = TransferTransaction()

let bodyBytes = try new.maxTransactionFee(10).transactionValidDuration(Duration.seconds(119))
.transactionMemo("Frosted flakes").hbarTransfer(AccountId(2), 2).hbarTransfer(AccountId(101), -2)
.transactionId(Resources.txId).nodeAccountIds(Resources.nodeAccountIds).freeze().sign(Self.firstPrivateKey)
.sign(Self.secondPrivateKey)
.toBytes()

let tx2 = try Transaction.fromBytes(bodyBytes)

XCTAssertEqual(try tx2.getSignatures().count, 2)
}

internal func testGetAllSignaturesFromChunked() throws {
let client = Client.forTestnet()
client.setOperator(0, .generateEd25519())

let transactionId = TransactionId(accountId: 0, validStart: .now)

let tx = try TopicMessageSubmitTransaction()
.topicId(314)
.message("Meep!".data(using: .utf8)!)
.chunkSize(8)
.maxChunks(2)
.transactionId(transactionId)
.freezeWith(client)

XCTAssertEqual(try tx.getAllSignatures().count, 1)
}
}

0 comments on commit d7a193c

Please sign in to comment.