From 4bce57d17798b04c615b59966dab55664b696336 Mon Sep 17 00:00:00 2001 From: MarcoDotIO Date: Tue, 19 Dec 2023 18:47:52 -0500 Subject: [PATCH] Fix issue with Blake2 and kiosk test warnings --- .../xcshareddata/swiftpm/Package.resolved | 4 +- Package.swift | 2 +- Package@swift-5.5.swift | 2 +- Package@swift-5.6.swift | 2 +- Package@swift-5.7.swift | 2 +- Package@swift-5.8.swift | 2 +- .../Transactions/TransactionBlockData.swift | 2 +- .../Types/Structs/Signers/RawSigner.swift | 2 +- .../Utils/ED25519/ED25519PrivateKey.swift | 2 +- .../Utils/ED25519/ED25519PublicKey.swift | 4 +- .../Utils/SECP256R1/SECP256R1PrivateKey.swift | 2 +- .../Utils/SECP256R1/SECP256R1PublicKey.swift | 5 +- .../Utils/SECP256k1/SECP256K1PrivateKey.swift | 2 +- .../Utils/SECP256k1/SECP256K1PublicKey.swift | 5 +- .../Utils/zkLogin/zkLoginPublicKey.swift | 3 +- .../Utils/zkLogin/zkLoginUtilities.swift | 2 +- .../SuiKitTests/E2E/Utils/KioskToolbox.swift | 64 +++++++++++-------- 17 files changed, 57 insertions(+), 50 deletions(-) diff --git a/Examples/iOS_Example/iOS_Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Examples/iOS_Example/iOS_Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 751bff2..0520ae4 100644 --- a/Examples/iOS_Example/iOS_Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Examples/iOS_Example/iOS_Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -32,8 +32,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/tesseract-one/Blake2.swift.git", "state" : { - "revision" : "106f68231100021e1f745779e241f81a065ad0de", - "version" : "0.1.2" + "revision" : "29c55c8fe42d6661e5a32cc5bbbad1fff64fd01e", + "version" : "0.2.0" } }, { diff --git a/Package.swift b/Package.swift index f09ea82..9e39543 100644 --- a/Package.swift +++ b/Package.swift @@ -14,7 +14,7 @@ let package = Package( .package(url: "https://github.com/hyugit/UInt256.git", from: "0.2.2"), .package(url: "https://github.com/pebble8888/ed25519swift.git", from: "1.2.7"), .package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.0.0"), - .package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.1.0"), + .package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.2.0"), .package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.0"), .package(url: "https://github.com/tesseract-one/Bip39.swift.git", from: "0.1.1"), .package(url: "https://github.com/web3swift-team/web3swift.git", from: "3.2.0"), diff --git a/Package@swift-5.5.swift b/Package@swift-5.5.swift index 17cc121..d6bdc4b 100644 --- a/Package@swift-5.5.swift +++ b/Package@swift-5.5.swift @@ -14,7 +14,7 @@ let package = Package( .package(url: "https://github.com/hyugit/UInt256.git", from: "0.2.2"), .package(url: "https://github.com/pebble8888/ed25519swift.git", from: "1.2.7"), .package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.0.0"), - .package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.1.0"), + .package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.2.0"), .package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.0"), .package(url: "https://github.com/tesseract-one/Bip39.swift.git", from: "0.1.1"), .package(url: "https://github.com/web3swift-team/web3swift.git", from: "3.2.0"), diff --git a/Package@swift-5.6.swift b/Package@swift-5.6.swift index 6cec2c1..d258b4f 100644 --- a/Package@swift-5.6.swift +++ b/Package@swift-5.6.swift @@ -14,7 +14,7 @@ let package = Package( .package(url: "https://github.com/hyugit/UInt256.git", from: "0.2.2"), .package(url: "https://github.com/pebble8888/ed25519swift.git", from: "1.2.7"), .package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.0.0"), - .package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.1.0"), + .package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.2.0"), .package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.0"), .package(url: "https://github.com/tesseract-one/Bip39.swift.git", from: "0.1.1"), .package(url: "https://github.com/web3swift-team/web3swift.git", from: "3.2.0"), diff --git a/Package@swift-5.7.swift b/Package@swift-5.7.swift index 88f0380..a34ba5f 100644 --- a/Package@swift-5.7.swift +++ b/Package@swift-5.7.swift @@ -14,7 +14,7 @@ let package = Package( .package(url: "https://github.com/hyugit/UInt256.git", from: "0.2.2"), .package(url: "https://github.com/pebble8888/ed25519swift.git", from: "1.2.7"), .package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.0.0"), - .package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.1.0"), + .package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.2.0"), .package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.0"), .package(url: "https://github.com/tesseract-one/Bip39.swift.git", from: "0.1.1"), .package(url: "https://github.com/web3swift-team/web3swift.git", from: "3.2.0"), diff --git a/Package@swift-5.8.swift b/Package@swift-5.8.swift index 07be047..c84ae91 100644 --- a/Package@swift-5.8.swift +++ b/Package@swift-5.8.swift @@ -14,7 +14,7 @@ let package = Package( .package(url: "https://github.com/hyugit/UInt256.git", from: "0.2.2"), .package(url: "https://github.com/pebble8888/ed25519swift.git", from: "1.2.7"), .package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.0.0"), - .package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.1.0"), + .package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.2.0"), .package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.0"), .package(url: "https://github.com/tesseract-one/Bip39.swift.git", from: "0.1.1"), .package(url: "https://github.com/web3swift-team/web3swift.git", from: "3.2.0"), diff --git a/Sources/SuiKit/Types/Structs/Builders/Transactions/TransactionBlockData.swift b/Sources/SuiKit/Types/Structs/Builders/Transactions/TransactionBlockData.swift index 034bd5d..753d61c 100644 --- a/Sources/SuiKit/Types/Structs/Builders/Transactions/TransactionBlockData.swift +++ b/Sources/SuiKit/Types/Structs/Builders/Transactions/TransactionBlockData.swift @@ -76,7 +76,7 @@ public struct TransactionBlockDataBuilder: KeyProtocol { dataWithTag.append(contentsOf: typeTagBytes) dataWithTag.append(contentsOf: data) - let hashedData = try Blake2.hash(.b2b, size: 32, data: Data(dataWithTag)) + let hashedData = try Blake2b.hash(size: 32, data: Data(dataWithTag)) let hash = Array(hashedData) return hash.base58EncodedString diff --git a/Sources/SuiKit/Types/Structs/Signers/RawSigner.swift b/Sources/SuiKit/Types/Structs/Signers/RawSigner.swift index 56be8b5..3da7ba3 100644 --- a/Sources/SuiKit/Types/Structs/Signers/RawSigner.swift +++ b/Sources/SuiKit/Types/Structs/Signers/RawSigner.swift @@ -59,7 +59,7 @@ public struct RawSigner: SignerWithProviderProtocol { /// - Throws: An error if the signing process fails. public func signData(data: Data) throws -> String { let pubKey = self.account.publicKey.base64() - let digest = try Blake2.hash(.b2b, size: 32, data: data) + let digest = try Blake2b.hash(size: 32, data: data) let signature = try self.account.sign(digest) let signatureScheme = self.account.accountType return try Self.toSerializedSignature(signature, signatureScheme, pubKey) diff --git a/Sources/SuiKit/Utils/ED25519/ED25519PrivateKey.swift b/Sources/SuiKit/Utils/ED25519/ED25519PrivateKey.swift index 7fe2169..c4fdce2 100644 --- a/Sources/SuiKit/Utils/ED25519/ED25519PrivateKey.swift +++ b/Sources/SuiKit/Utils/ED25519/ED25519PrivateKey.swift @@ -117,7 +117,7 @@ public struct ED25519PrivateKey: Equatable, PrivateKeyProtocol { public func signWithIntent(_ bytes: [UInt8], _ intent: IntentScope) throws -> Signature { let intentMessage = RawSigner.messageWithIntent(intent, Data(bytes)) - let digest = try Blake2.hash(.b2b, size: 32, data: intentMessage) + let digest = try Blake2b.hash(size: 32, data: intentMessage) let signature = try self.sign(data: digest) return signature diff --git a/Sources/SuiKit/Utils/ED25519/ED25519PublicKey.swift b/Sources/SuiKit/Utils/ED25519/ED25519PublicKey.swift index a25c2e8..5a89b5f 100644 --- a/Sources/SuiKit/Utils/ED25519/ED25519PublicKey.swift +++ b/Sources/SuiKit/Utils/ED25519/ED25519PublicKey.swift @@ -91,7 +91,7 @@ public struct ED25519PublicKey: Equatable, PublicKeyProtocol { try tmp.set([SignatureSchemeFlags.SIGNATURE_SCHEME_TO_FLAG["ED25519"]!]) try tmp.set([UInt8](self.key), offset: 1) let result = try Inputs.normalizeSuiAddress( - value: try Blake2.hash(.b2b, size: 32, data: tmp).hexEncodedString()[0.. Bool { let intentMessage = RawSigner.messageWithIntent(intent, Data(bytes)) - let digest = try Blake2.hash(.b2b, size: 32, data: intentMessage) + let digest = try Blake2b.hash(size: 32, data: intentMessage) return try self.verify(data: digest, signature: signature) } diff --git a/Sources/SuiKit/Utils/SECP256R1/SECP256R1PrivateKey.swift b/Sources/SuiKit/Utils/SECP256R1/SECP256R1PrivateKey.swift index ca6ec98..89dfe5c 100644 --- a/Sources/SuiKit/Utils/SECP256R1/SECP256R1PrivateKey.swift +++ b/Sources/SuiKit/Utils/SECP256R1/SECP256R1PrivateKey.swift @@ -130,7 +130,7 @@ public struct SECP256R1PrivateKey: PrivateKeyProtocol { public func signWithIntent(_ bytes: [UInt8], _ intent: IntentScope) throws -> Signature { let intentMessage = RawSigner.messageWithIntent(intent, Data(bytes)) - let digest = try Blake2.hash(.b2b, size: 32, data: intentMessage) + let digest = try Blake2b.hash(size: 32, data: intentMessage) let signature = try self.sign(data: digest) return signature diff --git a/Sources/SuiKit/Utils/SECP256R1/SECP256R1PublicKey.swift b/Sources/SuiKit/Utils/SECP256R1/SECP256R1PublicKey.swift index f231117..ca83cc3 100644 --- a/Sources/SuiKit/Utils/SECP256R1/SECP256R1PublicKey.swift +++ b/Sources/SuiKit/Utils/SECP256R1/SECP256R1PublicKey.swift @@ -81,8 +81,7 @@ public struct SECP256R1PublicKey: PublicKeyProtocol { public func toSuiAddress() throws -> String { return try Inputs.normalizeSuiAddress( - value: try Blake2.hash( - .b2b, + value: try Blake2b.hash( size: 32, data: Data(try self.toSuiBytes()) ).hexEncodedString()[0..<(32 * 2)] @@ -125,7 +124,7 @@ public struct SECP256R1PublicKey: PublicKeyProtocol { public func verifyWithIntent(_ bytes: [UInt8], _ signature: Signature, _ intent: IntentScope) throws -> Bool { let intentMessage = RawSigner.messageWithIntent(intent, Data(bytes)) - let digest = try Blake2.hash(.b2b, size: 32, data: intentMessage) + let digest = try Blake2b.hash(size: 32, data: intentMessage) return try self.verify(data: digest, signature: signature) } diff --git a/Sources/SuiKit/Utils/SECP256k1/SECP256K1PrivateKey.swift b/Sources/SuiKit/Utils/SECP256k1/SECP256K1PrivateKey.swift index b0ffe02..89ff298 100644 --- a/Sources/SuiKit/Utils/SECP256k1/SECP256K1PrivateKey.swift +++ b/Sources/SuiKit/Utils/SECP256k1/SECP256K1PrivateKey.swift @@ -163,7 +163,7 @@ public struct SECP256K1PrivateKey: Equatable, PrivateKeyProtocol { public func signWithIntent(_ bytes: [UInt8], _ intent: IntentScope) throws -> Signature { let intentMessage = RawSigner.messageWithIntent(intent, Data(bytes)) - let digest = try Blake2.hash(.b2b, size: 32, data: intentMessage) + let digest = try Blake2b.hash(size: 32, data: intentMessage) let signature = try self.sign(data: digest) return signature diff --git a/Sources/SuiKit/Utils/SECP256k1/SECP256K1PublicKey.swift b/Sources/SuiKit/Utils/SECP256k1/SECP256K1PublicKey.swift index 7b84864..9c6b4f5 100644 --- a/Sources/SuiKit/Utils/SECP256k1/SECP256K1PublicKey.swift +++ b/Sources/SuiKit/Utils/SECP256k1/SECP256K1PublicKey.swift @@ -120,8 +120,7 @@ public struct SECP256K1PublicKey: Equatable, PublicKeyProtocol { public func toSuiAddress() throws -> String { return try Inputs.normalizeSuiAddress( - value: try Blake2.hash( - .b2b, + value: try Blake2b.hash( size: 32, data: Data(try self.toSuiBytes()) ).hexEncodedString()[0..<(32 * 2)] @@ -157,7 +156,7 @@ public struct SECP256K1PublicKey: Equatable, PublicKeyProtocol { public func verifyWithIntent(_ bytes: [UInt8], _ signature: Signature, _ intent: IntentScope) throws -> Bool { let intentMessage = RawSigner.messageWithIntent(intent, Data(bytes)) - let digest = try Blake2.hash(.b2b, size: 32, data: intentMessage) + let digest = try Blake2b.hash(size: 32, data: intentMessage) return try self.verify(data: digest, signature: signature) } diff --git a/Sources/SuiKit/Utils/zkLogin/zkLoginPublicKey.swift b/Sources/SuiKit/Utils/zkLogin/zkLoginPublicKey.swift index 128a276..a11db38 100644 --- a/Sources/SuiKit/Utils/zkLogin/zkLoginPublicKey.swift +++ b/Sources/SuiKit/Utils/zkLogin/zkLoginPublicKey.swift @@ -61,8 +61,7 @@ public struct zkLoginPublicIdentifier: PublicKeyProtocol { public func toSuiAddress() throws -> String { return try Inputs.normalizeSuiAddress( - value: try Blake2.hash( - .b2b, + value: try Blake2b.hash( size: 32, data: Data(try self.toSuiBytes()) ).hexEncodedString()[0..<(32 * 2)] diff --git a/Sources/SuiKit/Utils/zkLogin/zkLoginUtilities.swift b/Sources/SuiKit/Utils/zkLogin/zkLoginUtilities.swift index 0b67670..2d02843 100644 --- a/Sources/SuiKit/Utils/zkLogin/zkLoginUtilities.swift +++ b/Sources/SuiKit/Utils/zkLogin/zkLoginUtilities.swift @@ -47,7 +47,7 @@ public struct zkLoginUtilities { tmp[2..<(2+addressParamBytes.count)] = addressParamBytes tmp[(2+addressParamBytes.count)..<(addressParamBytes.count+addressSeedBytesBigEndian.count)] = Data(addressSeedBytesBigEndian) - let blake = (try Blake2.hash(.b2b, size: 32, data: tmp)) + let blake = (try Blake2b.hash(size: 32, data: tmp)) return try Inputs.normalizeSuiAddress(value: blake.hexEncodedString()) } diff --git a/Tests/SuiKitTests/E2E/Utils/KioskToolbox.swift b/Tests/SuiKitTests/E2E/Utils/KioskToolbox.swift index 1fab679..fcf750b 100644 --- a/Tests/SuiKitTests/E2E/Utils/KioskToolbox.swift +++ b/Tests/SuiKitTests/E2E/Utils/KioskToolbox.swift @@ -16,12 +16,13 @@ internal struct KioskToolbox { let publisher = try await self.testToolbox.getPublisherObject() var txb = try TransactionBlock() let tpTx = TransferPolicyTransactionClient(params: TransferPolicyTransactionParams(kioskClient: self.kioskClient, cap: nil), transactionBlock: &txb) - try await tpTx.create(params: TransferPolicyBaseParams(type: "\(heroPackageId)::hero::Hero", publisher: .string(publisher))) - try tpTx.addLockRule() - try tpTx.addFloorPriceRule(minPrice: "1000") - try tpTx.addRoyaltyRule(percentageBps: "\(Int(try Double(10.0).percentageToBasisPoints()))", minAmount: "100") - try tpTx.addPersonalKioskRule() - try await tpTx.shareAndTransferCap(address: try self.testToolbox.address()) + try await tpTx + .create(params: TransferPolicyBaseParams(type: "\(heroPackageId)::hero::Hero", publisher: .string(publisher))) + .addLockRule() + .addFloorPriceRule(minPrice: "1000") + .addRoyaltyRule(percentageBps: "\(Int(try Double(10.0).percentageToBasisPoints()))", minAmount: "100") + .addPersonalKioskRule() + .shareAndTransferCap(address: try self.testToolbox.address()) let _ = try await testToolbox.executeTransactionBlock(txb: &txb) } @@ -39,23 +40,28 @@ internal struct KioskToolbox { let kioskTx = try KioskTransactionClient(transactionBlock: &txb, kioskClient: self.kioskClient, kioskCap: cap) let policies = try await self.kioskClient.getTransferPolicies(type: itemType) guard policies.count == 1 else { throw SuiError.notImplemented } - try kioskTx.lock(itemType: itemType, itemId: .string(itemId), policy: .string(policies[0].id.hex())) - try kioskTx.finalize() + try kioskTx + .lock(itemType: itemType, itemId: .string(itemId), policy: .string(policies[0].id.hex())) + .finalize() let _ = try await self.testToolbox.executeTransactionBlock(txb: &txb) } func existingKioskManagementFlow(cap: KioskOwnerCap, itemType: String, itemId: String) async throws { var txb = try TransactionBlock() let kioskTx = try KioskTransactionClient(transactionBlock: &txb, kioskClient: self.kioskClient, kioskCap: cap) - try kioskTx.place(itemType: itemType, item: .string(itemId)) - try kioskTx.list(itemType: itemType, itemId: itemId, price: "100000") - try kioskTx.delist(itemType: itemType, itemId: itemId) + let _ = try kioskTx + .place(itemType: itemType, item: .string(itemId)) + .list(itemType: itemType, itemId: itemId, price: "100000") + .delist(itemType: itemType, itemId: itemId) + let item = try kioskTx.take(itemType: itemType, itemId: itemId) - try kioskTx.placeAndList(itemType: itemType, item: .objectArgument(item), price: "100000") - try kioskTx.delist(itemType: itemType, itemId: itemId) - try kioskTx.transfer(itemType: itemType, itemId: itemId, address: try self.testToolbox.address()) - try kioskTx.withdraw(address: try self.testToolbox.address()) - try kioskTx.finalize() + + try kioskTx + .placeAndList(itemType: itemType, item: .objectArgument(item), price: "100000") + .delist(itemType: itemType, itemId: itemId) + .transfer(itemType: itemType, itemId: itemId, address: try self.testToolbox.address()) + .withdraw(address: try self.testToolbox.address()) + .finalize() let _ = try await self.testToolbox.executeTransactionBlock(txb: &txb) } @@ -63,14 +69,16 @@ internal struct KioskToolbox { let salePrice: UInt64 = 100_000 var sellTxb = try TransactionBlock() let sellKioskTx = try KioskTransactionClient(transactionBlock: &sellTxb, kioskClient: self.kioskClient, kioskCap: sellerCap) - try sellKioskTx.placeAndList(itemType: itemType, item: .string(itemId), price: "\(salePrice)") - try sellKioskTx.finalize() + try sellKioskTx + .placeAndList(itemType: itemType, item: .string(itemId), price: "\(salePrice)") + .finalize() let _ = try await self.testToolbox.executeTransactionBlock(txb: &sellTxb) var purchaseTxb = try TransactionBlock() let purchaseTx = try KioskTransactionClient(transactionBlock: &purchaseTxb, kioskClient: self.kioskClient, kioskCap: buyerCap) - try await purchaseTx.purchaseAndResolve(itemType: itemType, itemId: itemId, price: salePrice, sellerKiosk: .string(sellerCap.kioskId)) - try purchaseTx.finalize() + try await purchaseTx + .purchaseAndResolve(itemType: itemType, itemId: itemId, price: salePrice, sellerKiosk: .string(sellerCap.kioskId)) + .finalize() let _ = try await self.testToolbox.executeTransactionBlock(txb: &purchaseTxb) } @@ -78,17 +86,18 @@ internal struct KioskToolbox { let salePrice: UInt64 = 100_000 var sellTxb = try TransactionBlock() let sellKioskTx = try KioskTransactionClient(transactionBlock: &sellTxb, kioskClient: self.kioskClient, kioskCap: sellerCap) - try sellKioskTx.placeAndList(itemType: itemType, item: .string(itemId), price: "\(salePrice)") - try sellKioskTx.finalize() + try sellKioskTx + .placeAndList(itemType: itemType, item: .string(itemId), price: "\(salePrice)") + .finalize() let _ = try await self.testToolbox.executeTransactionBlock(txb: &sellTxb) var purchaseTxb = try TransactionBlock() let purchaseTx = try KioskTransactionClient(transactionBlock: &purchaseTxb, kioskClient: self.kioskClient, kioskCap: nil) - if personal != nil, personal! { try purchaseTx.createPersonal(borrow: true) } - else { try purchaseTx.create() } + if personal != nil, personal! { let _ = try purchaseTx.createPersonal(borrow: true) } + else { let _ = try purchaseTx.create() } - try await purchaseTx.purchaseAndResolve(itemType: itemType, itemId: itemId, price: salePrice, sellerKiosk: .string(sellerCap.kioskId)) + let _ = try await purchaseTx.purchaseAndResolve(itemType: itemType, itemId: itemId, price: salePrice, sellerKiosk: .string(sellerCap.kioskId)) if personal == nil || (personal != nil && !(personal!)) { try purchaseTx.shareAndTransferCap(address: try self.testToolbox.address()) } try purchaseTx.finalize() let _ = try await self.testToolbox.executeTransactionBlock(txb: &purchaseTxb) @@ -109,8 +118,9 @@ internal struct KioskToolbox { func createPersonalKiosk() async throws { var txb = try TransactionBlock() let kioskTxClient = try KioskTransactionClient(transactionBlock: &txb, kioskClient: self.kioskClient) - try kioskTxClient.createPersonal() - try kioskTxClient.finalize() + try kioskTxClient + .createPersonal() + .finalize() let _ = try await self.testToolbox.executeTransactionBlock(txb: &txb) }