SmartAccountUtils
public enum SmartAccountUtils
Cryptographic helpers for smart-account operations.
Provides utilities for WebAuthn signature processing, public-key extraction, and contract-address derivation. All members operate on raw byte material and do not depend on any platform-specific WebAuthn API.
-
Normalises a DER-encoded secp256r1 signature to compact format with low-S normalisation.
Steps:
- Parse the DER format via
parseDerSignature. - Normalise
sto its low-S form (s = n - s) whens > n/2. - Pad both
randsto exactly 32 bytes. - Return concatenated
r || s(64 bytes total).
Low-S normalisation ensures that signatures with
svalues greater than half the curve order are converted to their complement, which the Stellar/Soroban verifier requires.Throws
SmartAccountValidationException.InvalidInputwhen the DER format is invalid.Declaration
Swift
public static func normalizeSignature(_ derSignature: Data) throws -> DataParameters
derSignatureDER-encoded signature bytes.
Return Value
64-byte compact signature
r || s. - Parse the DER format via
-
Extracts the secp256r1 public key from a WebAuthn registration response.
Tries
publicKey,authenticatorData, andattestationObjectin order; at least one must be non-nil. Compressed keys (0x02/0x03prefix) throw immediately.Throws
SmartAccountValidationException.InvalidInputwhen a compressed key is detected, when no source is provided, or when all strategies fail.Declaration
Swift
public static func extractPublicKeyFromRegistration( publicKey: Data? = nil, authenticatorData: Data? = nil, attestationObject: Data? = nil ) throws -> DataParameters
publicKeyOptional direct public key bytes (last 65 bytes used when longer, handling COSE/SPKI-wrapped keys).
authenticatorDataOptional raw authenticator data from registration.
attestationObjectOptional raw attestation object from registration.
Return Value
65-byte uncompressed public key (
0x04prefix +X+Y). -
Computes the contract salt from a WebAuthn credential ID.
The salt is used during contract-address derivation so each credential ID maps to a unique smart-account contract address. The salt is the SHA-256 hash of the credential ID.
Declaration
Swift
public static func getContractSalt(credentialId: Data) -> DataParameters
credentialIdWebAuthn credential ID.
Return Value
32-byte SHA-256 digest of the credential ID.
-
Derives the smart-account contract address from a credential ID and deployer.
Computes the deterministic contract address that will be created when deploying a smart-account contract with the given credential ID from the specified deployer account on the specified network.
Algorithm:
salt = SHA-256(credentialId) deployerAddress = SCAddress::Account(deployerPublicKey) networkId = SHA-256(networkPassphrase as UTF-8) preimage = HashIDPreimage::ContractID { networkId, contractIDPreimage: ContractIDPreimage::FromAddress { address: deployerAddress, salt: Uint256(salt) } } contractIdBytes = SHA-256(XDR_encode(preimage)) contractId = StrKey.encodeContractId(contractIdBytes)Throws
SmartAccountValidationException.InvalidAddresswhen the deployer key is invalid,SmartAccountValidationException.InvalidInputwhen contract-ID encoding fails, orSmartAccountTransactionException.SigningFailedwhen XDR encoding fails.Declaration
Swift
public static func deriveContractAddress( credentialId: Data, deployerPublicKey: String, networkPassphrase: String ) throws -> StringParameters
credentialIdWebAuthn credential ID used to generate the salt.
deployerPublicKeyStellar account ID (
G…strkey) of the deployer.networkPassphraseNetwork passphrase.
Return Value
Contract address as a
C…strkey.
View on GitHub
Install in Dash