Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit f61462e

Browse files
authoredDec 2, 2021
scripts-dev/sign_json: support for signing events (#11486)
1 parent a6f1a3a commit f61462e

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed
 

‎changelog.d/11486.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Extend the `scripts-dev/sign_json` script to support signing events.

‎scripts-dev/federation_client.py

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717

18+
19+
"""
20+
Script for signing and sending federation requests.
21+
22+
Some tips on doing the join dance with this:
23+
24+
room_id=...
25+
user_id=...
26+
27+
# make_join
28+
federation_client.py "/_matrix/federation/v1/make_join/$room_id/$user_id?ver=5" > make_join.json
29+
30+
# sign
31+
jq -M .event make_join.json | sign_json --sign-event-room-version=$(jq -r .room_version make_join.json) -o signed-join.json
32+
33+
# send_join
34+
federation_client.py -X PUT "/_matrix/federation/v2/send_join/$room_id/x" --body $(<signed-join.json) > send_join.json
35+
"""
36+
1837
import argparse
1938
import base64
2039
import json

‎scripts-dev/sign_json

+23-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import yaml
2222
from signedjson.key import read_signing_keys
2323
from signedjson.sign import sign_json
2424

25+
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
26+
from synapse.crypto.event_signing import add_hashes_and_signatures
2527
from synapse.util import json_encoder
2628

2729

@@ -68,6 +70,16 @@ Example usage:
6870
),
6971
)
7072

73+
parser.add_argument(
74+
"--sign-event-room-version",
75+
type=str,
76+
help=(
77+
"Sign the JSON as an event for the given room version, rather than raw JSON. "
78+
"This means that we will add a 'hashes' object, and redact the event before "
79+
"signing."
80+
),
81+
)
82+
7183
input_args = parser.add_mutually_exclusive_group()
7284

7385
input_args.add_argument("input_data", nargs="?", help="Raw JSON to be signed.")
@@ -116,7 +128,17 @@ Example usage:
116128
print("Input json was not an object", file=sys.stderr)
117129
sys.exit(1)
118130

119-
sign_json(obj, args.server_name, keys[0])
131+
if args.sign_event_room_version:
132+
room_version = KNOWN_ROOM_VERSIONS.get(args.sign_event_room_version)
133+
if not room_version:
134+
print(
135+
f"Unknown room version {args.sign_event_room_version}", file=sys.stderr
136+
)
137+
sys.exit(1)
138+
add_hashes_and_signatures(room_version, obj, args.server_name, keys[0])
139+
else:
140+
sign_json(obj, args.server_name, keys[0])
141+
120142
for c in json_encoder.iterencode(obj):
121143
args.output.write(c)
122144
args.output.write("\n")

0 commit comments

Comments
 (0)
This repository has been archived.