-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.go
More file actions
87 lines (79 loc) · 2.53 KB
/
Copy pathcodec.go
File metadata and controls
87 lines (79 loc) · 2.53 KB
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
package urma
import (
"encoding/json"
"fmt"
"github.com/invopop/jsonschema"
)
// ValidateClaimSize checks that a serialized UrmaClaim fits on-chain when
// encoded as an unsigned claim value envelope. The total on-chain size is:
//
// ClaimScriptOverhead + valuePushPrefix + EnvelopeUnsignedOverhead + JSON
//
// where valuePushPrefix depends on the envelope size (1-3 bytes per the
// canonical push encoding in lbcd's script builder).
func ValidateClaimSize(claim UrmaClaim) error {
data, err := EncodeClaim(claim)
if err != nil {
return err
}
total := ClaimScriptOverhead + ValuePushSize(EnvelopeUnsignedOverhead+len(data)) + EnvelopeUnsignedOverhead + len(data)
if total > MaxClaimScriptSize {
return fmt.Errorf("claim size %d exceeds maximum %d", total, MaxClaimScriptSize)
}
return nil
}
// ValidateClaimSizeSigned checks that a serialized UrmaClaim fits on-chain
// when encoded as a signed claim value envelope. The total on-chain size is:
//
// ClaimScriptOverhead + valuePushPrefix + EnvelopeSignedOverhead + JSON
//
// The signed envelope adds EnvelopeSignedOverhead (85 bytes) for the version
// byte, channel ClaimID, and signature.
func ValidateClaimSizeSigned(claim UrmaClaim) error {
data, err := EncodeClaim(claim)
if err != nil {
return err
}
total := ClaimScriptOverhead + ValuePushSize(EnvelopeSignedOverhead+len(data)) + EnvelopeSignedOverhead + len(data)
if total > MaxClaimScriptSize {
return fmt.Errorf("signed claim size %d exceeds maximum %d", total, MaxClaimScriptSize)
}
return nil
}
// ValuePushSize returns the number of bytes the canonical script push prefix
// occupies for a data element of the given length, matching lbcd's
// canonicalDataSize logic.
func ValuePushSize(dataLen int) int {
switch {
case dataLen == 0:
return 1
case dataLen < 0x4c: // OP_PUSHDATA1
return 1
case dataLen <= 0xff:
return 2
case dataLen <= 0xffff:
return 3
default:
return 5
}
}
// EncodeClaim serializes a UrmaClaim to JSON.
func EncodeClaim(claim UrmaClaim) ([]byte, error) {
return json.Marshal(claim)
}
// DecodeClaim deserializes a UrmaClaim from JSON.
func DecodeClaim(data []byte) (UrmaClaim, error) {
var claim UrmaClaim
if err := json.Unmarshal(data, &claim); err != nil {
return UrmaClaim{}, fmt.Errorf("decode claim: %w", err)
}
return claim, nil
}
// GenerateSchema generates a JSON Schema for the UrmaClaim type.
func GenerateSchema() ([]byte, error) {
reflector := jsonschema.Reflector{
DoNotReference: true,
}
schema := reflector.Reflect(UrmaClaim{})
return json.MarshalIndent(schema, "", " ")
}