PCZT methods

- also attempt to fix _uuid in views
This commit is contained in:
Lukas Korba 2024-12-10 14:39:39 +01:00
parent 08e5a73ff0
commit 6bab337568
7 changed files with 243 additions and 6 deletions

View File

@ -85,7 +85,7 @@ public enum ZcashTransaction {
public let rawID: Data
public let pool: Pool
public let index: Int
public let fromAccount: AccountId?
public let fromAccount: AccountUUID?
public let recipient: TransactionRecipient
public let value: Zatoshi
public let isChange: Bool
@ -105,8 +105,8 @@ extension ZcashTransaction.Output {
static let rawID = SQLite.Expression<Blob>("txid")
static let pool = SQLite.Expression<Int>("output_pool")
static let index = SQLite.Expression<Int>("output_index")
static let toAccount = SQLite.Expression<Int?>("to_account_id")
static let fromAccount = SQLite.Expression<Int?>("from_account_id")
static let toAccount = SQLite.Expression<Blob?>("to_account_uuid")
static let fromAccount = SQLite.Expression<Blob?>("from_account_uuid")
static let toAddress = SQLite.Expression<String?>("to_address")
static let value = SQLite.Expression<Int64>("value")
static let isChange = SQLite.Expression<Bool>("is_change")
@ -119,7 +119,7 @@ extension ZcashTransaction.Output {
pool = .init(rawValue: try row.get(Column.pool))
index = try row.get(Column.index)
if let accountId = try row.get(Column.fromAccount) {
fromAccount = AccountId(accountId)
fromAccount = AccountUUID(id: [UInt8](Data(blob: accountId)))
} else {
fromAccount = nil
}
@ -132,7 +132,7 @@ extension ZcashTransaction.Output {
{
recipient = TransactionRecipient.address(try Recipient(outputRecipient, network: metadata.networkType))
} else if let toAccount = try row.get(Column.toAccount) {
recipient = .internalAccount(UInt32(toAccount))
recipient = .internalAccount(AccountUUID(id: [UInt8](Data(blob: toAccount))))
} else {
throw ZcashError.zcashTransactionOutputInconsistentRecipient
}

View File

@ -251,7 +251,7 @@ public struct TexAddress: Equatable, StringEncoded, Comparable {
public enum TransactionRecipient: Equatable {
case address(Recipient)
case internalAccount(UInt32)
case internalAccount(AccountUUID)
}
/// Represents a valid recipient of Zcash

View File

@ -291,6 +291,104 @@ struct ZcashRustBackend: ZcashRustBackendWelding {
count: Int(proposal.pointee.len)
))
}
@DBActor
func createPCZTFromProposal(
ufvk: UnifiedFullViewingKey,
accountUUID: AccountUUID,
proposal: FfiProposal
) async throws -> FfiProposal {
let proposalBytes = try proposal.serializedData(partial: false).bytes
let pcztPtr = proposalBytes.withUnsafeBufferPointer { proposalPtr in
zcashlc_create_pczt_from_proposal(
dbData.0,
dbData.1,
networkType.networkId,
proposalPtr.baseAddress,
UInt(proposalBytes.count),
[CChar](ufvk.encoding.utf8CString)
)
}
guard let pcztPtr else {
throw ZcashError.rustCreateToAddress(lastErrorMessage(fallback: "`createPCZTFromProposal` failed with unknown error"))
}
defer { zcashlc_free_boxed_slice(pcztPtr) }
return try FfiProposal(serializedBytes: Data(
bytes: pcztPtr.pointee.ptr,
count: Int(pcztPtr.pointee.len)
))
}
@DBActor
func addProofsToPCZT(
pczt: FfiProposal
) async throws -> FfiProposal {
let pcztBytes = try pczt.serializedData(partial: false).bytes
let pcztPtr = pcztBytes.withUnsafeBufferPointer { pcztPtr in
zcashlc_add_proofs_to_pczt(
pcztPtr.baseAddress,
UInt(pcztPtr.count),
spendParamsPath.0,
spendParamsPath.1,
outputParamsPath.0,
outputParamsPath.1
)
}
guard let pcztPtr else {
throw ZcashError.rustCreateToAddress(lastErrorMessage(fallback: "`addProofsToPCZT` failed with unknown error"))
}
defer { zcashlc_free_boxed_slice(pcztPtr) }
return try FfiProposal(serializedBytes: Data(
bytes: pcztPtr.pointee.ptr,
count: Int(pcztPtr.pointee.len)
))
}
@DBActor
func extractAndStoreTxFromPCZT(
pcztWithProofs: FfiProposal,
pcztWithSigs: FfiProposal
) async throws -> FfiProposal {
let pcztWithProofsBytes = try pcztWithProofs.serializedData(partial: false).bytes
let pcztWithSigsBytes = try pcztWithSigs.serializedData(partial: false).bytes
let pcztPtr = pcztWithProofsBytes.withUnsafeBufferPointer { pcztWithProofsBytesPtr in
pcztWithSigsBytes.withUnsafeBufferPointer { pcztWithSigsBytesPtr in
zcashlc_extract_and_store_from_pczt(
dbData.0,
dbData.1,
networkType.networkId,
pcztWithProofsBytesPtr.baseAddress,
UInt(pcztWithProofsBytesPtr.count),
pcztWithSigsBytesPtr.baseAddress,
UInt(pcztWithSigsBytesPtr.count),
spendParamsPath.0,
spendParamsPath.1,
outputParamsPath.0,
outputParamsPath.1
)
}
}
guard let pcztPtr else {
throw ZcashError.rustCreateToAddress(lastErrorMessage(fallback: "`addProofsToPCZT` failed with unknown error"))
}
defer { zcashlc_free_boxed_slice(pcztPtr) }
return try FfiProposal(serializedBytes: Data(
bytes: pcztPtr.pointee.ptr,
count: Int(pcztPtr.pointee.len)
))
}
@DBActor
func decryptAndStoreTransaction(txBytes: [UInt8], minedHeight: UInt32?) async throws {

View File

@ -283,6 +283,22 @@ protocol ZcashRustBackendWelding {
usk: UnifiedSpendingKey
) async throws -> [Data]
/// PCZT logic
func createPCZTFromProposal(
ufvk: UnifiedFullViewingKey,
accountUUID: AccountUUID,
proposal: FfiProposal
) async throws -> FfiProposal
func addProofsToPCZT(
pczt: FfiProposal
) async throws -> FfiProposal
func extractAndStoreTxFromPCZT(
pcztWithProofs: FfiProposal,
pcztWithSigs: FfiProposal
) async throws -> FfiProposal
/// Gets the consensus branch id for the given height
/// - Parameter height: the height you what to know the branch id for
/// - Throws: `rustNoConsensusBranchId` if rust layer returns error.

View File

@ -227,6 +227,21 @@ public protocol Synchronizer: AnyObject {
accountUUID: AccountUUID
) async throws -> Proposal
func createPCZTFromProposal(
ufvk: UnifiedFullViewingKey,
accountUUID: AccountUUID,
proposal: Proposal
) async throws -> Proposal
func addProofsToPCZT(
pczt: Proposal
) async throws -> Proposal
func extractAndStoreTxFromPCZT(
pcztWithProofs: Proposal,
pcztWithSigs: Proposal
) async throws -> Proposal
/// all the transactions that are on the blockchain
var transactions: [ZcashTransaction.Overview] { get async }

View File

@ -400,6 +400,42 @@ public class SDKSynchronizer: Synchronizer {
}
}
}
public func createPCZTFromProposal(
ufvk: UnifiedFullViewingKey,
accountUUID: AccountUUID,
proposal: Proposal
) async throws -> Proposal {
try await Proposal(
inner: initializer.rustBackend.createPCZTFromProposal(
ufvk: ufvk,
accountUUID: accountUUID,
proposal: proposal.inner
)
)
}
public func addProofsToPCZT(
pczt: Proposal
) async throws -> Proposal {
try await Proposal(
inner: initializer.rustBackend.addProofsToPCZT(
pczt: pczt.inner
)
)
}
public func extractAndStoreTxFromPCZT(
pcztWithProofs: Proposal,
pcztWithSigs: Proposal
) async throws -> Proposal {
try await Proposal(
inner: initializer.rustBackend.extractAndStoreTxFromPCZT(
pcztWithProofs: pcztWithProofs.inner,
pcztWithSigs: pcztWithSigs.inner
)
)
}
public func allReceivedTransactions() async throws -> [ZcashTransaction.Overview] {
try await transactionRepository.findReceived(offset: 0, limit: Int.max)

View File

@ -3022,6 +3022,78 @@ class ZcashRustBackendWeldingMock: ZcashRustBackendWelding {
}
}
// MARK: - createPCZTFromProposal
var createPCZTFromProposalUfvkAccountUUIDProposalThrowableError: Error?
var createPCZTFromProposalUfvkAccountUUIDProposalCallsCount = 0
var createPCZTFromProposalUfvkAccountUUIDProposalCalled: Bool {
return createPCZTFromProposalUfvkAccountUUIDProposalCallsCount > 0
}
var createPCZTFromProposalUfvkAccountUUIDProposalReceivedArguments: (ufvk: UnifiedFullViewingKey, accountUUID: AccountUUID, proposal: FfiProposal)?
var createPCZTFromProposalUfvkAccountUUIDProposalReturnValue: FfiProposal!
var createPCZTFromProposalUfvkAccountUUIDProposalClosure: ((UnifiedFullViewingKey, AccountUUID, FfiProposal) async throws -> FfiProposal)?
func createPCZTFromProposal(ufvk: UnifiedFullViewingKey, accountUUID: AccountUUID, proposal: FfiProposal) async throws -> FfiProposal {
if let error = createPCZTFromProposalUfvkAccountUUIDProposalThrowableError {
throw error
}
createPCZTFromProposalUfvkAccountUUIDProposalCallsCount += 1
createPCZTFromProposalUfvkAccountUUIDProposalReceivedArguments = (ufvk: ufvk, accountUUID: accountUUID, proposal: proposal)
if let closure = createPCZTFromProposalUfvkAccountUUIDProposalClosure {
return try await closure(ufvk, accountUUID, proposal)
} else {
return createPCZTFromProposalUfvkAccountUUIDProposalReturnValue
}
}
// MARK: - addProofsToPCZT
var addProofsToPCZTPcztThrowableError: Error?
var addProofsToPCZTPcztCallsCount = 0
var addProofsToPCZTPcztCalled: Bool {
return addProofsToPCZTPcztCallsCount > 0
}
var addProofsToPCZTPcztReceivedPczt: FfiProposal?
var addProofsToPCZTPcztReturnValue: FfiProposal!
var addProofsToPCZTPcztClosure: ((FfiProposal) async throws -> FfiProposal)?
func addProofsToPCZT(pczt: FfiProposal) async throws -> FfiProposal {
if let error = addProofsToPCZTPcztThrowableError {
throw error
}
addProofsToPCZTPcztCallsCount += 1
addProofsToPCZTPcztReceivedPczt = pczt
if let closure = addProofsToPCZTPcztClosure {
return try await closure(pczt)
} else {
return addProofsToPCZTPcztReturnValue
}
}
// MARK: - extractAndStoreTxFromPCZT
var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsThrowableError: Error?
var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsCallsCount = 0
var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsCalled: Bool {
return extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsCallsCount > 0
}
var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsReceivedArguments: (pcztWithProofs: FfiProposal, pcztWithSigs: FfiProposal)?
var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsReturnValue: FfiProposal!
var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsClosure: ((FfiProposal, FfiProposal) async throws -> FfiProposal)?
func extractAndStoreTxFromPCZT(pcztWithProofs: FfiProposal, pcztWithSigs: FfiProposal) async throws -> FfiProposal {
if let error = extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsThrowableError {
throw error
}
extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsCallsCount += 1
extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsReceivedArguments = (pcztWithProofs: pcztWithProofs, pcztWithSigs: pcztWithSigs)
if let closure = extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsClosure {
return try await closure(pcztWithProofs, pcztWithSigs)
} else {
return extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsReturnValue
}
}
// MARK: - consensusBranchIdFor
var consensusBranchIdForHeightThrowableError: Error?