pplcdid 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/AUTHORS +1 -0
- data/LICENSE +201 -0
- data/README.md +2 -0
- data/VERSION +1 -0
- data/lib/ppldid/basic.rb +278 -0
- data/lib/ppldid/didcomm.rb +120 -0
- data/lib/ppldid/log.rb +394 -0
- data/lib/ppldid.rb +739 -0
- data/spec/input/basic/arrays.json +8 -0
- data/spec/input/basic/french.json +6 -0
- data/spec/input/basic/sample2_get_location.doc +1 -0
- data/spec/input/basic/sample2_retrieve_document.doc +1 -0
- data/spec/input/basic/sample3_retrieve_document.doc +1 -0
- data/spec/input/basic/sample4_retrieve_document.doc +1 -0
- data/spec/input/basic/sample5_retrieve_document.doc +1 -0
- data/spec/input/basic/sample_dec.doc +1 -0
- data/spec/input/basic/sample_enc.doc +1 -0
- data/spec/input/basic/sample_get_location.doc +1 -0
- data/spec/input/basic/sample_hash.doc +1 -0
- data/spec/input/basic/sample_invalid2_readkey.doc +1 -0
- data/spec/input/basic/sample_invalid2_verify.doc +1 -0
- data/spec/input/basic/sample_invalid3_readkey.doc +1 -0
- data/spec/input/basic/sample_invalid3_verify.doc +1 -0
- data/spec/input/basic/sample_invalid_privkey.doc +1 -0
- data/spec/input/basic/sample_invalid_readkey.doc +1 -0
- data/spec/input/basic/sample_invalid_sign.doc +1 -0
- data/spec/input/basic/sample_invalid_verify.doc +1 -0
- data/spec/input/basic/sample_key.doc +1 -0
- data/spec/input/basic/sample_readkey.doc +1 -0
- data/spec/input/basic/sample_retrieve_document.doc +1 -0
- data/spec/input/basic/sample_sign.doc +1 -0
- data/spec/input/basic/sample_valid_privkey.doc +1 -0
- data/spec/input/basic/sample_verify.doc +1 -0
- data/spec/input/basic/structures.json +8 -0
- data/spec/input/basic/unicode.json +3 -0
- data/spec/input/basic/values.json +5 -0
- data/spec/input/basic/wierd.json +11 -0
- data/spec/input/basic/zQmaBZTghn.doc +1 -0
- data/spec/input/log/sample0_dag2array.doc +1 -0
- data/spec/input/log/sample0_dag_did.doc +1 -0
- data/spec/input/log/sample1_dag_did.doc +1 -0
- data/spec/input/log/sample1_dag_update.doc +1 -0
- data/spec/input/log/sample2_dag_did.doc +1 -0
- data/spec/input/log/sample2_dag_update.doc +1 -0
- data/spec/input/log/sample2_retrieve_log.doc +1 -0
- data/spec/input/log/sample3_dag_did.doc +1 -0
- data/spec/input/log/sample3_dag_update.doc +1 -0
- data/spec/input/log/sample3_retrieve_log.doc +1 -0
- data/spec/input/log/sample4_dag_did.doc +1 -0
- data/spec/input/log/sample4_dag_update.doc +1 -0
- data/spec/input/log/sample4_retrieve_log.doc +1 -0
- data/spec/input/log/sample5_dag_update.doc +1 -0
- data/spec/input/log/sample5_retrieve_log.doc +1 -0
- data/spec/input/log/sample6_dag_update.doc +1 -0
- data/spec/input/log/sample6_retrieve_log.doc +1 -0
- data/spec/input/log/sample7_dag_update.doc +1 -0
- data/spec/input/log/sample7_retrieve_log.doc +1 -0
- data/spec/input/log/sample8_dag_update.doc +1 -0
- data/spec/input/log/sample_addhash.doc +1 -0
- data/spec/input/log/sample_dag_update.doc +1 -0
- data/spec/input/log/sample_match_log.doc +1 -0
- data/spec/input/log/sample_op1_addhash.doc +1 -0
- data/spec/input/log/sample_retrieve_log.doc +1 -0
- data/spec/input/main/sample0_read.doc +1 -0
- data/spec/output/basic/arrays.json +1 -0
- data/spec/output/basic/french.json +1 -0
- data/spec/output/basic/sample2_get_location.doc +1 -0
- data/spec/output/basic/sample2_retrieve_document.doc +1 -0
- data/spec/output/basic/sample3_retrieve_document.doc +1 -0
- data/spec/output/basic/sample4_retrieve_document.doc +1 -0
- data/spec/output/basic/sample5_retrieve_document.doc +1 -0
- data/spec/output/basic/sample_dec.doc +1 -0
- data/spec/output/basic/sample_enc.doc +1 -0
- data/spec/output/basic/sample_get_location.doc +1 -0
- data/spec/output/basic/sample_hash.doc +1 -0
- data/spec/output/basic/sample_invalid2_readkey.doc +1 -0
- data/spec/output/basic/sample_invalid2_verify.doc +1 -0
- data/spec/output/basic/sample_invalid3_readkey.doc +1 -0
- data/spec/output/basic/sample_invalid3_verify.doc +1 -0
- data/spec/output/basic/sample_invalid_privkey.doc +1 -0
- data/spec/output/basic/sample_invalid_readkey.doc +1 -0
- data/spec/output/basic/sample_invalid_sign.doc +1 -0
- data/spec/output/basic/sample_invalid_verify.doc +1 -0
- data/spec/output/basic/sample_key.doc +1 -0
- data/spec/output/basic/sample_readkey.doc +1 -0
- data/spec/output/basic/sample_retrieve_document.doc +1 -0
- data/spec/output/basic/sample_sign.doc +1 -0
- data/spec/output/basic/sample_valid_privkey.doc +1 -0
- data/spec/output/basic/sample_verify.doc +1 -0
- data/spec/output/basic/structures.json +1 -0
- data/spec/output/basic/unicode.json +1 -0
- data/spec/output/basic/values.json +1 -0
- data/spec/output/basic/wierd.json +1 -0
- data/spec/output/log/sample0_dag2array.doc +1 -0
- data/spec/output/log/sample0_dag_did.doc +1 -0
- data/spec/output/log/sample1_dag_did.doc +1 -0
- data/spec/output/log/sample1_dag_update.doc +1 -0
- data/spec/output/log/sample2_dag_did.doc +1 -0
- data/spec/output/log/sample2_dag_update.doc +1 -0
- data/spec/output/log/sample2_retrieve_log.doc +1 -0
- data/spec/output/log/sample3_dag_did.doc +1 -0
- data/spec/output/log/sample3_dag_update.doc +1 -0
- data/spec/output/log/sample3_retrieve_log.doc +1 -0
- data/spec/output/log/sample4_dag_did.doc +1 -0
- data/spec/output/log/sample4_dag_update.doc +1 -0
- data/spec/output/log/sample4_retrieve_log.doc +1 -0
- data/spec/output/log/sample5_dag_update.doc +1 -0
- data/spec/output/log/sample5_retrieve_log.doc +1 -0
- data/spec/output/log/sample6_dag_update.doc +1 -0
- data/spec/output/log/sample6_retrieve_log.doc +1 -0
- data/spec/output/log/sample7_dag_update.doc +1 -0
- data/spec/output/log/sample7_retrieve_log.doc +1 -0
- data/spec/output/log/sample8_dag_update.doc +1 -0
- data/spec/output/log/sample_addhash.doc +1 -0
- data/spec/output/log/sample_dag_update.doc +1 -0
- data/spec/output/log/sample_match_log.doc +1 -0
- data/spec/output/log/sample_op1_addhash.doc +1 -0
- data/spec/output/log/sample_retrieve_log.doc +1 -0
- data/spec/output/main/sample0_read.doc +1 -0
- data/spec/ppldid_spec.rb +170 -0
- data/spec/spec_helper.rb +31 -0
- metadata +420 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4078f962120d21b5e55d2a62fdb62ee7278375672b31eac6b4d55b343dc381d7
|
4
|
+
data.tar.gz: a38212b89e4fe7ba44c0db3c720da018988879d374bddca123a43120c4fbf280
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 891cc95218461f8f0d9c554579a1ca496fbc98d13e9725602595ba3e73d80ca1d3849eda4d5da2523c748a4cff491169c5e7e196a28003dd3f5eb64558d162d7
|
7
|
+
data.tar.gz: 87bab16182bc655081b600b6c4173c0bc465c274a1d8577c11adeea1a4763cc7a9cf3fdd33a0b6a2d15214a4455e77607f81f88c36928383e73f81fcaf1eb393
|
data/AUTHORS
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
* jerry zhang <jerry.zhang.bill@gmail.com>
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [2022] [Peopledata.org]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.2.3
|
data/lib/ppldid/basic.rb
ADDED
@@ -0,0 +1,278 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
class Pplcdid
|
5
|
+
|
6
|
+
# basic functions ---------------------------
|
7
|
+
def self.encode(message, method = "base58btc")
|
8
|
+
Multibases.pack(method, message).to_s
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.decode(message)
|
12
|
+
Multibases.unpack(message).decode.to_s('ASCII-8BIT')
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.hash(message)
|
16
|
+
encode(Multihashes.encode(RbNaCl::Hash.sha256(message), "sha2-256").unpack('C*'))
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.canonical(message)
|
20
|
+
if message.is_a? String
|
21
|
+
message = JSON.parse(message) rescue message
|
22
|
+
else
|
23
|
+
message = JSON.parse(message.to_json) rescue message
|
24
|
+
end
|
25
|
+
message.to_json_c14n
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.percent_encode(did)
|
29
|
+
# remove "https://" from string as it is default
|
30
|
+
did = did.sub("https://","").sub("@", "%40").sub("http://","http%3A%2F%2F")
|
31
|
+
end
|
32
|
+
|
33
|
+
# key management ----------------------------
|
34
|
+
def self.generate_private_key(input, method = "ed25519-priv")
|
35
|
+
begin
|
36
|
+
omc = Multicodecs[method].code
|
37
|
+
rescue
|
38
|
+
return [nil, "unknown key codec"]
|
39
|
+
end
|
40
|
+
|
41
|
+
case Multicodecs[method].name
|
42
|
+
when 'ed25519-priv'
|
43
|
+
if input != ""
|
44
|
+
raw_key = Ed25519::SigningKey.new(RbNaCl::Hash.sha256(input)).to_bytes
|
45
|
+
else
|
46
|
+
raw_key = Ed25519::SigningKey.generate.to_bytes
|
47
|
+
end
|
48
|
+
else
|
49
|
+
return [nil, "unsupported key codec"]
|
50
|
+
end
|
51
|
+
length = raw_key.bytesize
|
52
|
+
return [encode([omc, length, raw_key].pack("SCa#{length}")), ""]
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.public_key(private_key, method = "ed25519-pub")
|
56
|
+
code, length, digest = decode(private_key).unpack('SCa*')
|
57
|
+
case Multicodecs[code].name
|
58
|
+
when 'ed25519-priv'
|
59
|
+
case method
|
60
|
+
when 'ed25519-pub'
|
61
|
+
public_key = Ed25519::SigningKey.new(digest).verify_key
|
62
|
+
when 'x25519-pub'
|
63
|
+
public_key = RbNaCl::PrivateKey.new(digest).public_key
|
64
|
+
else
|
65
|
+
return [nil, "unsupported key codec"]
|
66
|
+
end
|
67
|
+
length = public_key.to_bytes.bytesize
|
68
|
+
return [encode([Multicodecs[method].code, length, public_key].pack("CCa#{length}")), ""]
|
69
|
+
|
70
|
+
else
|
71
|
+
return [nil, "unsupported key codec"]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.sign(message, private_key)
|
76
|
+
code, length, digest = decode(private_key).unpack('SCa*')
|
77
|
+
case Multicodecs[code].name
|
78
|
+
when 'ed25519-priv'
|
79
|
+
return [encode(Ed25519::SigningKey.new(digest).sign(message)), ""]
|
80
|
+
else
|
81
|
+
return [nil, "unsupported key codec"]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.verify(message, signature, public_key)
|
86
|
+
begin
|
87
|
+
code, length, digest = decode(public_key).unpack('CCa*')
|
88
|
+
case Multicodecs[code].name
|
89
|
+
when 'ed25519-pub'
|
90
|
+
verify_key = Ed25519::VerifyKey.new(digest)
|
91
|
+
signature_verification = false
|
92
|
+
begin
|
93
|
+
verify_key.verify(decode(signature), message)
|
94
|
+
signature_verification = true
|
95
|
+
rescue Ed25519::VerifyError
|
96
|
+
signature_verification = false
|
97
|
+
end
|
98
|
+
return [signature_verification, ""]
|
99
|
+
else
|
100
|
+
return [nil, "unsupported key codec"]
|
101
|
+
end
|
102
|
+
rescue
|
103
|
+
return [nil, "unknown key codec"]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.encrypt(message, public_key)
|
108
|
+
begin
|
109
|
+
code, length, digest = decode(public_key).unpack('CCa*')
|
110
|
+
case Multicodecs[code].name
|
111
|
+
when 'x25519-pub'
|
112
|
+
pubKey = RbNaCl::PublicKey.new(digest)
|
113
|
+
authHash = RbNaCl::Hash.sha256('auth'.dup.force_encoding('ASCII-8BIT'))
|
114
|
+
authKey = RbNaCl::PrivateKey.new(authHash)
|
115
|
+
box = RbNaCl::Box.new(pubKey, authKey)
|
116
|
+
nonce = RbNaCl::Random.random_bytes(box.nonce_bytes)
|
117
|
+
msg = message.force_encoding('ASCII-8BIT')
|
118
|
+
cipher = box.encrypt(nonce, msg)
|
119
|
+
return [
|
120
|
+
{
|
121
|
+
value: cipher.unpack('H*')[0],
|
122
|
+
nonce: nonce.unpack('H*')[0]
|
123
|
+
}, ""
|
124
|
+
]
|
125
|
+
else
|
126
|
+
return [nil, "unsupported key codec"]
|
127
|
+
end
|
128
|
+
rescue
|
129
|
+
return [nil, "encryption failed"]
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def self.decrypt(message, private_key)
|
134
|
+
begin
|
135
|
+
cipher = [JSON.parse(message)["value"]].pack('H*')
|
136
|
+
nonce = [JSON.parse(message)["nonce"]].pack('H*')
|
137
|
+
code, length, digest = decode(private_key).unpack('SCa*')
|
138
|
+
case Multicodecs[code].name
|
139
|
+
when 'ed25519-priv'
|
140
|
+
privKey = RbNaCl::PrivateKey.new(digest)
|
141
|
+
authHash = RbNaCl::Hash.sha256('auth'.dup.force_encoding('ASCII-8BIT'))
|
142
|
+
authKey = RbNaCl::PrivateKey.new(authHash).public_key
|
143
|
+
box = RbNaCl::Box.new(authKey, privKey)
|
144
|
+
retVal = box.decrypt(nonce, cipher)
|
145
|
+
return [retVal, ""]
|
146
|
+
else
|
147
|
+
return [nil, "unsupported key codec"]
|
148
|
+
end
|
149
|
+
rescue
|
150
|
+
return [nil, "decryption failed"]
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def self.read_private_key(filename)
|
155
|
+
begin
|
156
|
+
f = File.open(filename)
|
157
|
+
key_encoded = f.read
|
158
|
+
f.close
|
159
|
+
rescue
|
160
|
+
return [nil, "cannot read file"]
|
161
|
+
end
|
162
|
+
decode_private_key(key_encoded)
|
163
|
+
end
|
164
|
+
|
165
|
+
def self.decode_private_key(key_encoded)
|
166
|
+
begin
|
167
|
+
code, length, digest = decode(key_encoded).unpack('SCa*')
|
168
|
+
case Multicodecs[code].name
|
169
|
+
when 'ed25519-priv'
|
170
|
+
private_key = Ed25519::SigningKey.new(digest).to_bytes
|
171
|
+
else
|
172
|
+
return [nil, "unsupported key codec"]
|
173
|
+
end
|
174
|
+
length = private_key.bytesize
|
175
|
+
return [pplcdid.encode([code, length, private_key].pack("SCa#{length}")), ""]
|
176
|
+
rescue
|
177
|
+
return [nil, "invalid key"]
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
# storage functions -----------------------------
|
182
|
+
def self.write_private_storage(payload, filename)
|
183
|
+
File.open(filename, 'w') {|f| f.write(payload)}
|
184
|
+
end
|
185
|
+
|
186
|
+
def self.read_private_storage(filename)
|
187
|
+
begin
|
188
|
+
File.open(filename, 'r') { |f| f.read }
|
189
|
+
rescue
|
190
|
+
nil
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
def self.get_location(id)
|
195
|
+
if id.include?(LOCATION_PREFIX)
|
196
|
+
id_split = id.split(LOCATION_PREFIX)
|
197
|
+
return id_split[1]
|
198
|
+
else
|
199
|
+
if id.include?(CGI.escape(LOCATION_PREFIX))
|
200
|
+
id_split = id.split(CGI.escape(LOCATION_PREFIX))
|
201
|
+
return id_split[1]
|
202
|
+
else
|
203
|
+
return DEFAULT_LOCATION
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
def self.retrieve_document(doc_hash, doc_file, doc_location, options)
|
209
|
+
if doc_location == ""
|
210
|
+
doc_location = DEFAULT_LOCATION
|
211
|
+
end
|
212
|
+
if !(doc_location == "" || doc_location == "local")
|
213
|
+
if !doc_location.start_with?("http")
|
214
|
+
doc_location = "https://" + doc_location
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
case doc_location
|
219
|
+
when /^http/
|
220
|
+
doc_location = doc_location.sub("%3A%2F%2F","://")
|
221
|
+
retVal = HTTParty.get(doc_location + "/doc/" + doc_hash)
|
222
|
+
if retVal.code != 200
|
223
|
+
msg = retVal.parsed_response("error").to_s rescue "invalid response from " + doc_location.to_s + "/doc/" + doc_hash.to_s
|
224
|
+
return [nil, msg]
|
225
|
+
end
|
226
|
+
if options.transform_keys(&:to_s)["trace"]
|
227
|
+
if options[:silent].nil? || !options[:silent]
|
228
|
+
puts "GET " + doc_hash + " from " + doc_location
|
229
|
+
end
|
230
|
+
end
|
231
|
+
return [retVal.parsed_response, ""]
|
232
|
+
when "", "local"
|
233
|
+
doc = JSON.parse(read_private_storage(doc_file)) rescue {}
|
234
|
+
if doc == {}
|
235
|
+
return [nil, "cannot read file"]
|
236
|
+
else
|
237
|
+
return [doc, ""]
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
def self.retrieve_document_raw(doc_hash, doc_file, doc_location, options)
|
243
|
+
if doc_location == ""
|
244
|
+
doc_location = DEFAULT_LOCATION
|
245
|
+
end
|
246
|
+
if !(doc_location == "" || doc_location == "local")
|
247
|
+
if !doc_location.start_with?("http")
|
248
|
+
doc_location = "https://" + doc_location
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
case doc_location
|
253
|
+
when /^http/
|
254
|
+
doc_location = doc_location.sub("%3A%2F%2F","://")
|
255
|
+
retVal = HTTParty.get(doc_location + "/doc_raw/" + doc_hash)
|
256
|
+
if retVal.code != 200
|
257
|
+
msg = retVal.parsed_response("error").to_s rescue "invalid response from " + doc_location.to_s + "/doc/" + doc_hash.to_s
|
258
|
+
return [nil, msg]
|
259
|
+
end
|
260
|
+
if options.transform_keys(&:to_s)["trace"]
|
261
|
+
if options[:silent].nil? || !options[:silent]
|
262
|
+
puts "GET " + doc_hash + " from " + doc_location
|
263
|
+
end
|
264
|
+
end
|
265
|
+
return [retVal.parsed_response, ""]
|
266
|
+
when "", "local"
|
267
|
+
doc = JSON.parse(read_private_storage(doc_file)) rescue {}
|
268
|
+
log = JSON.parse(read_private_storage(doc_file.sub(".doc", ".log"))) rescue {}
|
269
|
+
if doc == {}
|
270
|
+
return [nil, "cannot read file"]
|
271
|
+
else
|
272
|
+
obj = {"doc" => doc, "log" => log}
|
273
|
+
return [obj, ""]
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
class Pplcdid
|
5
|
+
|
6
|
+
# DIDComm Plain Message ---------------------
|
7
|
+
def self.dcpm(payload, options)
|
8
|
+
dcDoc = {}
|
9
|
+
dcDoc["id"] = SecureRandom.random_number(10e14).to_i
|
10
|
+
dcDoc["type"] = options[:didcomm_type]
|
11
|
+
if !options[:didcomm_from_did].nil?
|
12
|
+
dcDoc["from"] = options[:didcomm_from_did]
|
13
|
+
end
|
14
|
+
dcDoc["to"] = [options[:didcomm_to_did]]
|
15
|
+
dcDoc["created_time"] = Time.now.utc.to_i
|
16
|
+
dcDoc["body"] = payload
|
17
|
+
return [dcDoc, ""]
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
# DIDComm Signed Message --------------------
|
22
|
+
def self.dcsm(payload, private_key_encoded, options)
|
23
|
+
error = ""
|
24
|
+
code, length, digest = decode(private_key_encoded).unpack('SCa*')
|
25
|
+
case Multicodecs[code].name
|
26
|
+
when 'ed25519-priv'
|
27
|
+
private_key = RbNaCl::Signatures::Ed25519::SigningKey.new(digest)
|
28
|
+
token = JWT.encode payload, private_key, 'ED25519', { typ: 'JWM', kid: options[:sign_did].to_s, alg: 'ED25519' }
|
29
|
+
else
|
30
|
+
token = nil
|
31
|
+
error = "unsupported key codec"
|
32
|
+
end
|
33
|
+
return [token, error]
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.dcsm_verify(token, options)
|
37
|
+
error = ""
|
38
|
+
decoded_payload = JWT.decode token, nil, false
|
39
|
+
pubkey_did = decoded_payload.last["kid"]
|
40
|
+
result, msg = pplcdid.read(pubkey_did, options)
|
41
|
+
public_key_encoded = pplcdid.w3c(result, options)["authentication"].first["publicKeyMultibase"]
|
42
|
+
begin
|
43
|
+
code, length, digest = pplcdid.decode(public_key_encoded).unpack('CCa*')
|
44
|
+
case Multicodecs[code].name
|
45
|
+
when 'ed25519-pub'
|
46
|
+
public_key = RbNaCl::Signatures::Ed25519::VerifyKey.new(digest)
|
47
|
+
payload = JWT.decode token.to_s, public_key, true, { algorithm: 'ED25519' }
|
48
|
+
else
|
49
|
+
payload = nil
|
50
|
+
error = "unsupported key codec"
|
51
|
+
end
|
52
|
+
return [payload, error]
|
53
|
+
rescue
|
54
|
+
return [nil, "verification failed"]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# encryption -----------------------------------
|
59
|
+
def self.msg_encrypt(payload, private_key_encoded, did)
|
60
|
+
error = ""
|
61
|
+
code, length, digest = decode(private_key_encoded).unpack('SCa*')
|
62
|
+
case Multicodecs[code].name
|
63
|
+
when 'ed25519-priv'
|
64
|
+
private_key = RbNaCl::Signatures::Ed25519::SigningKey.new(digest)
|
65
|
+
token = JWT.encode payload, private_key, 'ED25519'
|
66
|
+
else
|
67
|
+
token = nil
|
68
|
+
error = "unsupported key codec"
|
69
|
+
end
|
70
|
+
return [token, error]
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.msg_decrypt(token, public_key_encoded)
|
74
|
+
error = ""
|
75
|
+
code, length, digest = pplcdid.decode(public_key_encoded).unpack('CCa*')
|
76
|
+
case Multicodecs[code].name
|
77
|
+
when 'ed25519-pub'
|
78
|
+
public_key = RbNaCl::Signatures::Ed25519::VerifyKey.new(digest)
|
79
|
+
payload = JWT.decode token.to_s, public_key, true, { algorithm: 'ED25519' }
|
80
|
+
else
|
81
|
+
payload = nil
|
82
|
+
error = "unsupported key codec"
|
83
|
+
end
|
84
|
+
return [payload, error]
|
85
|
+
end
|
86
|
+
|
87
|
+
# signing for JWS ---------------------------
|
88
|
+
def self.msg_sign(payload, hmac_secret)
|
89
|
+
token = JWT.encode payload, hmac_secret, 'HS256'
|
90
|
+
return [token, ""]
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.msg_verify_jws(token, hmac_secret)
|
94
|
+
begin
|
95
|
+
decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
|
96
|
+
return [decoded_token, ""]
|
97
|
+
rescue
|
98
|
+
return [nil, "verification failed"]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# DID Auth for data container with challenge ---
|
103
|
+
def self.token_from_challenge(host, pwd)
|
104
|
+
sid = SecureRandom.hex(20).to_s
|
105
|
+
retVal = HTTParty.post(host + "/pplcdid/init",
|
106
|
+
headers: { 'Content-Type' => 'application/json' },
|
107
|
+
body: { "session_id": sid }.to_json )
|
108
|
+
challenge = retVal.parsed_response["challenge"]
|
109
|
+
signed_challenge = pplcdid.sign(challenge, pplcdid.generate_private_key(pwd).first).first
|
110
|
+
public_key = pplcdid.public_key(pplcdid.generate_private_key(pwd).first).first
|
111
|
+
retVal = HTTParty.post(host + "/pplcdid/token",
|
112
|
+
headers: { 'Content-Type' => 'application/json' },
|
113
|
+
body: {
|
114
|
+
"session_id": sid,
|
115
|
+
"signed_challenge": signed_challenge,
|
116
|
+
"public_key": public_key
|
117
|
+
}.to_json)
|
118
|
+
return retVal.parsed_response["access_token"]
|
119
|
+
end
|
120
|
+
end
|