message.gno
3.96 Kb · 111 lines
1package qrc
2
3// Domain opens every message signed with a commit key, so that such a
4// signature can never be mistaken for anything else.
5const Domain = "QuantumRecoveryCommit/v1"
6
7// Action tells what a signed message asks for.
8type Action uint8
9
10const (
11 // ActionBind registers new public key bytes for the account.
12 ActionBind Action = 1
13 // ActionRotate replaces the account's commit by a new one.
14 ActionRotate Action = 2
15 // ActionAuthorize approves a payload defined by another realm.
16 ActionAuthorize Action = 3
17)
18
19// ID identifies one commit: the n-th commit of an account, in one registry,
20// on one chain. Messages carry it so that a signature made for one commit is
21// worthless for any other, and signers derive their keys from it so that no
22// one-time key is ever shared between two commits.
23type ID struct {
24 ChainID string // chain the registry runs on
25 Registry string // package path of the realm that stores the commit
26 Account string // address of the account, as the chain writes it (lower-case bech32)
27 Generation uint32 // number of times the account replaced its commit
28}
29
30// Bytes returns the canonical encoding of id: each string preceded by its
31// length as a big-endian uint16, then the generation as a big-endian uint32.
32func (id ID) Bytes() []byte {
33 b := make([]byte, 0, 10+len(id.ChainID)+len(id.Registry)+len(id.Account))
34 b = appendString(b, id.ChainID)
35 b = appendString(b, id.Registry)
36 b = appendString(b, id.Account)
37 return appendUint32(b, id.Generation)
38}
39
40// BindMessage returns the bytes to sign, with one-time key number leaf, in
41// order to register key as the new public key of the account. keyType is a
42// free label naming the scheme key belongs to.
43//
44// header || uint16 len(keyType) || keyType || uint32 len(key) || key
45//
46// The header common to all messages is
47//
48// Domain || 0x00 || action || id.Bytes() || uint32 leaf
49//
50// All integers are big-endian. Every variable-length field carries its
51// length, so two different requests never produce the same bytes.
52func BindMessage(id ID, leaf uint32, keyType string, key []byte) []byte {
53 b := header(id, leaf, ActionBind, 6+len(keyType)+len(key))
54 b = appendString(b, keyType)
55 return appendBytes(b, key)
56}
57
58// RotateMessage returns the bytes to sign, with one-time key number leaf, in
59// order to replace the commit id by a new one made of the two given roots
60// and tree height.
61//
62// header || rootSHA256 || rootKeccak256 || uint8 height
63func RotateMessage(id ID, leaf uint32, rootSHA256, rootKeccak256 [HashSize]byte, height int) []byte {
64 if height < MinHeight || height > MaxHeight {
65 panic(ErrHeight.Error())
66 }
67 b := header(id, leaf, ActionRotate, 2*HashSize+1)
68 b = append(b, rootSHA256[:]...)
69 b = append(b, rootKeccak256[:]...)
70 return append(b, byte(height))
71}
72
73// AuthorizeMessage returns the bytes to sign, with one-time key number leaf,
74// in order to approve payload on behalf of the account. app is the package
75// path of the realm the approval is meant for; no other realm can use it.
76//
77// header || uint16 len(app) || app || uint32 len(payload) || payload
78func AuthorizeMessage(id ID, leaf uint32, app string, payload []byte) []byte {
79 b := header(id, leaf, ActionAuthorize, 6+len(app)+len(payload))
80 b = appendString(b, app)
81 return appendBytes(b, payload)
82}
83
84func header(id ID, leaf uint32, action Action, bodySize int) []byte {
85 idBytes := id.Bytes()
86 b := make([]byte, 0, len(Domain)+2+len(idBytes)+4+bodySize)
87 b = append(b, []byte(Domain)...)
88 b = append(b, 0x00, byte(action))
89 b = append(b, idBytes...)
90 return appendUint32(b, leaf)
91}
92
93func appendString(b []byte, s string) []byte {
94 if len(s) > 0xffff {
95 panic("qrc: string too long")
96 }
97 b = append(b, byte(len(s)>>8), byte(len(s)))
98 return append(b, []byte(s)...)
99}
100
101func appendBytes(b []byte, p []byte) []byte {
102 if uint64(len(p)) > 0xffffffff {
103 panic("qrc: payload too long")
104 }
105 b = appendUint32(b, uint32(len(p)))
106 return append(b, p...)
107}
108
109func appendUint32(b []byte, v uint32) []byte {
110 return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
111}