Skip to content

Commit

Permalink
Fix issue with Blake2 and kiosk test warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoDotIO committed Dec 19, 2023
1 parent 7bf7425 commit 4bce57d
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/SuiKit/Types/Structs/Signers/RawSigner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SuiKit/Utils/ED25519/ED25519PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sources/SuiKit/Utils/ED25519/ED25519PublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<ED25519PublicKey.LENGTH * 2]
value: try Blake2b.hash(size: 32, data: tmp).hexEncodedString()[0..<ED25519PublicKey.LENGTH * 2]
)
return result
}
Expand Down Expand Up @@ -132,7 +132,7 @@ public struct ED25519PublicKey: 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)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SuiKit/Utils/SECP256R1/SECP256R1PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions Sources/SuiKit/Utils/SECP256R1/SECP256R1PublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SuiKit/Utils/SECP256k1/SECP256K1PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions Sources/SuiKit/Utils/SECP256k1/SECP256K1PublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/SuiKit/Utils/zkLogin/zkLoginPublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion Sources/SuiKit/Utils/zkLogin/zkLoginUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
64 changes: 37 additions & 27 deletions Tests/SuiKitTests/E2E/Utils/KioskToolbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -39,56 +40,64 @@ 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)
}

func purchaseFlow(buyerCap: KioskOwnerCap, sellerCap: KioskOwnerCap, itemType: String, itemId: String) async throws {
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)
}

func purchaseOnNewKiosk(sellerCap: KioskOwnerCap, itemType: String, itemId: String, personal: Bool? = nil) async throws {
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)
Expand All @@ -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)
}

Expand Down

0 comments on commit 4bce57d

Please sign in to comment.