upsertAddress method
Store or update address metadata
Implementation
@override
Future<void> upsertAddress(String walletId, AddressMetadata metadata) async {
_ensureInitialized();
await _pool!.execute(
Sql.named('''
INSERT INTO addresses (
wallet_id, address, script_type, derivation_path, derivation_index,
chain, label, purpose, first_used_at, last_used_at,
usage_count, balance, created_at, is_watched,
type42_sender_public_key, type42_invoice_number,
type42_anchor_public_key, type42_anchor_context
) VALUES (
@walletId, @address, @scriptType, @derivationPath, @derivationIndex,
@chain, @label, @purpose, @firstUsedAt, @lastUsedAt,
@usageCount, @balance, @createdAt, @isWatched,
@type42SenderPublicKey, @type42InvoiceNumber, @type42AnchorPublicKey, @type42AnchorContext
)
ON CONFLICT (wallet_id, address) DO UPDATE SET
script_type = COALESCE(@scriptType, addresses.script_type),
derivation_path = COALESCE(@derivationPath, addresses.derivation_path),
derivation_index = COALESCE(@derivationIndex, addresses.derivation_index),
chain = @chain,
label = @label,
purpose = @purpose,
first_used_at = COALESCE(addresses.first_used_at, @firstUsedAt),
last_used_at = @lastUsedAt,
usage_count = @usageCount,
balance = @balance,
is_watched = @isWatched,
type42_sender_public_key = @type42SenderPublicKey,
type42_invoice_number = @type42InvoiceNumber,
type42_anchor_public_key = @type42AnchorPublicKey,
type42_anchor_context = @type42AnchorContext
'''),
parameters: {
'walletId': walletId,
'address': metadata.address,
'scriptType': metadata.scriptType,
'derivationPath': metadata.derivationPath,
'derivationIndex': metadata.derivationIndex,
'chain': metadata.chain?.index,
'type42SenderPublicKey': metadata.type42?.senderPublicKey,
'type42InvoiceNumber': metadata.type42?.invoiceNumber,
'type42AnchorPublicKey': metadata.type42?.anchorPublicKey,
'type42AnchorContext': metadata.type42?.anchorContext,
'label': metadata.label,
'purpose': metadata.purpose,
'firstUsedAt': metadata.firstUsedAt,
'lastUsedAt': metadata.lastUsedAt,
'usageCount': metadata.usageCount,
'balance': metadata.balance.toInt(),
// The address's own creation time (it used to be the store time),
// so the newest-first order matches the other backends.
'createdAt': metadata.createdAt,
'isWatched': metadata.isWatched,
},
);
}