Skip to main content

Precompute Sign

Presign allows the signing parties to offline precompute expensive signature material. Offline means before any active user needs to sign a specific message. By doing so the total running time for computing signatures is almost instant minimizing net work traffic load and computation demand.

We can finalise the precomputed signature with actual message later.

Precompute signing (preSign / preSignFinal) is available for the ECDSA algorithm only.

Step 1 : Create Session


Step 2 : Perform Precompute Signature


  • Call trioSession.preSign() which returns Result of Success with Precompute Signature bytes as Data or Failure with error.

        let result = await trioSession.preSign(keyId: keyId)
    // returns nil if operation fails or other flow you might have
    switch result {
    case .success(let dataBytes):
    do {
    //todo: Do final signature later using preComputedSignature (lambda argument received here)
    Swift.print("PreComputeSignature : Success, Pre Computed Signature size : \(dataBytes.count)")
    return dataBytes
    }
    case .failure(let error):
    do {
    // show error to user or abort process
    Swift.print(error)
    return nil
    }
    }
    • keyId identifies the client's key; the SDK loads the keyshare from your StorageClient using it.
    • preComputedSignature is the pre computed signature bytes as Data which could later be used to perform final signature.

Step 3 : Finalise Precompute Signature (Later in the future)


  • Call trioSession.preSignFinal() which returns Result of Success with the Final Signature bytes as Data or Failure with error.


    let messageHash = "e2a159d17b7bb714aed7675d7c7d394dec8d2e4337842848104694bf89c71c03"

    let result = await trioSession.preSign(keyId: keyId)
    // returns nil if operation fails or other flow you might have
    switch result {
    case .success(let dataBytes):
    do {
    //todo: Do final signature later using preComputedSignature (lambda argument received here)
    Swift.print("PreComputeSignature : Success, Pre Computed Signature size : \(dataBytes.count)")


    let finalResult = await trioSession.preSignFinal(
    preSignature: dataBytes,
    message: messageHash
    )
    // returns nil if operation fails or other flow you might have
    switch finalResult {
    case .success(let finalSignatureBytes):
    do {
    Swift.print("PreSignatureFinal : Success, Signature size : \(finalSignatureBytes.count)")
    return dataBytes
    }
    case .failure(let error):
    do {
    // show error to user or abort process
    Swift.print(error)
    return nil
    }
    }
    }
    case .failure(let error):
    do {
    // show error to user or abort process
    Swift.print(error)
    return nil
    }
    }
    • preComputedSignature is the pre computed signature bytes as Data generated in step 2.
    • messageHash is the messageHash in Hex string without 0x suffix (absolute hex-string).

Example (Precompute Signature and Finalise Precomputed Signature)


Example.swift
    let messageHash = "e2a159d17b7bb714aed7675d7c7d394dec8d2e4337842848104694bf89c71c03"

// preSign precomputes the message-independent part of a signature; preSignFinal
// turns it into a full signature once the message is known. ECDSA only.
func performPreSignAndFinalize(keyId: String, trioSession: TrioSession) async -> Data? {
// Compute the pre-signature.
let preSignResult = await trioSession.preSign(keyId: keyId)
switch preSignResult {
case .success(let preComputedSignature):
Swift.print("PreSign: success, size \(preComputedSignature.count)")

// Finalize the pre-signature with the actual message.
let finalResult = await trioSession.preSignFinal(
preSignature: preComputedSignature,
message: messageHash,
derivationPath: "m" // This is the default; use your desired path, e.g. "m/1/2"
)
switch finalResult {
case .success(let signature):
Swift.print("PreSignFinal: success, size \(signature.count)")
return signature
case .failure(let error):
// show the error to the user or abort the process
Swift.print(error)
return nil
}
case .failure(let error):
// show the error to the user or abort the process
Swift.print(error)
return nil
}
}
  • keyId identifies the client's key; the SDK loads the keyshare from your StorageClient using it.
  • preComputedSignature is the pre computed signature bytes as Data which could later be used to perform final signature.
  • messageHash is the messageHash in Hex string without 0x suffix (absolute hex-string).

  • trioSession.preSign() performs message exchange between mobile and server to generate a pre computed ECDSA signature byte as Data which is expected to be used to finalise the signature later with message.
  • Result of trioSession.preSign() could be a Success with bytes as Data (ECDSA pre-computed signature), corresponding to the public key (or address) of the wallet or Failure with error.

  • trioSession.preSignFinal() performs message exchange between mobile and server to generate a ECDSA signature.
  • Result of trioSession.preSignFinal() could be a Success with bytes as Data (ECDSA signature) of messageHash, corresponding to the public key (or address) of the wallet or Failure with error.