How to Build an Embeddable Bitcoin Donation Widget
An embedded donation widget can display a public receiving address without giving the host page wallet API credentials.
Implementation approach
Serve a minimal HTML fragment from trusted configuration. Escape display text and restrict address characters. For cross-site embedding, use a dedicated iframe origin and a deliberate frame-ancestors policy. The example renders markup only, not a hosted service or QR image.
Source code
1from html import escape
2import re
3
4def widget(address, label):
5 if not re.fullmatch(r'[a-zA-Z0-9]{14,90}',address): raise ValueError('unsafe address')
6 return '<section><h2>'+escape(label)+'</h2><code>'+escape(address)+'</code><p><a href="bitcoin:'+address+'">Open wallet</a></p></section>'
7
8if __name__ == '__main__':
9 html=widget('tb1qsyntheticfixture','<script>alert(1)</script>')
10 assert '<script>' not in html and '<script>' in htmlLocal validation
Run stage23-donation-widget.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
