oydid 0.2.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/oydid/basic.rb +5 -1
  4. data/lib/oydid.rb +98 -29
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 84460bc149df8e10a4f5094879894399bd3d6670ff5ef1fc8c500811af5fb5e2
4
- data.tar.gz: 3515b7f22cf286b3abda13299fd15a6a713f39a2048c8f1d3553f36832577e24
3
+ metadata.gz: ac6225dd965dafae4c9a9013d023882dd0ffe6408eb8922ef4179b8c0be96e10
4
+ data.tar.gz: ec26220fc09876cef2fb554ebc8cb16b03485cd31d019514384831188addd645
5
5
  SHA512:
6
- metadata.gz: 1eadd2c6dfba9ef8788e72514b3c12561e8628a20c6fc14bb21172d3777227bde42accb6dfe5cda14ccdeb027cb53415c179193cbd70bf1fa637b1bfe1208078
7
- data.tar.gz: 9e0f47ca1316e508faf65af727afb8a61de3a89e023d789d21ce69ca9328da46f4d1c9a197ee7486aba3c460476112bebf919803c26b7f0c06896f911b1ecfa6
6
+ metadata.gz: b982abdf30a063066e8d6d636cff2b144b80d25aca19aaadc13b2c9aa8f05a794f51362cd03877ad1113be6d08e4f0a038308eafed9c00a6aea25d10ef59db89
7
+ data.tar.gz: f783d819dea7744cfe54a1a7575cbf7657b56c0f836822a61eeb4b658e8e1eead2cc3cc1f2d120f06cca6e22e2c6ebda1d83ea3ce50a2050e5e37e2aa58fc4ee
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.1
data/lib/oydid/basic.rb CHANGED
@@ -124,7 +124,11 @@ class Oydid
124
124
  end
125
125
 
126
126
  def self.read_private_storage(filename)
127
- File.open(filename, 'r') { |f| f.read }
127
+ begin
128
+ File.open(filename, 'r') { |f| f.read }
129
+ rescue
130
+ nil
131
+ end
128
132
  end
129
133
 
130
134
  def self.get_location(id)
data/lib/oydid.rb CHANGED
@@ -139,9 +139,9 @@ class Oydid
139
139
 
140
140
  def self.generate_base(content, did, mode, options)
141
141
  # input validation
142
- did_doc = JSON.parse(content.to_json) rescue {}
143
- if did_doc == {}
144
- return [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "empty or invalid payload"]
142
+ did_doc = JSON.parse(content.to_json) rescue nil
143
+ if did_doc.nil?
144
+ return [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "invalid payload"]
145
145
  end
146
146
  did_old = nil
147
147
  log_old = nil
@@ -205,8 +205,38 @@ class Oydid
205
205
  did_old = did.dup
206
206
  did10_old = did10.dup
207
207
  log_old = did_info["log"]
208
- privateKey_old = read_private_storage(did10_old + "_private_key.b58")
209
- revocationKey_old = read_private_storage(did10_old + "_revocation_key.b58")
208
+ if options[:old_doc_key].nil?
209
+ if options[:old_doc_enc].nil?
210
+ if options[:old_doc_pwd].nil?
211
+ privateKey_old = read_private_storage(did10_old + "_private_key.b58")
212
+ else
213
+ privateKey_old, msg = generate_private_key(options[:old_doc_pwd].to_s, 'ed25519-priv')
214
+ end
215
+ else
216
+ privateKey_old, msg = decode_private_key(options[:old_doc_enc].to_s)
217
+ end
218
+ else
219
+ privateKey_old, msg = read_private_key(options[:old_doc_key].to_s)
220
+ end
221
+ if privateKey_old.nil?
222
+ return [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "invalid or missing old private document key"]
223
+ end
224
+ if options[:old_rev_key].nil?
225
+ if options[:old_rev_enc].nil?
226
+ if options[:old_rev_pwd].nil?
227
+ revocationKey_old = read_private_storage(did10_old + "_revocation_key.b58")
228
+ else
229
+ revocationKey_old, msg = generate_private_key(options[:old_rev_pwd].to_s, 'ed25519-priv')
230
+ end
231
+ else
232
+ revocationKey_old, msg = decode_private_key(options[:old_rev_enc].to_s)
233
+ end
234
+ else
235
+ revocationKey_old, msg = read_private_key(options[:old_rev_key].to_s)
236
+ end
237
+ if revocationKey_old.nil?
238
+ return [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "invalid or missing old private revocation key"]
239
+ end
210
240
 
211
241
  # key management
212
242
  if options[:doc_key].nil?
@@ -218,13 +248,19 @@ class Oydid
218
248
  else
219
249
  privateKey, msg = read_private_key(options[:doc_key].to_s)
220
250
  end
221
- if options[:rev_key].nil? && options[:rev_pwd].nil?
222
- revocationKey, msg = generate_private_key("", 'ed25519-priv')
223
- revocationLog = read_private_storage(did10 + "_revocation.json")
224
- else
251
+ # if options[:rev_key].nil? && options[:rev_pwd].nil? && options[:rev_enc].nil?
252
+ # revocationLog = read_private_storage(did10 + "_revocation.json")
253
+ # if revocationLog.nil?
254
+ # return [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "invalid or missing old revocation log"]
255
+ # end
256
+ # else
225
257
  if options[:rev_key].nil?
226
258
  if options[:rev_enc].nil?
227
- revocationKey, msg = generate_private_key(options[:rev_pwd].to_s, 'ed25519-priv')
259
+ if options[:rev_pwd].nil?
260
+ revocationKey, msg = generate_private_key("", 'ed25519-priv')
261
+ else
262
+ revocationKey, msg = generate_private_key(options[:rev_pwd].to_s, 'ed25519-priv')
263
+ end
228
264
  else
229
265
  revocationKey, msg = decode_private_key(options[:rev_enc].to_s)
230
266
  end
@@ -246,7 +282,7 @@ class Oydid
246
282
  "op": 1, # REVOKE
247
283
  "doc": subDidHash,
248
284
  "sig": signedSubDidHash }.transform_keys(&:to_s).to_json
249
- end
285
+ # end
250
286
  revoc_log = JSON.parse(revocationLog)
251
287
  revoc_log["previous"] = [
252
288
  hash(canonical(log_old[did_info["doc_log_id"].to_i])),
@@ -431,7 +467,7 @@ class Oydid
431
467
  end
432
468
  end
433
469
 
434
- def self.revoke(did, options)
470
+ def self.revoke_base(did, options)
435
471
  did_orig = did.dup
436
472
  doc_location = options[:doc_location]
437
473
  if options[:ts].nil?
@@ -462,25 +498,40 @@ class Oydid
462
498
  did_old = did.dup
463
499
  did10_old = did10.dup
464
500
  log_old = did_info["log"]
465
- privateKey_old = read_private_storage(did10_old + "_private_key.b58")
466
- revocationKey_old = read_private_storage(did10_old + "_revocation_key.b58")
467
501
 
468
- if options[:doc_key].nil?
469
- if options[:doc_pwd].nil?
470
- if options[:doc_enc].nil?
471
- privateKey, msg = read_private_key(did10 + "_private_key.b58")
502
+ if options[:old_doc_key].nil?
503
+ if options[:old_doc_enc].nil?
504
+ if options[:old_doc_pwd].nil?
505
+ privateKey_old = read_private_storage(did10_old + "_private_key.b58")
472
506
  else
473
- privateKey, msg = decode_private_key(options[:doc_enc].to_s)
507
+ privateKey_old, msg = generate_private_key(options[:old_doc_pwd].to_s, 'ed25519-priv')
474
508
  end
475
509
  else
476
- privateKey, msg = generate_private_key(options[:doc_pwd].to_s, 'ed25519-priv')
510
+ privateKey_old, msg = decode_private_key(options[:old_doc_enc].to_s)
477
511
  end
478
512
  else
479
- privateKey, msg = read_private_key(options[:doc_key].to_s)
513
+ privateKey_old, msg = read_private_key(options[:old_doc_key].to_s)
480
514
  end
481
- if privateKey.nil?
482
- return [nil, "private key not found"]
515
+ if privateKey_old.nil?
516
+ return [nil, "invalid or missing old private document key"]
483
517
  end
518
+ if options[:old_rev_key].nil?
519
+ if options[:old_rev_enc].nil?
520
+ if options[:old_rev_pwd].nil?
521
+ revocationKey_old = read_private_storage(did10_old + "_revocation_key.b58")
522
+ else
523
+ revocationKey_old, msg = generate_private_key(options[:old_rev_pwd].to_s, 'ed25519-priv')
524
+ end
525
+ else
526
+ revocationKey_old, msg = decode_private_key(options[:old_rev_enc].to_s)
527
+ end
528
+ else
529
+ revocationKey_old, msg = read_private_key(options[:old_rev_key].to_s)
530
+ end
531
+ if revocationKey_old.nil?
532
+ return [nil, "invalid or missing old private revocation key"]
533
+ end
534
+
484
535
  if options[:rev_key].nil? && options[:rev_pwd].nil? && options[:rev_enc].nil?
485
536
  revocationKey, msg = read_private_key(did10 + "_revocation_key.b58")
486
537
  revocationLog = read_private_storage(did10 + "_revocation.json")
@@ -502,7 +553,7 @@ class Oydid
502
553
  did_key_old = publicKey_old + ":" + pubRevoKey_old
503
554
  subDid = {"doc": did_old_doc, "key": did_key_old}.to_json
504
555
  subDidHash = hash(subDid)
505
- signedSubDidHash = sign(subDidHash, revocationKey).first
556
+ signedSubDidHash = sign(subDidHash, revocationKey_old).first
506
557
  revocationLog = {
507
558
  "ts": ts_old,
508
559
  "op": 1, # REVOKE
@@ -519,7 +570,18 @@ class Oydid
519
570
  hash(canonical(log_old[did_info["doc_log_id"].to_i])),
520
571
  hash(canonical(log_old[did_info["termination_log_id"].to_i]))
521
572
  ]
573
+ return [revoc_log, ""]
574
+ end
522
575
 
576
+ def self.revoke_publish(did, revoc_log, options)
577
+ did_hash = did.delete_prefix("did:oyd:")
578
+ did10 = did_hash[0,10]
579
+ doc_location = options[:doc_location]
580
+ if did_hash.include?(LOCATION_PREFIX)
581
+ hash_split = did_hash.split(LOCATION_PREFIX)
582
+ did_hash = hash_split[0]
583
+ doc_location = hash_split[1]
584
+ end
523
585
  if doc_location.to_s == ""
524
586
  doc_location = DEFAULT_LOCATION
525
587
  end
@@ -527,22 +589,29 @@ class Oydid
527
589
  # publish revocation log based on location
528
590
  case doc_location.to_s
529
591
  when /^http/
530
- retVal = HTTParty.post(doc_location.to_s + "/log/" + did.to_s,
592
+ retVal = HTTParty.post(doc_location.to_s + "/log/" + did_hash.to_s,
531
593
  headers: { 'Content-Type' => 'application/json' },
532
594
  body: {"log": revoc_log}.to_json )
533
595
  if retVal.code != 200
534
- msg = retVal.parsed_response("error").to_s rescue "invalid response from " + doc_location.to_s + "/log/" + did.to_s
596
+ msg = retVal.parsed_response("error").to_s rescue "invalid response from " + doc_location.to_s + "/log/" + did_hash.to_s
535
597
  return [nil, msg]
536
598
  end
537
599
  else
538
- File.write(did10 + ".log", [log_old, revoc_log].flatten.compact.to_json)
600
+ File.write(did10 + ".log", revoc_log.to_json)
539
601
  if !did_old.nil?
540
- File.write(did10_old + ".log", [log_old, revoc_log].flatten.compact.to_json)
602
+ File.write(did10_old + ".log", revoc_log.to_json)
541
603
  end
542
604
  end
543
605
 
544
- return [did_orig, ""]
606
+ return [did, ""]
607
+ end
545
608
 
609
+ def self.revoke(did, options)
610
+ revoc_log, msg = revoke_base(did, options)
611
+ if revoc_log.nil?
612
+ return [nil, msg]
613
+ end
614
+ success, msg = revoke_publish(did, revoc_log, options)
546
615
  end
547
616
 
548
617
  def self.clone(did, options)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oydid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christoph Fabianek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-14 00:00:00.000000000 Z
11
+ date: 2022-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dag