Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove cryptoswift #11

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,13 @@
"version": "5.3.0"
}
},
{
"package": "CryptoSwift",
"repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git",
"state": {
"branch": null,
"revision": "32f641cf24fc7abc1c591a2025e9f2f572648b0f",
"version": "1.7.2"
}
},
{
"package": "curvelib.swift",
"repositoryURL": "https://github.com/tkey/curvelib.swift",
"state": {
"branch": null,
"revision": "7dad3bf1793de263f83406c08c18c9316abf082f",
"version": "0.1.2"
"revision": "2df9f638fd121b445ce3b410cd79ac4d0cfa94ed",
"version": "1.0.0"
}
},
{
Expand Down Expand Up @@ -132,8 +123,8 @@
"repositoryURL": "https://github.com/torusresearch/tss-client-swift.git",
"state": {
"branch": null,
"revision": "9590a1d784177d4679eca62d4dbfe781e9870c40",
"version": "2.0.2"
"revision": "30e7ec88b5b854581f2f1dc074cea00462f4142e",
"version": "3.0.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(
],
dependencies: [
.package(url: "https://github.com/argentlabs/web3.swift", from:"1.6.0"),
.package(url: "https://github.com/torusresearch/tss-client-swift.git", from: "2.0.2"),
.package(url: "https://github.com/torusresearch/tss-client-swift.git", from: "3.0.0"),
],
targets: [
.target(
Expand Down
23 changes: 16 additions & 7 deletions Sources/Web3SwiftMpcProvider/EthereumTssAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ public final class EthereumTssAccount: EthereumAccountProtocol {
/// - Parameters:
/// - params : Parameters used to initialize the account
///
public required init(params: EthTssAccountParams) {
public required init(params: EthTssAccountParams) throws {
ethAccountParams = params
guard let publicKey = Data(hexString: ethAccountParams.publicKey) else {
throw CustomSigningError.generalError(error: "Cannot convert public key to data")
}
// swiftlint:disable:next line_length
address = EthereumAddress(KeyUtil.generateAddress(from: Data(hex: ethAccountParams.publicKey).suffix(64)).toChecksumAddress())
address = EthereumAddress(KeyUtil.generateAddress(from: publicKey.suffix(64)).toChecksumAddress())
}

/// Signs using provided Data
Expand All @@ -44,7 +47,7 @@ public final class EthereumTssAccount: EthereumAccountProtocol {
///
/// - Throws: On signing failure
public func sign(data: Data) throws -> Data {
let hash = data.sha3(.keccak256)
let hash = try keccak256(data: data)
let signature = try sign(message: hash)
return signature
}
Expand All @@ -58,7 +61,7 @@ public final class EthereumTssAccount: EthereumAccountProtocol {
///
/// - Throws: On signing failure
public func sign(hex: String) throws -> Data {
if let data = Data(hex: hex) {
if let data = Data(hexString: hex) {
return try sign(data: data)
} else {
throw EthereumAccountError.signError
Expand Down Expand Up @@ -113,8 +116,11 @@ public final class EthereumTssAccount: EthereumAccountProtocol {

try client.cleanup(signatures: ethAccountParams.authSigs)

guard let pk = Data(hexString: ethAccountParams.publicKey) else {
throw CustomSigningError.generalError(error: "Unable to convert public key to data")
}
// swiftlint:disable:next line_length
let verified = TSSHelpers.verifySignature(msgHash: signingMessage, s: s, r: r, v: v, pubKey: Data(hex: ethAccountParams.publicKey))
let verified = TSSHelpers.verifySignature(msgHash: signingMessage, s: s, r: r, v: v, pubKey: pk)
if !verified {
throw EthereumSignerError.unknownError
}
Expand Down Expand Up @@ -228,7 +234,7 @@ public final class EthereumTssAccount: EthereumAccountProtocol {
throw CustomSigningError.generalError(error: "Could not generate random key for sessionID nonce")
}
let random = BigInt(sign: .plus, magnitude: randomKeyBigUint) + BigInt(Date().timeIntervalSince1970)
let sessionNonce = TSSHelpers.base64ToBase64url(base64: TSSHelpers.hashMessage(message: String(random)))
let sessionNonce = TSSHelpers.base64ToBase64url(base64: try TSSHelpers.hashMessage(message: String(random)))
// create the full session string
// swiftlint:disable:next line_length
let session = TSSHelpers.assembleFullSession(verifier: params.verifier, verifierId: params.verifierID, tssTag: params.selectedTag, tssNonce: String(params.tssNonce), sessionNonce: sessionNonce)
Expand All @@ -249,11 +255,14 @@ public final class EthereumTssAccount: EthereumAccountProtocol {
// swiftlint:disable:next line_length
let denormalizeShare = try TSSHelpers.denormalizeShare(participatingServerDKGIndexes: nodeInd.map({ BigInt($0) }), userTssIndex: userTssIndex, userTssShare: share)
// swiftlint:disable:next line_length
guard let pk = Data(hexString: params.publicKey) else {
throw CustomSigningError.generalError(error: "Unable to convert public key to data")
}
let client = try TSSClient(session: session, index: Int32(clientIndex), parties: partyIndexes.map({ Int32($0) }),
// swiftlint:disable:next line_length
endpoints: urls.map({ URL(string: $0 ?? "") }), tssSocketEndpoints: socketUrls.map({ URL(string: $0 ?? "") }),
// swiftlint:disable:next line_length
share: TSSHelpers.base64Share(share: denormalizeShare), pubKey: try TSSHelpers.base64PublicKey(pubKey: Data(hex: params.publicKey)))
share: TSSHelpers.base64Share(share: denormalizeShare), pubKey: try TSSHelpers.base64PublicKey(pubKey: pk))

return (client, coeffs)
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/Web3SwiftMpcProviderTests/MpcProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ final class Web3SwiftMpcProviderTests: XCTestCase {
func testSigningMessage() throws {
let params = EthTssAccountParams(publicKey: fullAddress, factorKey: factorKey, tssNonce: Int32(tssNonce), tssShare: tssShare, tssIndex: tssIndex, selectedTag: selected_tag, verifier: verifier, verifierID: verifierId, nodeIndexes: [], tssEndpoints: tssEndpoints, authSigs: sigs)

let account = EthereumTssAccount(params: params)
let account = try EthereumTssAccount(params: params)

let msg = "hello world"
let _ = try account.sign(message: msg)
}

func testSigningTransaction() throws {
let params = EthTssAccountParams(publicKey: fullAddress, factorKey: factorKey, tssNonce: Int32(tssNonce), tssShare: tssShare, tssIndex: tssIndex, selectedTag: selected_tag, verifier: verifier, verifierID: verifierId, nodeIndexes: [], tssEndpoints: tssEndpoints, authSigs: sigs)
let tssAccount = EthereumTssAccount(params: params)
let tssAccount = try EthereumTssAccount(params: params)
let chainID = 5
let amount = 0.001
let toAddress = tssAccount.address
Expand All @@ -114,7 +114,7 @@ final class Web3SwiftMpcProviderTests: XCTestCase {
func testSignTyped() throws {
let typedData = try decoder.decode(TypedData.self, from: example1)
let params = EthTssAccountParams(publicKey: fullAddress, factorKey: factorKey, tssNonce: Int32(tssNonce), tssShare: tssShare, tssIndex: tssIndex, selectedTag: selected_tag, verifier: verifier, verifierID: verifierId, nodeIndexes: [], tssEndpoints: tssEndpoints, authSigs: sigs)
let tssAccount = EthereumTssAccount(params: params)
let tssAccount = try EthereumTssAccount(params: params)
let _ = try tssAccount.signMessage(message: typedData)
}
}
Loading