OZSmartAccountAuth
public enum OZSmartAccountAuth
Authentication helpers for OpenZeppelin smart-account authorization entries.
Provides functions to attach signatures to authorisation entries and to build the payload hashes that signers must commit to. The helpers cover:
- Computing the auth digest that binds context rule IDs to a signature payload.
- Building the authorisation payload hash for both address and source-account credentials.
- Attaching pre-computed signatures to authorisation entries while preserving any existing signatures and ordering map entries deterministically.
- Adding raw signature map entries (used for delegated-signer placeholders).
Example:
let payloadHash = try await OZSmartAccountAuth.buildAuthPayloadHash(
entry: unsignedEntry,
expirationLedger: currentLedger + 100,
networkPassphrase: Network.testnet.passphrase
)
// ... compute the signature over `payloadHash` externally ...
let signedEntry = try await OZSmartAccountAuth.signAuthEntry(
entry: unsignedEntry,
signer: webAuthnSigner,
signature: webAuthnSignature,
expirationLedger: currentLedger + 100
)
-
buildAuthDigest(signaturePayload:AsynchronouscontextRuleIds: ) Computes the auth digest that binds context rule IDs to the signature payload.
The digest is
SHA-256(signaturePayload || contextRuleIds.toXDR())wherecontextRuleIds.toXDR()is the XDR encoding ofScVal::Vec([ScVal::U32(id), ...]). Binding the rule IDs into the digest prevents replay of a signed payload against a different rule set.Throws
SmartAccountTransactionException.SigningFailedwhen XDR encoding fails.Declaration
Swift
public static func buildAuthDigest( signaturePayload: Data, contextRuleIds: [UInt32] ) async throws -> DataParameters
signaturePayload32-byte signature payload hash from
buildAuthPayloadHash.contextRuleIdsContext rule IDs to bind into the digest.
Return Value
32-byte SHA-256 auth digest.
-
Builds the authorisation payload hash for signing.
Computes the hash that must be signed to authorise a Soroban operation; the hash is used as the WebAuthn challenge when collecting biometric signatures. The entry must have address credentials.
The preimage is constructed as
HashIDPreimage::SorobanAuthorization { networkId, nonce, signatureExpirationLedger, invocation }and the returned value isSHA-256(XDR_encode(preimage)).Throws
SmartAccountTransactionException.SigningFailedwhen credentials are not address type or when XDR encoding fails.Declaration
Swift
public static func buildAuthPayloadHash( entry: SorobanAuthorizationEntryXDR, expirationLedger: UInt32, networkPassphrase: String ) async throws -> DataParameters
entryAuthorisation entry to build the payload hash for.
expirationLedgerLedger number at which the signature expires.
networkPassphraseNetwork passphrase.
Return Value
32-byte SHA-256 hash of the authorisation payload.
-
Builds the authorisation payload hash for source-account credentials.
Used when converting source-account credentials to address credentials, typically for relayer fee sponsoring. The preimage is constructed identically to
buildAuthPayloadHashbut uses the suppliednonceandexpirationLedgerinstead of reading them from existing credentials.Throws
SmartAccountTransactionException.SigningFailedwhen XDR encoding fails.Declaration
Swift
public static func buildSourceAccountAuthPayloadHash( entry: SorobanAuthorizationEntryXDR, nonce: Int64, expirationLedger: UInt32, networkPassphrase: String ) async throws -> DataParameters
entryAuthorisation entry whose root invocation is bound into the preimage.
nonceNonce to use for the new address credentials.
expirationLedgerLedger number at which the signature expires.
networkPassphraseNetwork passphrase.
Return Value
32-byte SHA-256 hash of the authorisation payload.
-
Attaches a pre-computed signature to an authorisation entry without mutating the input.
Does not perform cryptographic signing; the caller must compute the signature over the hash returned by
buildAuthPayloadHashusing the sameexpirationLedger. WhencontextRuleIdsis non-empty it overrides any existing context-rule IDs in the payload; otherwise the existing value is preserved.Throws
SmartAccountTransactionException.SigningFailedwhen credentials are not address type, when the XDR clone fails, or when encoding the signer or signature fails.Declaration
Swift
public static func signAuthEntry( entry: SorobanAuthorizationEntryXDR, signer: any OZSmartAccountSigner, signature: any OZSmartAccountSignature, expirationLedger: UInt32, contextRuleIds: [UInt32] = [] ) async throws -> SorobanAuthorizationEntryXDRParameters
entryAuthorisation entry to attach the signature to.
signerSmart-account signer (delegated or external).
signaturePre-computed signature object (WebAuthn, Ed25519, or Policy).
expirationLedgerLedger number at which the signature expires (must match the value used when computing the payload hash).
contextRuleIdsOptional override for the bound context rule IDs.
Return Value
A new authorisation entry with the signature attached.
-
Adds a raw key/value entry to the auth entry’s signature map.
Used for delegated-signer placeholders where the value is
Bytes(often empty). Uses the AuthPayload format accepted by the OpenZeppelin Smart Account contract.When
signatureValueis anSCValXDR.bytesvalue its raw bytes are stored directly; otherwise the value is XDR-encoded and the resulting bytes are stored. The input entry is never mutated; a new entry with the updated payload is returned.Throws
SmartAccountTransactionException.SigningFailedwhen credentials are not address type or when XDR encoding of the signature value fails.Declaration
Swift
public static func addRawSignatureMapEntry( entry: SorobanAuthorizationEntryXDR, signerKey: SCValXDR, signatureValue: SCValXDR, contextRuleIds: [UInt32] = [] ) throws -> SorobanAuthorizationEntryXDRParameters
entryAuthorisation entry to modify.
signerKeySigner-key ScVal (map key).
signatureValueRaw ScVal value to attach.
contextRuleIdsOptional override for the bound context rule IDs.
Return Value
A new authorisation entry with the map entry added.
View on GitHub
Install in Dash