make Memo and MemoBytes parameters nullable so they can be omitted when sending to transparent receivers.

update libzcashlc
This commit is contained in:
Francisco Gindre 2022-10-24 09:38:47 -03:00
parent 7403bcc809
commit 5de1b50b29
12 changed files with 47 additions and 38 deletions

View File

@ -86,7 +86,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/zcash-hackworks/zcash-light-client-ffi",
"state" : {
"revision" : "0059f090e655667f9ee5ed3306bd87ca78c7711a"
"revision" : "57c2de3825aab0ab8b5f4100dc0184e52ca86ba1"
}
}
],

View File

@ -16,7 +16,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.8.0"),
.package(url: "https://github.com/stephencelis/SQLite.swift.git", from: "0.13.0"),
.package(name:"libzcashlc", url: "https://github.com/zcash-hackworks/zcash-light-client-ffi", revision: "0059f090e655667f9ee5ed3306bd87ca78c7711a")
.package(name:"libzcashlc", url: "https://github.com/zcash-hackworks/zcash-light-client-ffi", revision: "57c2de3825aab0ab8b5f4100dc0184e52ca86ba1")
],
targets: [
.target(

View File

@ -61,7 +61,7 @@ struct PendingTransaction: PendingTransactionEntity, Decodable, Encodable {
raw: entity.raw,
id: entity.id,
value: entity.value,
memo: entity.memo == nil ? Data(MemoBytes.empty().bytes) : entity.memo,
memo: entity.memo,
rawTransactionId: entity.raw,
fee: entity.fee
)
@ -179,7 +179,13 @@ struct PendingTransaction: PendingTransactionEntity, Decodable, Encodable {
}
extension PendingTransaction {
init(value: Zatoshi, recipient: PendingTransactionRecipient, memo: MemoBytes, account index: Int) {
init(value: Zatoshi, recipient: PendingTransactionRecipient, memo: MemoBytes?, account index: Int) {
var memoData: Data?
if let memo = memo {
memoData = Data(memo.bytes)
}
self = PendingTransaction(
recipient: recipient,
accountIndex: index,
@ -194,7 +200,7 @@ extension PendingTransaction {
raw: nil,
id: nil,
value: value,
memo: Data(memo.bytes),
memo: memoData,
rawTransactionId: nil,
fee: nil
)

View File

@ -225,13 +225,8 @@ extension Optional where WrappedType == String {
}
}
extension Optional where WrappedType == Data {
func intoMemoBytes() throws -> MemoBytes {
switch self {
case .none:
return .empty()
case .some(let data):
return try .init(bytes: data.bytes)
}
extension Data {
func intoMemoBytes() throws -> MemoBytes? {
try .init(bytes: self.bytes)
}
}

View File

@ -10,7 +10,6 @@ import Foundation
import libzcashlc
class ZcashRustBackend: ZcashRustBackendWelding {
static let minimumConfirmations: UInt32 = 10
static func createAccount(dbData: URL, seed: [UInt8], networkType: NetworkType) throws -> UnifiedSpendingKey {
@ -37,14 +36,14 @@ class ZcashRustBackend: ZcashRustBackendWelding {
usk: UnifiedSpendingKey,
to address: String,
value: Int64,
memo: MemoBytes,
memo: MemoBytes?,
spendParamsPath: String,
outputParamsPath: String,
networkType: NetworkType
) -> Int64 {
let dbData = dbData.osStr()
return usk.bytes.withUnsafeBufferPointer{ uskPtr in
return usk.bytes.withUnsafeBufferPointer { uskPtr in
zcashlc_create_to_address(
dbData.0,
dbData.1,
@ -52,7 +51,7 @@ class ZcashRustBackend: ZcashRustBackendWelding {
UInt(usk.bytes.count),
[CChar](address.utf8CString),
value,
memo.bytes,
memo?.bytes,
spendParamsPath,
UInt(spendParamsPath.lengthOfBytes(using: .utf8)),
outputParamsPath,
@ -584,7 +583,7 @@ class ZcashRustBackend: ZcashRustBackendWelding {
dbCache: URL,
dbData: URL,
usk: UnifiedSpendingKey,
memo: MemoBytes,
memo: MemoBytes?,
spendParamsPath: String,
outputParamsPath: String,
networkType: NetworkType
@ -597,7 +596,7 @@ class ZcashRustBackend: ZcashRustBackendWelding {
dbData.1,
uskBuffer.baseAddress,
UInt(usk.bytes.count),
memo.bytes,
memo?.bytes,
spendParamsPath,
UInt(spendParamsPath.lengthOfBytes(using: .utf8)),
outputParamsPath,

View File

@ -62,7 +62,7 @@ protocol ZcashRustBackendWelding {
/// - Parameter usk: `UnifiedSpendingKey` for the account that controls the funds to be spent.
/// - Parameter to: recipient address
/// - Parameter value: transaction amount in Zatoshi
/// - Parameter memo: the `Memo` for this transaction
/// - Parameter memo: the `MemoBytes` for this transaction. pass `nil` when sending to transparent receivers
/// - Parameter spendParamsPath: path escaped String for the filesystem locations where the spend parameters are located
/// - Parameter outputParamsPath: path escaped String for the filesystem locations where the output parameters are located
/// - Parameter networkType: network type of this key
@ -71,7 +71,7 @@ protocol ZcashRustBackendWelding {
usk: UnifiedSpendingKey,
to address: String,
value: Int64,
memo: MemoBytes,
memo: MemoBytes?,
spendParamsPath: String,
outputParamsPath: String,
networkType: NetworkType
@ -451,7 +451,7 @@ protocol ZcashRustBackendWelding {
dbCache: URL,
dbData: URL,
usk: UnifiedSpendingKey,
memo: MemoBytes,
memo: MemoBytes?,
spendParamsPath: String,
outputParamsPath: String,
networkType: NetworkType

View File

@ -109,13 +109,13 @@ public protocol Synchronizer {
/// - Parameter spendingKey: the `UnifiedSpendingKey` that allows spends to occur.
/// - Parameter zatoshi: the amount to send in Zatoshi.
/// - Parameter toAddress: the recipient's address.
/// - Parameter memo: the memo to include as part of the transaction.
/// - Parameter memo: an `Optional<Memo>`with the memo to include as part of the transaction. send `nil` when sending to transparent receivers otherwise the function will throw an error
// swiftlint:disable:next function_parameter_count
func sendToAddress(
spendingKey: UnifiedSpendingKey,
zatoshi: Zatoshi,
toAddress: Recipient,
memo: Memo
memo: Memo?
) async throws -> PendingTransactionEntity
/// Shields transparent funds from the given private key into the best shielded pool of the account associated to the given `UnifiedSpendingKey`.

View File

@ -461,14 +461,19 @@ public class SDKSynchronizer: Synchronizer {
spendingKey: UnifiedSpendingKey,
zatoshi: Zatoshi,
toAddress: Recipient,
memo: Memo
memo: Memo?
) async throws -> PendingTransactionEntity {
do {
try await initializer.downloadParametersIfNeeded()
} catch {
throw SynchronizerError.parameterMissing(underlyingError: error)
}
if case Recipient.transparent = toAddress,
memo != nil {
throw SynchronizerError.generalError(message: "Memos can't be sent to transparent addresses.")
}
return try await createToAddress(
spendingKey: spendingKey,
zatoshi: zatoshi,
@ -510,13 +515,13 @@ public class SDKSynchronizer: Synchronizer {
spendingKey: UnifiedSpendingKey,
zatoshi: Zatoshi,
recipient: Recipient,
memo: Memo
memo: Memo?
) async throws -> PendingTransactionEntity {
do {
let spend = try transactionManager.initSpend(
zatoshi: zatoshi,
recipient: .address(recipient),
memo: memo.asMemoBytes(),
memo: memo?.asMemoBytes(),
from: Int(spendingKey.account)
)

View File

@ -42,7 +42,7 @@ class PersistentTransactionManager: OutboundTransactionManager {
func initSpend(
zatoshi: Zatoshi,
recipient: PendingTransactionRecipient,
memo: MemoBytes,
memo: MemoBytes?,
from accountIndex: Int
) throws -> PendingTransactionEntity {
guard let insertedTx = try repository.find(
@ -72,7 +72,7 @@ class PersistentTransactionManager: OutboundTransactionManager {
do {
let encodedTransaction = try await self.encoder.createShieldingTransaction(
spendingKey: spendingKey,
memoBytes: try pendingTransaction.memo.intoMemoBytes(),
memoBytes: try pendingTransaction.memo?.intoMemoBytes(),
from: pendingTransaction.accountIndex
)
let transaction = try self.encoder.expandEncodedTransaction(encodedTransaction)
@ -115,7 +115,7 @@ class PersistentTransactionManager: OutboundTransactionManager {
spendingKey: spendingKey,
zatoshi: pendingTransaction.value,
to: toAddress!,
memoBytes: try pendingTransaction.memo.intoMemoBytes(),
memoBytes: try pendingTransaction.memo?.intoMemoBytes(),
from: pendingTransaction.accountIndex
)
let transaction = try self.encoder.expandEncodedTransaction(encodedTransaction)

View File

@ -34,7 +34,7 @@ protocol TransactionEncoder {
spendingKey: UnifiedSpendingKey,
zatoshi: Zatoshi,
to address: String,
memoBytes: MemoBytes,
memoBytes: MemoBytes?,
from accountIndex: Int
) async throws -> EncodedTransaction
@ -50,7 +50,7 @@ protocol TransactionEncoder {
*/
func createShieldingTransaction(
spendingKey: UnifiedSpendingKey,
memoBytes: MemoBytes,
memoBytes: MemoBytes?,
from accountIndex: Int
) async throws -> EncodedTransaction

View File

@ -17,7 +17,7 @@ protocol OutboundTransactionManager {
func initSpend(
zatoshi: Zatoshi,
recipient: PendingTransactionRecipient,
memo: MemoBytes,
memo: MemoBytes?,
from accountIndex: Int
) throws -> PendingTransactionEntity

View File

@ -51,7 +51,7 @@ class WalletTransactionEncoder: TransactionEncoder {
spendingKey: UnifiedSpendingKey,
zatoshi: Zatoshi,
to address: String,
memoBytes: MemoBytes,
memoBytes: MemoBytes?,
from accountIndex: Int
) async throws -> EncodedTransaction {
let txId = try createSpend(
@ -80,7 +80,7 @@ class WalletTransactionEncoder: TransactionEncoder {
spendingKey: UnifiedSpendingKey,
zatoshi: Zatoshi,
to address: String,
memoBytes: MemoBytes,
memoBytes: MemoBytes?,
from accountIndex: Int
) throws -> Int {
guard ensureParams(spend: self.spendParamsURL, output: self.spendParamsURL) else {
@ -107,7 +107,7 @@ class WalletTransactionEncoder: TransactionEncoder {
func createShieldingTransaction(
spendingKey: UnifiedSpendingKey,
memoBytes: MemoBytes,
memoBytes: MemoBytes?,
from accountIndex: Int
) async throws -> EncodedTransaction {
let txId = try createShieldingSpend(
@ -129,7 +129,11 @@ class WalletTransactionEncoder: TransactionEncoder {
throw TransactionEncoderError.notFound(transactionId: txId)
}
}
func createShieldingSpend(spendingKey: UnifiedSpendingKey, memo: MemoBytes, accountIndex: Int) throws -> Int {
func createShieldingSpend(
spendingKey: UnifiedSpendingKey,
memo: MemoBytes?,
accountIndex: Int
) throws -> Int {
guard ensureParams(spend: self.spendParamsURL, output: self.spendParamsURL) else {
throw TransactionEncoderError.missingParams
}