import asyncio
from fabric import Fabric, RecordSet, Record, sign_schnorr
from fabric import CertificateChain, MessageBuilder, ChainProof, SIG_PRIMARY_ZONE
async def example_resolve_intro():
fabric = Fabric()
resolved = await fabric.resolve("alice@bitcoin")
async def example_resolve():
fabric = Fabric()
resolved = await fabric.resolve("alice@bitcoin")
if resolved is None:
print("handle not found")
return
print(f"Handle found: {resolved.zone.handle}")
async def example_trust_and_verification():
fabric = Fabric()
resolved = await fabric.resolve("alice@bitcoin")
fabric.badge(resolved)
qr = "veritas://scan?id=14ef902621df01bdeee0b23fedf67458563a20df600af8979a4748dcd9d1b9f9"
await fabric.trust_from_qr(qr)
fabric.badge(resolved)
await fabric.semi_trust_from_qr(qr)
fabric.trusted() fabric.semi_trusted() fabric.observed()
fabric.clear_trusted()
fabric.clear_semi_trusted()
async def example_unpack_records():
fabric = Fabric()
resolved = await fabric.resolve("alice@bitcoin")
records = resolved.zone.records.unpack()
for record in records:
if record.type == "txt":
print(f"txt {record.key}={', '.join(record.value)}")
elif record.type == "addr":
print(f"addr {record.key}={', '.join(record.value)}")
async def example_resolve_all():
fabric = Fabric()
batch = await fabric.resolve_all(["alice@bitcoin", "bob@bitcoin"])
for zone in batch.zones:
print(f"{zone.handle}: {zone.sovereignty}")
def example_pack_records():
records = RecordSet.pack([
Record.seq(1),
Record.txt("website", ["https://example.com"]),
Record.addr("btc", ["bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"]),
Record.addr("nostr", [
"npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6",
"wss://relay.example.com",
]),
])
async def example_publish():
fabric = Fabric()
secret_key = bytes.fromhex(
"0000000000000000000000000000000000000000000000000000000000000001"
)
rs = RecordSet.pack([
Record.seq(1),
Record.txt("website", ["https://example.com"]),
Record.addr("btc", ["bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"]),
])
cert = await fabric.export("alice@bitcoin")
await fabric.publish(cert, rs, secret_key, primary=True)
async def example_resolve_by_id():
fabric = Fabric()
resolved = await fabric.resolve_by_id("num1qx8dtlzq...")
if resolved is None:
print("handle not found")
return
print(f"Handle found: {resolved.zone.handle}")
async def example_search_addr():
fabric = Fabric()
batch = await fabric.search_addr("nostr", "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6")
for zone in batch.zones:
print(f"{zone.handle}: {zone.sovereignty}")
async def example_message_builder():
fabric = Fabric()
secret_key = bytes.fromhex(
"0000000000000000000000000000000000000000000000000000000000000001"
)
cert_bytes = await fabric.export("alice@bitcoin")
records = RecordSet.pack([
Record.seq(1),
Record.addr("btc", ["bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"]),
])
cert = CertificateChain.from_slice(cert_bytes)
builder = MessageBuilder()
builder.add_handle(cert, records)
proof_bytes = await fabric.prove(builder.chain_proof_request())
proof = ChainProof.from_slice(proof_bytes)
msg, unsigned = builder.build(proof)
for u in unsigned:
u.flags |= SIG_PRIMARY_ZONE
sig = sign_schnorr(u.signing_id(), secret_key)
msg.set_records(u.canonical, u.pack_sig(sig))
await fabric.broadcast(msg.to_bytes())
async def main():
await example_resolve()
try:
await example_trust_and_verification()
except Exception as e:
print(f"verification example failed (expected): {e}")
await example_unpack_records()
await example_resolve_all()
example_pack_records()
await example_publish()
await example_resolve_by_id()
await example_search_addr()
await example_message_builder()
print("Done!")
if __name__ == "__main__":
asyncio.run(main())