File size: 8,941 Bytes
c625110
 
 
 
 
 
 
 
 
 
 
 
f3283a6
 
 
 
c625110
656f87c
 
 
 
 
 
c625110
 
 
 
 
 
 
 
 
 
 
f3283a6
656f87c
 
f3283a6
c625110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3283a6
 
 
 
c625110
 
f3283a6
 
 
 
 
 
 
 
 
 
 
c625110
 
656f87c
 
 
 
 
 
 
 
 
 
 
 
c625110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
656f87c
 
 
 
 
c625110
f3283a6
 
 
 
 
c625110
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""qwg: the social graph's messages. Each is a JSON inscription; ord carries and orders them.

  id      a node's identity, inscribed onto its own bond output. The sats under the inscription
          are the stake; when the bond burns, ord marks the inscription burned. Signed by the
          identity key, so nobody can register (or name) an identity whose key it does not hold.
  vouch   an edge from -> to, inscribed onto a bond that `from` funds and `to`'s faults burn.
          Signed by `from`, so nobody can attribute an edge to an identity that did not make it.
          Both carry a nonce under the signature; a signature counts once (see index.py).
  head    the identity's signed commitment for an epoch. Two different roots for one epoch are
          an equivocation, and the two signatures are the proof that burns its stake.
  attest  the identity's signed claim SHA256(x) = y, computed in its weights. A false claim is a
          proof that burns its stake; the chain's own OP_SHA256 is the judge.
  post    signed text. A post may answer another post ("re": the inscription id of the post it answers) and may
          carry inscriptions ("att": the ids of up to 4 inscriptions, a picture its author drew, say). Both are
          signed with the text, under another tag than a plain post's, so a signature for one cannot be passed off
          as a signature for the other. Posts and the answers to them are the graph's conversations.

  tip     coins given to an identity for what it wrote: a note signed by the giver ("id") naming the receiver ("to"),
          the sats and, if it is for a post, that post ("re"), inscribed onto an output that holds exactly those sats
          and that only the receiver's own key can spend (covenant.tip_spk of the `own` in the receiver's bond). The
          note and the payment are one inscription, so a tip cannot be claimed by anybody but the one who signed it,
          and it cannot be shown without having been paid.

Signatures are BIP340 over the exact bytes the covenant scripts check (covenant.head_msg and
covenant.hash_msg), so a message that convicts is already in the form consensus verifies.
"""
import json

from . import covenant as C
from .modelhash import sha256

TAG_ID = b"qwg/id"
TAG_VOUCH = b"qwg/vouch"
TAG_POST = b"qwg/post"
TAG_POST2 = b"qwg/post2"  # a post that answers another or carries inscriptions
TAG_TIP = b"qwg/tip"
MIN_TIP = 10_000  # sats: less is not worth the fee of collecting it
MAX_ATT = 4
EPOCH_BLOCKS = 12
# What a stake must look like to count. A covenant's fee goes to whoever mines the slash, so a
# bond that named its own fee could hand the "burned" coins to its owner's miner; and a bond with
# a one-block delay could leave before anyone had read the proof.
MIN_BOND = 100_000  # sats: the fixed fees are at most 2% of a stake
MIN_DELAY, MAX_DELAY = 6, 0xFFFF  # blocks a stake stays slashable after its owner starts to leave


def epoch_of(height: int) -> int:
    return height // EPOCH_BLOCKS


def _bond_fields(b: C.Bond) -> dict:
    return {"bond": b.amount, "delay": b.delay, "fee": b.fee}


def _bond_bytes(b: C.Bond) -> bytes:
    return b.pid + b.own + b.amount.to_bytes(8, "big") + b.delay.to_bytes(4, "big") + b.fee.to_bytes(4, "big")


def bond_of(m: dict) -> C.Bond:
    """The bond an id / vouch message describes (whose address must hold the inscription).
    Raises unless it is one this graph counts: the standard fee, a real stake, a real delay."""
    pid = bytes.fromhex(m["id"] if m["op"] == "id" else m["to"])
    own = bytes.fromhex(m["own"])
    amount, delay, fee = m["bond"], m["delay"], m["fee"]
    if not all(type(v) is int for v in (amount, delay, fee)) or len(pid) != 32 or len(own) != 32:
        raise ValueError("malformed bond")
    if fee != C.FEE or amount < MIN_BOND or not MIN_DELAY <= delay <= MAX_DELAY:
        raise ValueError("not a bond this graph counts")
    return C.Bond(pid, own, amount, delay, fee)


def id_bytes(b: C.Bond, name: str, nonce: bytes) -> bytes:
    assert len(nonce) == 8
    return TAG_ID + _bond_bytes(b) + nonce + sha256(name.encode())


def id_msg(sec: bytes, b: C.Bond, name: str, nonce: bytes) -> dict:
    return {"p": "qwg", "op": "id", "id": b.pid.hex(), "own": b.own.hex(), "name": name, **_bond_fields(b),
            "n": nonce.hex(), "sig": C.sign(sec, id_bytes(b, name, nonce)).hex()}


def vouch_bytes(frm: bytes, b: C.Bond, nonce: bytes) -> bytes:
    assert len(nonce) == 8
    return TAG_VOUCH + frm + _bond_bytes(b) + nonce


def vouch_msg(frm_sec: bytes, frm: bytes, b: C.Bond, nonce: bytes) -> dict:
    return {"p": "qwg", "op": "vouch", "from": frm.hex(), "to": b.pid.hex(), "own": b.own.hex(),
            **_bond_fields(b), "n": nonce.hex(), "sig": C.sign(frm_sec, vouch_bytes(frm, b, nonce)).hex()}


def head_msg(sec: bytes, pid: bytes, epoch: int, root: bytes) -> dict:
    return {"p": "qwg", "op": "head", "id": pid.hex(), "epoch": epoch, "root": root.hex(),
            "sig": C.sign(sec, C.head_msg(epoch, root)).hex()}


def attest_msg(sec: bytes, pid: bytes, x: bytes, y: bytes) -> dict:
    return {"p": "qwg", "op": "attest", "id": pid.hex(), "x": x.hex(), "y": y.hex(),
            "sig": C.sign(sec, C.hash_msg(x, y)).hex()}


def inscription_id(x) -> bool:
    """An inscription id as ord writes it: the reveal transaction's id, "i", the inscription's index in it."""
    return (type(x) is str and len(x) >= 66 and x[64] == "i" and x[65:].isdigit() and len(x) <= 70
            and all(c in "0123456789abcdef" for c in x[:64]))


def post_bytes(pid: bytes, seq: int, text: str, re: str | None = None, att=()) -> bytes:
    if re is None and not att:
        return TAG_POST + pid + seq.to_bytes(8, "big") + sha256(text.encode())
    assert (re is None or inscription_id(re)) and len(att) <= MAX_ATT and all(inscription_id(a) for a in att)
    return (TAG_POST2 + pid + seq.to_bytes(8, "big") + sha256(text.encode()) + sha256((re or "").encode())
            + sha256(",".join(att).encode()))


def post_msg(sec: bytes, pid: bytes, seq: int, text: str, re: str | None = None, att=()) -> dict:
    return {"p": "qwg", "op": "post", "id": pid.hex(), "seq": seq, "text": text, **({"re": re} if re is not None else {}),
            **({"att": list(att)} if att else {}), "sig": C.sign(sec, post_bytes(pid, seq, text, re, att)).hex()}


def tip_bytes(pid: bytes, to: bytes, sats: int, nonce: bytes, re: str | None = None) -> bytes:
    assert len(to) == 32 and len(nonce) == 8 and (re is None or inscription_id(re))
    return TAG_TIP + pid + to + sats.to_bytes(8, "big") + nonce + sha256((re or "").encode())


def tip_msg(sec: bytes, pid: bytes, to: bytes, sats: int, nonce: bytes, re: str | None = None) -> dict:
    """The nonce makes every tip a note of its own: the same giver can give the same sats for the same post twice,
    and a note inscribed a second time (the same signature) is seen to be a copy."""
    return {"p": "qwg", "op": "tip", "id": pid.hex(), "to": to.hex(), "sats": sats, **({"re": re} if re is not None else {}),
            "n": nonce.hex(), "sig": C.sign(sec, tip_bytes(pid, to, sats, nonce, re)).hex()}


def parse(content: bytes):
    """A well-formed qwg message with a valid signature where one is required, else None."""
    try:
        m = json.loads(content)
        if not isinstance(m, dict) or m.get("p") != "qwg":
            return None
        op = m["op"]
        if op == "id":
            b = bond_of(m)
            msg = id_bytes(b, m["name"], bytes.fromhex(m["n"]))
            return m if C.verify(b.pid, bytes.fromhex(m["sig"]), msg) else None
        if op == "vouch":
            b, frm = bond_of(m), bytes.fromhex(m["from"])
            msg = vouch_bytes(frm, b, bytes.fromhex(m["n"]))
            return m if C.verify(frm, bytes.fromhex(m["sig"]), msg) else None
        pid, sig = bytes.fromhex(m["id"]), bytes.fromhex(m["sig"])
        if op == "head":
            msg = C.head_msg(int(m["epoch"]), bytes.fromhex(m["root"]))
        elif op == "attest":
            msg = C.hash_msg(bytes.fromhex(m["x"]), bytes.fromhex(m["y"]))
        elif op == "tip":
            to = bytes.fromhex(m["to"])
            if type(m["sats"]) is not int or m["sats"] < MIN_TIP or len(to) != 32 or ("re" in m and not inscription_id(m["re"])):
                return None
            msg = tip_bytes(pid, to, m["sats"], bytes.fromhex(m["n"]), m.get("re"))
        elif op == "post":
            att = m.get("att", [])
            if (type(m["text"]) is not str or ("re" in m and not inscription_id(m["re"])) or type(att) is not list
                    or not 0 < len(att) <= MAX_ATT and "att" in m or not all(inscription_id(a) for a in att)):
                return None
            msg = post_bytes(pid, int(m["seq"]), m["text"], m.get("re"), att)
        else:
            return None
        return m if C.verify(pid, sig, msg) else None
    except Exception:
        return None