-
Notifications
You must be signed in to change notification settings - Fork 19
/
Test.sol
157 lines (134 loc) · 3.31 KB
/
Test.sol
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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.9;
import {
MessageDataCodec,
MessageData,
MessageType,
ReactionType,
CastId
} from './protobufs/message.proto.sol';
import {
Blake3
} from './libraries/Blake3.sol';
import {
Ed25519
} from './libraries/Ed25519.sol';
contract Test {
event MessageCastAddVerified(
uint64 fid,
string text,
uint64[] mentions
);
event MessageReactionAddVerified(
uint64 fid,
ReactionType reaction_type,
uint64 target_fid,
bytes target_hash
);
event MessageFrameActionBodyVerified(
uint64 fid,
uint32 button_index,
uint64 target_fid,
bytes target_hash,
bytes url,
bytes input_text
);
error InvalidSignature();
error InvalidEncoding();
error InvalidMessageType();
function _verifyMessage(
bytes32 public_key,
bytes32 signature_r,
bytes32 signature_s,
bytes memory message
) internal pure returns(MessageData memory) {
// Calculate Blake3 hash of FC message (first 20 bytes)
bytes memory message_hash = Blake3.hash(message, 20);
// Verify signature
bool valid = Ed25519.verify(
public_key,
signature_r,
signature_s,
message_hash
);
if (!valid) {
revert InvalidSignature();
}
(
bool success,
,
MessageData memory message_data
) = MessageDataCodec.decode(0, message, uint64(message.length));
if (!success) {
revert InvalidEncoding();
}
return message_data;
}
function verifyCastAddMessage(
bytes32 public_key,
bytes32 signature_r,
bytes32 signature_s,
bytes memory message
) external {
MessageData memory message_data = _verifyMessage(
public_key,
signature_r,
signature_s,
message
);
if (message_data.type_ != MessageType.MESSAGE_TYPE_CAST_ADD) {
revert InvalidMessageType();
}
emit MessageCastAddVerified(
message_data.fid,
message_data.cast_add_body.text,
message_data.cast_add_body.mentions
);
}
function verifyReactionAddMessage(
bytes32 public_key,
bytes32 signature_r,
bytes32 signature_s,
bytes memory message
) external {
MessageData memory message_data = _verifyMessage(
public_key,
signature_r,
signature_s,
message
);
if (message_data.type_ != MessageType.MESSAGE_TYPE_REACTION_ADD) {
revert InvalidMessageType();
}
emit MessageReactionAddVerified(
message_data.fid,
message_data.reaction_body.type_,
message_data.reaction_body.target_cast_id.fid,
message_data.reaction_body.target_cast_id.hash_
);
}
function verifyFrameActionBodyMessage(
bytes32 public_key,
bytes32 signature_r,
bytes32 signature_s,
bytes memory message
) external {
MessageData memory message_data = _verifyMessage(
public_key,
signature_r,
signature_s,
message
);
if (message_data.type_ != MessageType.MESSAGE_TYPE_FRAME_ACTION) {
revert InvalidMessageType();
}
emit MessageFrameActionBodyVerified(
message_data.fid,
message_data.frame_action_body.button_index,
message_data.frame_action_body.cast_id.fid,
message_data.frame_action_body.cast_id.hash_,
message_data.frame_action_body.url,
message_data.frame_action_body.input_text
);
}
}