@adapt-toolkit/mufl 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/LICENSE +172 -0
  2. package/LICENSE.fsl +105 -0
  3. package/NOTICE +25 -0
  4. package/README.md +70 -0
  5. package/bin/mufl-compile.js +4 -0
  6. package/bin/mufl.js +4 -0
  7. package/lib/launcher.js +60 -0
  8. package/lib/paths.js +26 -0
  9. package/meta/meta.mm +2666 -0
  10. package/mufl_stdlib/config.mufl +86 -0
  11. package/mufl_stdlib/continuation.mm +51 -0
  12. package/mufl_stdlib/control_packet.mu +184 -0
  13. package/mufl_stdlib/cryptography/address_document.mm +333 -0
  14. package/mufl_stdlib/cryptography/address_document_types.mm +16 -0
  15. package/mufl_stdlib/cryptography/attestation/attestation_document.mm +68 -0
  16. package/mufl_stdlib/cryptography/attestation/config.mufl +12 -0
  17. package/mufl_stdlib/cryptography/attestation/container_associated_data.mm +12 -0
  18. package/mufl_stdlib/cryptography/attestation/control_packet_identity_proof_document.mm +36 -0
  19. package/mufl_stdlib/cryptography/attestation/identity_proof_document_impl.mm +57 -0
  20. package/mufl_stdlib/cryptography/attestation/identity_proof_document_types.mm +45 -0
  21. package/mufl_stdlib/cryptography/attestation/node_description.mm +9 -0
  22. package/mufl_stdlib/cryptography/attestation/platform_dependent_attestation_document/browser/browser_attestation_document.mm +25 -0
  23. package/mufl_stdlib/cryptography/attestation/platform_dependent_attestation_document/browser/config.mufl +12 -0
  24. package/mufl_stdlib/cryptography/attestation/platform_dependent_attestation_document/config.mufl +12 -0
  25. package/mufl_stdlib/cryptography/attestation/platform_dependent_attestation_document/native/config.mufl +12 -0
  26. package/mufl_stdlib/cryptography/attestation/platform_dependent_attestation_document/native/native_attestation_document.mm +25 -0
  27. package/mufl_stdlib/cryptography/attestation/platform_dependent_attestation_document/platform_dependent_attestation_document.mm +57 -0
  28. package/mufl_stdlib/cryptography/authorizing_container.mm +21 -0
  29. package/mufl_stdlib/cryptography/config.mufl +12 -0
  30. package/mufl_stdlib/cryptography/encrypted_channel.mm +95 -0
  31. package/mufl_stdlib/cryptography/key_storage.mm +391 -0
  32. package/mufl_stdlib/cryptography/key_storage_api.mm +17 -0
  33. package/mufl_stdlib/cryptography/key_utils.mm +20 -0
  34. package/mufl_stdlib/cryptography/peer_container.mu +17 -0
  35. package/mufl_stdlib/cryptography/registration_proof.mm +40 -0
  36. package/mufl_stdlib/current_transaction_info.mm +78 -0
  37. package/mufl_stdlib/identity_proof_document.mm +21 -0
  38. package/mufl_stdlib/integration_test_api.mm +39 -0
  39. package/mufl_stdlib/lists.mm +84 -0
  40. package/mufl_stdlib/permissions.mm +219 -0
  41. package/mufl_stdlib/platform_type_id.mm +20 -0
  42. package/mufl_stdlib/random.mm +50 -0
  43. package/mufl_stdlib/rslt.mm +32 -0
  44. package/mufl_stdlib/transaction_message_decoder.mm +22 -0
  45. package/mufl_stdlib/transaction_queue.mm +60 -0
  46. package/mufl_stdlib/vector.mm +115 -0
  47. package/mufl_stdlib/verification.mm +35 -0
  48. package/package.json +43 -0
  49. package/prebuilds/linux-x64/mufl +0 -0
  50. package/prebuilds/linux-x64/mufl-compile +0 -0
  51. package/transactions/__t_wrapper.mm +169 -0
  52. package/transactions/config.mufl +22 -0
  53. package/transactions/transaction.mm +200 -0
@@ -0,0 +1,86 @@
1
+ config script {
2
+ (
3
+ $exports->(
4
+ $libraries->(
5
+ $continuation->#"continuation.mm",
6
+ $current_transaction_info->#"current_transaction_info.mm",
7
+ $identity_proof_document->#"identity_proof_document.mm",
8
+ $lists->#"lists.mm",
9
+ $vector->#"vector.mm",
10
+ $platform_type_id->#"platform_type_id.mm",
11
+ $transaction_message_decoder->#"transaction_message_decoder.mm",
12
+ $transaction_queue->#"transaction_queue.mm",
13
+ $verification->#"verification.mm",
14
+ $address_document_types->#"cryptography/address_document_types.mm",
15
+ $address_document->#"cryptography/address_document.mm",
16
+ $encrypted_channel->#"cryptography/encrypted_channel.mm",
17
+ $authorizing_document->#"cryptography/authorizing_document.mm",
18
+ $authorizing_container->#"cryptography/authorizing_container.mm",
19
+ $key_storage_api->#"cryptography/key_storage_api.mm",
20
+ $key_storage->#"cryptography/key_storage.mm",
21
+ $key_utils->#"cryptography/key_utils.mm",
22
+ $registration_proof->#"cryptography/registration_proof.mm",
23
+ $attestation_document->#"cryptography/attestation/attestation_document.mm",
24
+ $container_associated_data->#"cryptography/attestation/container_associated_data.mm",
25
+ $control_packet_identity_proof_document->#"cryptography/attestation/control_packet_identity_proof_document.mm",
26
+ $identity_proof_document_impl->#"cryptography/attestation/identity_proof_document_impl.mm",
27
+ $identity_proof_document_types->#"cryptography/attestation/identity_proof_document_types.mm",
28
+ $node_description->#"cryptography/attestation/node_description.mm",
29
+ $platform_dependent_attestation_document->
30
+ #"cryptography/attestation/platform_dependent_attestation_document/platform_dependent_attestation_document.mm",
31
+ $browser_attestation_document->
32
+ #"cryptography/attestation/platform_dependent_attestation_document/browser/browser_attestation_document.mm",
33
+ $native_attestation_document->
34
+ #"cryptography/attestation/platform_dependent_attestation_document/native/native_attestation_document.mm",
35
+ $integration_test_api -> #"integration_test_api.mm",
36
+ $rslt -> #"rslt.mm",
37
+ $random -> #"random.mm",
38
+ $permissions -> #"permissions.mm",
39
+ ),
40
+ $applications->(
41
+ $control_packet->#"control_packet.mu",
42
+ $peer_container->#"cryptography/peer_container.mu"
43
+ ),
44
+ ),
45
+ $imports->(
46
+ $libraries->(
47
+ $__t_wrapper->#"../transactions/__t_wrapper.mm",
48
+ $transaction->#"../transactions/transaction.mm",
49
+ $continuation->#"continuation.mm",
50
+ $current_transaction_info->#"current_transaction_info.mm",
51
+ $identity_proof_document->#"identity_proof_document.mm",
52
+ $lists->#"lists.mm",
53
+ $vector->#"vector.mm",
54
+ $platform_type_id->#"platform_type_id.mm",
55
+ $transaction_message_decoder->#"transaction_message_decoder.mm",
56
+ $transaction_queue->#"transaction_queue.mm",
57
+ $verification->#"verification.mm",
58
+ $address_document_types->#"cryptography/address_document_types.mm",
59
+ $address_document->#"cryptography/address_document.mm",
60
+ $encrypted_channel->#"cryptography/encrypted_channel.mm",
61
+ $authorizing_document->#"cryptography/authorizing_document.mm",
62
+ $authorizing_container->#"cryptography/authorizing_container.mm",
63
+ $key_storage_api->#"cryptography/key_storage_api.mm",
64
+ $key_storage->#"cryptography/key_storage.mm",
65
+ $key_utils->#"cryptography/key_utils.mm",
66
+ $registration_proof->#"cryptography/registration_proof.mm",
67
+ $attestation_document->#"cryptography/attestation/attestation_document.mm",
68
+ $container_associated_data->#"cryptography/attestation/container_associated_data.mm",
69
+ $control_packet_identity_proof_document->#"cryptography/attestation/control_packet_identity_proof_document.mm",
70
+ $identity_proof_document_impl->#"cryptography/attestation/identity_proof_document_impl.mm",
71
+ $identity_proof_document_types->#"cryptography/attestation/identity_proof_document_types.mm",
72
+ $node_description->#"cryptography/attestation/node_description.mm",
73
+ $platform_dependent_attestation_document->
74
+ #"cryptography/attestation/platform_dependent_attestation_document/platform_dependent_attestation_document.mm",
75
+ $browser_attestation_document->
76
+ #"cryptography/attestation/platform_dependent_attestation_document/browser/browser_attestation_document.mm",
77
+ $native_attestation_document->
78
+ #"cryptography/attestation/platform_dependent_attestation_document/native/native_attestation_document.mm",
79
+ $rslt -> #"rslt.mm",
80
+ $random -> #"random.mm",
81
+ $permissions -> #"permissions.mm"
82
+ ),
83
+ $applications->(,)
84
+ )
85
+ ).
86
+ }
@@ -0,0 +1,51 @@
1
+ library continuation loads library current_transaction_info uses transactions
2
+ {
3
+ // add timeouts...
4
+
5
+ metadef main_function_t: global_id -> transaction::results::type.
6
+ metadef continuation_function_t: any.
7
+
8
+ hidden {
9
+ continuations is global_id->>any = (,).
10
+
11
+ continue_transaction = fn (_:($id->id: global_id, $args->args: any)) {
12
+ f = continuations id abort "Continuation not found. Id: " + id when is NIL.
13
+ delete continuations id.
14
+
15
+ return f args.
16
+ }
17
+ }
18
+
19
+ add_continuation = fn (f: continuation_function_t) -> global_id
20
+ {
21
+ id = _new_id "Continuation id.".
22
+ abort "Internal error: Continuation with given id already exists: " + id when continuations id != NIL.
23
+
24
+ continuations id -> f.
25
+
26
+ return id.
27
+ }
28
+
29
+ update_continuation = fn id: global_id, f: continuation_function_t
30
+ {
31
+ continuations id -> f.
32
+ }
33
+
34
+ fn respond(_:($sender_id -> sender_id: global_id+, $continuation_id -> continuation_id: global_id, $args -> args: any)) -> transaction::action::type
35
+ {
36
+ if (sender_id == NIL)
37
+ {
38
+ sender_id -> current_transaction_info::get_external_envelope_or_abort() $from. // if sender id is not passed explicitly, extract it from the external envelope
39
+ }
40
+ return transaction::action::send sender_id? ($name -> "::continuation::continue_transaction", $targ -> ($id -> continuation_id, $args -> args)).
41
+ }
42
+
43
+ fn set_breakpoint (main_cb: main_function_t, continuation_cb: continuation_function_t) -> transaction::results::type
44
+ {
45
+ continuation_id = add_continuation continuation_cb.
46
+ return main_cb continuation_id.
47
+ }
48
+
49
+ trn continue_transaction args:($id->global_id, $args->any) = (continue_transaction args).
50
+
51
+ }
@@ -0,0 +1,184 @@
1
+ application control_packet loads libraries
2
+ identity_proof_document,
3
+ attestation_document,
4
+ native_attestation_document,
5
+ browser_attestation_document,
6
+ address_document,
7
+ continuation,
8
+ control_packet_identity_proof_document,
9
+ key_storage,
10
+ key_storage_api,
11
+ transaction_message_decoder,
12
+ current_transaction_info
13
+ uses transactions
14
+ {
15
+
16
+ control_packet_identity_proof_document::is_control_packet -> TRUE.
17
+
18
+
19
+
20
+ hidden
21
+ {
22
+
23
+ // Route the injected deserializer through the FILTERED _read_external_or_abort
24
+ // (ExternalMessageTypeFilter -- rejects D_Code at decode). The grabbed primitive feeds
25
+ // key_storage::decrypt_message's post-decrypt _read of peer crypto_messages (untrusted
26
+ // external ingress); binding it to the local name _read_or_abort keeps the injection below
27
+ // unchanged. Trusted program-load/code-serialization _read stays on the bare _read.
28
+ _read_or_abort = grab( _read_external_or_abort ).
29
+ key_storage::init ($_read_or_abort -> _read_or_abort ).
30
+
31
+ module types
32
+ {
33
+ switch_to_debug = 0.
34
+ switch_to_release = 1.
35
+ get_logs = 2.
36
+ add_packet = 3.
37
+ backup_packet = 4.
38
+ remove_packet = 5.
39
+ }
40
+
41
+ module policy_validator
42
+ {
43
+ metadef packet_info: ($id->global_id).
44
+
45
+ hidden
46
+ {
47
+ validators is int->>(packet_info->bool) = (,).
48
+ }
49
+
50
+
51
+ add_validator = fn (type_id: int, validator: (packet_info->bool))
52
+ {
53
+ abort "Validator for given action type is already set: " + (_str type_id) when validators type_id != NIL.
54
+ validators type_id -> validator.
55
+ }
56
+
57
+ get_validator = fn (type_id: int) -> (packet_info->bool)+
58
+ {
59
+ return validators type_id.
60
+ }
61
+
62
+ validate = fn (type_id: int, info: packet_info)
63
+ {
64
+ validator = get_validator type_id abort "Validator for given action type not found: " + (_str type_id) when is NIL.
65
+ return validator info.
66
+ }
67
+
68
+ add_validator types::add_packet fn (_: packet_info) = TRUE.
69
+ add_validator types::backup_packet fn (_: packet_info) = TRUE.
70
+ add_validator types::remove_packet fn (_:packet_info) = TRUE.
71
+ add_validator types::get_logs fn (_: packet_info) = TRUE.
72
+ }
73
+
74
+ module actions
75
+ {
76
+
77
+ hidden
78
+ {
79
+ default_action = fn (type_id: int, packet_info: policy_validator::packet_info, args: (str->>any))
80
+ {
81
+ continuation_id = (args $continuation_id) as (global_id+).
82
+ continuation_id_local is global_id+ = NIL.
83
+ if continuation_id != NIL {
84
+ cb = fn (args)
85
+ {
86
+ return ::transaction::success [
87
+ ::transaction::action::send (packet_info $id) ($name->"::continuation::continue_transaction", $targ->($id->continuation_id?, $args->args))
88
+ ].
89
+ }
90
+ continuation_id_local -> continuation::add_continuation cb.
91
+ }
92
+ return transaction::success [
93
+ ::transaction::action::return_data ($type->type_id, $data->args'($continuation_id->continuation_id_local)) // override the continuation id with the local value
94
+ ].
95
+ }
96
+
97
+ overloaded_actions is int->>(int->(policy_validator::packet_info)->(str->>any)->any) = (,).
98
+
99
+ _add_action = fn (type_id: int, cb: int->policy_validator::packet_info->(str->>any)->any)
100
+ {
101
+ abort "Function for given type of action already exists: " + (_str type_id) when overloaded_actions type_id != NIL.
102
+ overloaded_actions type_id -> cb.
103
+ }
104
+
105
+ }
106
+
107
+ execute = fn (type_id: int, info: policy_validator::packet_info, args: (str->>any))
108
+ {
109
+ abort "Permission denied." when !(policy_validator::validate type_id info).
110
+
111
+ action = overloaded_actions type_id.
112
+ if action == NIL {
113
+ action -> default_action.
114
+ }
115
+
116
+
117
+ return action type_id info args.
118
+ }
119
+
120
+ }
121
+
122
+
123
+
124
+ }
125
+
126
+
127
+
128
+ trn switch_to_debug
129
+ args:($continuation_id->global_id+)
130
+ {
131
+ ::current_transaction_info::validate_origin_or_abort (::transaction::envelope::origin::external,).
132
+ envelope = ::current_transaction_info::get_external_envelope_or_abort().
133
+ packet_info = ($id -> envelope $from).
134
+ return actions::execute types::switch_to_debug packet_info args.
135
+ }
136
+
137
+ trn switch_to_release
138
+ args:($continuation_id->global_id+)
139
+ {
140
+ ::current_transaction_info::validate_origin_or_abort (::transaction::envelope::origin::external,).
141
+ envelope = ::current_transaction_info::get_external_envelope_or_abort().
142
+ packet_info = ($id -> envelope $from).
143
+ return actions::execute types::switch_to_release packet_info args.
144
+ }
145
+
146
+ trn get_logs
147
+ args:($continuation_id->global_id)
148
+ {
149
+ ::current_transaction_info::validate_origin_or_abort (::transaction::envelope::origin::external,).
150
+ envelope = ::current_transaction_info::get_external_envelope_or_abort().
151
+ packet_info = ($id -> envelope $from).
152
+ return actions::execute types::get_logs packet_info args.
153
+ }
154
+
155
+ trn add_packet
156
+ args:($unit_hash->global_id, $seed_phrase->str, $continuation_id->global_id+)
157
+ {
158
+ ::current_transaction_info::validate_origin_or_abort (::transaction::envelope::origin::external,).
159
+ envelope = ::current_transaction_info::get_external_envelope_or_abort().
160
+ packet_info = ($id -> envelope $from).
161
+ return actions::execute types::add_packet packet_info args.
162
+ }
163
+
164
+ trn backup_packet
165
+ args:($packet_id->global_id+, $state->hash_code+, $continuation_id->global_id+)
166
+ {
167
+ ::current_transaction_info::validate_origin_or_abort (::transaction::envelope::origin::external,).
168
+ envelope = ::current_transaction_info::get_external_envelope_or_abort().
169
+ packet_info = ($id -> envelope $from).
170
+ return actions::execute types::backup_packet packet_info args.
171
+ }
172
+
173
+ trn remove_packet
174
+ args: ($packet_id->global_id, $continuation_id->global_id+)
175
+ {
176
+ ::current_transaction_info::validate_origin_or_abort (::transaction::envelope::origin::external,).
177
+ envelope = ::current_transaction_info::get_external_envelope_or_abort().
178
+ packet_info = ($id -> envelope $from).
179
+ return actions::execute types::remove_packet packet_info args.
180
+ }
181
+
182
+
183
+
184
+ }
@@ -0,0 +1,333 @@
1
+ /*
2
+
3
+ ** == and != to make sure types make sense. Refuse to compare if type mismatch
4
+
5
+ type
6
+ 1 NIL
7
+ 2 TRUE
8
+ 3 LittleEndianInteger64 + 8bytes-data
9
+ 4 Tiny-string + 8bytes-data-nt
10
+ 5 Short-string + 2bytes-length + data-nt
11
+ 6 Long-string + 4bytes-length + data-nt
12
+ 7 Tiny-binary + 32bytes-data
13
+ 8 Short-binary + 2bytes-length + data
14
+ 9 Long-binary + 4bytes-length + data
15
+ 10 Dictionary + 2bytes-element-count + 4bytes-length + [key, value]*count
16
+ keys can be any type except dictionary
17
+ 11 implementation-specific-type-X + implementation-specific-type-ID + implementation-specific-type-data
18
+
19
+ */
20
+
21
+ /*
22
+ and address document is a record of keys that are attested to belong to a certain container.
23
+
24
+ */
25
+
26
+ library address_document loads libraries address_document_types, key_storage, key_utils, transaction_queue, current_transaction_info uses transactions
27
+ {
28
+ using key_utils.
29
+ using address_document_types.
30
+
31
+ /*
32
+ When creating a new container
33
+ 1. User creating a new container will supply the information for an authorizing container
34
+ 2. User creating a new container will have priviledges with the authorizing container to register a new container
35
+ 3. Eventually any container will have an address document with authorization from the specified authorizing container
36
+
37
+
38
+ /*
39
+ message (container to container):
40
+ body
41
+ originator address document
42
+ attestation document
43
+ */
44
+
45
+ hidden {
46
+ m_current_version IS int = 1.
47
+ m_authorizing_containers IS t_container_id(,) = (,).
48
+ m_ad_signatures IS (t_container_id ->> crypto_signature) = (,).
49
+ m_max_allowed_number_of_authorizations = 10.
50
+ m_valid_versions = (1,).
51
+ m_my_address_document IS t_address_document+ = NIL.
52
+ }
53
+
54
+ process_address_document = fn (ad: t_address_document, no_check_for_authorization: bool) -> nil
55
+ {
56
+ ad => (
57
+ $version -> incoming_version,
58
+ $identity -> incoming_identity = (
59
+ $key_list -> incoming_key_list,
60
+ $default_keys -> incoming_default_key_list,
61
+ $container_id -> incoming_container_id
62
+ ),
63
+ $authorizations -> incoming_authorizations
64
+ ).
65
+
66
+ abort "Invalid address document version" WHEN !(m_valid_versions incoming_version).
67
+ abort "Container ID cannot match mine" WHEN incoming_container_id == _get_container_id().
68
+ abort "Too many authorizing signatures" WHEN _count incoming_authorizations > m_max_allowed_number_of_authorizations.
69
+
70
+ sign_key_id_for_binding = incoming_default_key_list $SIGN.
71
+ sign_pub_for_binding IS publickey_sign+ = NIL.
72
+ sc incoming_key_list -- (k->) {
73
+ if _crypto_get_key_id k == sign_key_id_for_binding {
74
+ sign_pub_for_binding -> k SAFE(publickey_sign).
75
+ break.
76
+ }
77
+ }
78
+ abort "SIGN key not found in address document" WHEN sign_pub_for_binding == NIL.
79
+ abort "Address document: container id does not commit to signing key"
80
+ WHEN (key_storage::address_of_key sign_pub_for_binding?) != incoming_container_id.
81
+
82
+ keyset_difference = key_storage::analyse_keyset_difference incoming_container_id incoming_key_list incoming_default_key_list.
83
+ is_new_container = keyset_difference $is_new_container.
84
+
85
+ // Who is allowed to authorize this keyset?
86
+ authorizing_containers = m_authorizing_containers.
87
+ if !is_new_container || no_check_for_authorization {
88
+ authorizing_containers incoming_container_id -> TRUE.
89
+ }
90
+
91
+ count_authorizations IS int = 0.
92
+ added_signing_keys_unconfirmed IS t_key_id->>t_publickey = keyset_difference $added_keys | $SIGN |.
93
+
94
+ sc incoming_authorizations -- (signature -> ) {
95
+ key_id = _crypto_get_key_id signature.
96
+ if is_new_container {
97
+ abort "Invalid signature in address document" WHEN key_storage::check_signature_new_container (_value_id incoming_identity) signature incoming_key_list == NIL.
98
+ count_authorizations->count_authorizations + 1.
99
+ } else {
100
+ signing_container_id = key_storage::get_source_container signature| . // NIL in case of new container. ABORTS
101
+ if authorizing_containers signing_container_id? {
102
+ abort "Invalid signature in address document" WHEN key_storage::check_signature (_value_id incoming_identity) signature == NIL.
103
+
104
+ count_authorizations->count_authorizations + 1.
105
+ }
106
+ }
107
+
108
+ if added_signing_keys_unconfirmed key_id {
109
+ delete added_signing_keys_unconfirmed key_id.
110
+ }
111
+ }
112
+
113
+ abort "Not all keys being added to the keyset are confirmed via using the key to generate appropriate overall signature"
114
+ WHEN _count added_signing_keys_unconfirmed > 0.
115
+
116
+ abort "Container is not authorized by a known authorizing container" WHEN count_authorizations == 0.
117
+
118
+ unpacked_signatures IS t_key_id->>crypto_signature = (,).
119
+ sc incoming_authorizations -- (signature -> ) {
120
+ unpacked_signatures (_crypto_get_key_id signature) -> signature.
121
+ }
122
+
123
+
124
+ // Ensure that all new signing keys have associated signatures
125
+ sc keyset_difference $added_keys $SIGN | -- (key_id -> public_key) {
126
+ associated_signature = unpacked_signatures key_id abort "Signing key was added, but associated signature) not exist" WHEN IS NIL.
127
+ abort "Signature is invalid for new key added" WHEN _crypto_verify_hash_signature (_value_id incoming_identity) associated_signature? public_key as(publickey_sign) == NIL.
128
+ }
129
+
130
+
131
+ key_storage::update_keyset keyset_difference.
132
+ }
133
+
134
+ check_signature_by_authorizing_container = fn (hash: hash_code, signature: crypto_signature) -> bool {
135
+ abort "Signature failed validation." WHEN !(key_storage::check_signature hash signature).
136
+ cid = key_storage::container_id_of_signer signature.
137
+ abort "Signer container is not an authorizing container." WHEN !(_has m_authorizing_containers cid).
138
+ return TRUE.
139
+ }
140
+
141
+ add_authorizing_container = fn (ad: t_address_document) -> nil
142
+ {
143
+ process_address_document ad TRUE.
144
+ m_authorizing_containers (ad $identity $container_id)->TRUE.
145
+ }
146
+
147
+ produce_my_address_document = fn (_) -> t_address_document
148
+ {
149
+ my_identity = key_storage::get_my_identity().
150
+ signatures IS crypto_signature(,) = (,).
151
+ hash = _value_id my_identity.
152
+
153
+ sc my_identity $key_list -- (key -> ) ?? key_get_function key == $SIGN {
154
+ sig = key_storage::sign hash (_crypto_get_key_id key).
155
+ signatures sig -> TRUE.
156
+ }
157
+
158
+ m_my_address_document -> ($version->m_current_version, $identity->my_identity, $authorizations->signatures).
159
+ RETURN m_my_address_document?.
160
+ }
161
+
162
+ get_my_address_document = fn (_) -> t_address_document
163
+ {
164
+ if !m_my_address_document {
165
+ produce_my_address_document().
166
+ }
167
+ RETURN m_my_address_document?.
168
+ }
169
+
170
+
171
+ trn register_container
172
+ _:($ad -> ad: t_address_document) {
173
+ process_address_document ad NIL.
174
+ RETURN transaction::success [].
175
+ }
176
+
177
+ trn produce_address_document
178
+ _ {
179
+ produce_my_address_document().
180
+ RETURN transaction::success [].
181
+ }
182
+
183
+ trn get_address_document
184
+ _ {
185
+ current_transaction_info::validate_origin (transaction::envelope::origin::user,).
186
+ RETURN transaction::success [
187
+ transaction::action::return_data get_my_address_document()
188
+ ].
189
+ }
190
+
191
+ trn save_ad_signature
192
+ _:($authorizing_container_id -> cid: global_id, $signature -> signature: crypto_signature) {
193
+ abort "Authorizing container not recognized." WHEN (m_authorizing_containers cid == NIL).
194
+ m_ad_signatures cid -> signature.
195
+ RETURN transaction::success [].
196
+ }
197
+
198
+ hidden {
199
+ module handshake {
200
+ check_ad_signature = fn (_:($ad->ad: t_address_document, $signature->signature: crypto_signature)) {
201
+ hash = _value_id ad.
202
+ res IS bool = FALSE.
203
+ // check whether container signed the ad is authorizing container
204
+ sc m_authorizing_containers -- (auth -> ) {
205
+ if (key_storage::get_source_container signature == auth) {
206
+ res -> TRUE.
207
+ break.
208
+ }
209
+ }
210
+ abort "Address document signed by unrcognized container." WHEN !res.
211
+ abort "Invalid signature." WHEN (key_storage::check_signature hash signature == NIL).
212
+ process_address_document ad NIL.
213
+ }
214
+
215
+ send_ad_and_signatures = fn (cid: global_id, isinit: bool) -> transaction::action::type[] {
216
+ trn_name = "::address_document::handshake".
217
+ if !isinit { trn_name -> trn_name + "_final". }
218
+ auth_cid IS global_id+ = NIL.
219
+ sc m_authorizing_containers -- (cid -> ) {
220
+ auth_cid -> cid.
221
+ break.
222
+ }
223
+ abort "There is no authorizing container." WHEN auth_cid == NIL.
224
+ actions IS transaction::action::type[] = [].
225
+ sc m_ad_signatures -- ( -> signature) {
226
+ trn_nested = ($name->trn_name, $targ->($ad->m_my_address_document?, $signature->signature)).
227
+ tn = ($name->"::authorizing_container::resend_encrypted", $targ->($trn->trn_nested, $target_container_id->cid)).
228
+ encrypted_trn = transaction::encrypt ($cid->auth_cid?, $trn->tn, $isemsignature->TRUE).
229
+ actions (_count actions|) -> (transaction::action::send auth_cid? encrypted_trn).
230
+ }
231
+ RETURN actions.
232
+ }
233
+ }
234
+ }
235
+
236
+
237
+ trn register_authorizing_container
238
+ _: ($ad -> ad: t_address_document) {
239
+ if m_my_address_document == NIL {
240
+ produce_my_address_document().
241
+ }
242
+ // add authorizing container
243
+ add_authorizing_container ad.
244
+ // send request to this container to sign address document
245
+ cid = (ad $identity $container_id) SAFE(global_id).
246
+ encrypted_trn = transaction::encrypt ($cid -> cid, $trn -> ($name -> "::authorizing_container::sign_ad", $targ -> ($ad -> m_my_address_document?)), $isemsignature -> TRUE).
247
+ RETURN transaction::success [transaction::action::send cid encrypted_trn].
248
+ }
249
+
250
+ handshake_init = fn (_:($target_container_id -> cid: global_id)) -> transaction::action::type[] = (handshake::send_ad_and_signatures cid TRUE).
251
+
252
+ /**
253
+ 1. Register callback, that initializes a handshake after the ad signature from authorizer received
254
+ 2. Register callback, that should be called after handshake
255
+ */
256
+ on_handshake_final = fn (target_cid: global_id, actions_creator_cb: transaction_queue::t_actions_creator_func) -> transaction::action::type[] {
257
+ transaction_queue::add "::address_document::handshake_final" actions_creator_cb.
258
+
259
+ init_handshake_callback = fn (_) -> transaction::action::type[] = (handshake_init ($target_container_id -> target_cid)).
260
+
261
+ // if ad signature already saved, we can send handshake to another container right away
262
+ if (_count m_ad_signatures|) == 0 {
263
+ transaction_queue::add "::address_document::save_ad_signature" init_handshake_callback.
264
+ } else {
265
+ RETURN init_handshake_callback().
266
+ }
267
+
268
+ RETURN [].
269
+ }
270
+
271
+ trn handshake_init
272
+ arg:($target_container_id->global_id) {
273
+ RETURN transaction::success (handshake_init arg).
274
+ }
275
+
276
+
277
+ trn handshake
278
+ arg:($ad->ad: t_address_document, $signature->crypto_signature) {
279
+ handshake_callback = fn (_) -> transaction::action::type[] {
280
+ handshake::check_ad_signature arg.
281
+ cid = ad $identity $container_id.
282
+ RETURN (handshake::send_ad_and_signatures cid NIL).
283
+
284
+ }
285
+ if (_count m_ad_signatures|) == 0 {
286
+ transaction_queue::add "::address_document::save_ad_signature" handshake_callback.
287
+ } else {
288
+ RETURN transaction::success (handshake_callback()).
289
+ }
290
+
291
+ RETURN transaction::success [].
292
+ }
293
+
294
+ trn handshake_final
295
+ arg:($ad->t_address_document, $signature->crypto_signature) {
296
+ handshake::check_ad_signature arg.
297
+ RETURN transaction::success [].
298
+ }
299
+
300
+ }
301
+
302
+ /* THINKING ABOUT CONTAINER AUTHORIZATION PROTOCOL
303
+
304
+ Authorization/identification API
305
+ Role:
306
+ Container creator -- user or upper container ("root")
307
+ Authorization protocol:
308
+ Possibility 1: authorizing container (implement now)
309
+ Possibility 2: authorizing token -- NOT YET
310
+ Counterparty container
311
+
312
+ API:
313
+ API for peer container
314
+ Root->Container: For newly created container, creator introduces or changes authorizing container {AddressDocument of authorizing container}
315
+ Container->AuthorizingContainer: please sign my keys {My AddressAddressDocyment -> My addressDocument signed by AuthorizingContainer}
316
+ Container->Container: Here is my AddressDocument
317
+ RollKeys: Introduce a new keypair and creates the new addressdocument
318
+ RevokeKey: Revokes and old key and creates the new addressdocument
319
+ API for authorizing container
320
+ For authorizing container, and authorized user introduced newly created container {AddressDocument of container}
321
+ SignMyKey
322
+ RollMyKeys
323
+
324
+ Participants: Container, Dispatcher
325
+ Container is being launched and needs to generate an address document signed/authorized by Dispatcher.
326
+ User->Container: register Dispatcher's authorizing container information at launch of Container
327
+ set the authorization token, as per what the Dispatcher requires
328
+ (
329
+ Authorization token could be a proof of ownership of a blockchain address, or a cookie that the dispatcher has generated
330
+ )
331
+ Container->Dispatcher: self-signed address document plus the authorization token
332
+ Dispatcher->Container: authorized address document
333
+ */
@@ -0,0 +1,16 @@
1
+ library address_document_types loads library key_utils
2
+ {
3
+ using key_utils.
4
+ metadef t_address_document:
5
+ (
6
+ $version->int, // version of the address document
7
+ $identity -> t_container_identity: // specifies the identity of the container
8
+ (
9
+ $key_list->t_publickey(,), // list of keys active with this container
10
+ $default_keys->t_function->>t_key_id, // exactly two keys, one for signing, one for encryption, that are the default keys for this container
11
+ $container_id->t_container_id // id of the container
12
+ /* $code_id->hash_code */
13
+ ),
14
+ $authorizations -> crypto_signature(,)
15
+ ).
16
+ }