How to Generate Unique Bitcoin Donation Addresses for Each Session
Separate donation addresses help attribute incoming payments without requiring donor accounts.
Implementation approach
Allocate addresses through the server-side WAB RECEIVE operation (POST /api/v5/btc/wallet/receive with net and type). Persist the returned address before displaying it. This example implements the local mapping only; it does not allocate addresses or resolve ambiguous API timeouts. Use a persistent database and authenticated internal allocation worker.
Source code
1import sqlite3
2
3def init(db):
4 db.execute('CREATE TABLE sessions (session TEXT PRIMARY KEY, network TEXT NOT NULL, address TEXT NOT NULL, UNIQUE(network,address))')
5
6def bind(db, session, network, address):
7 if network not in ('main','test') or not session or not address:
8 raise ValueError('invalid mapping')
9 with db:
10 db.execute('INSERT INTO sessions VALUES (?,?,?) ON CONFLICT(session) DO NOTHING', (session,network,address))
11 row = db.execute('SELECT network,address FROM sessions WHERE session=?',(session,)).fetchone()
12 if row != (network,address): raise ValueError('session conflict')
13 return row
14
15if __name__ == '__main__':
16 db=sqlite3.connect(':memory:'); init(db)
17 assert bind(db,'s1','test','fixture1') == bind(db,'s1','test','fixture1')
18 try: bind(db,'s2','test','fixture1')
19 except sqlite3.IntegrityError: pass
20 else: raise AssertionError('address reused')
21 db.close()Local validation
Run stage23-donation-address-session.py with Python 3. Fixtures are not valid Bitcoin addresses. This is an educational example, not a production donation service. No SEND operation is included.
Related documentation
https://thamer.org/api
