sidetree 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 55fc6b6c312458a0e0d627daeb1f1f5cd84e87a6f1b74c0a9a9d584848e31a8a
4
+ data.tar.gz: 34a4aa6ee3457c84b0d1148108d19f5e7afea5f32d1210299ca530bdd00d6a59
5
+ SHA512:
6
+ metadata.gz: 47cfc0743c8da5ccf12bae7fa2114474607a0f68814fccf0738390f87bc52855e96b2eaedc70b68328a8b7b0b7dbc892f6eba199a0ba87b842b369e71603d472
7
+ data.tar.gz: 0032c7d6faedaf3873fdc1594f19eb3ceb431129cb5fd2dd436e322dfb9b5a155e5901c87c0530e9b42bb6d3ca01562e393c83bbc62f51ec61d4f4c4c37a8c25
@@ -0,0 +1,22 @@
1
+ name: Ruby
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ strategy:
8
+ fail-fast: true
9
+ matrix:
10
+ ruby: [2.6, 2.7, 3.0, 3.1]
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - name: Set up Ruby
15
+ uses: ruby/setup-ruby@v1
16
+ with:
17
+ ruby-version: ${{ matrix.ruby }}
18
+ bundler-cache: true
19
+ - name: Run prettier
20
+ run: bundle exec rbprettier --check '**/*.rb'
21
+ - name: Run the default task
22
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.prettierignore ADDED
@@ -0,0 +1,9 @@
1
+ /bin
2
+ /log
3
+ /tmp
4
+ /vendor
5
+ Gemfile.lock
6
+ *.txt
7
+ /.idea/
8
+ /.*
9
+ /spec/fixtures/*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ sidetreerb
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-3.1.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-02-25
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ - Demonstrating empathy and kindness toward other people
14
+ - Being respectful of differing opinions, viewpoints, and experiences
15
+ - Giving and gracefully accepting constructive feedback
16
+ - Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ - Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ - The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ - Trolling, insulting or derogatory comments, and personal or political attacks
24
+ - Public or private harassment
25
+ - Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ - Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at azuchi@chaintope.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in sidetree.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 azuchi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # [WIP]Sidetree for Ruby
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sidetree`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sidetree'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sidetree
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sidetree. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/sidetree/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Sidetree project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sidetree/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'sidetree'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/ion ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+ require 'thor'
3
+ require 'sidetree'
4
+
5
+ class ION < Thor
6
+ class << self
7
+ def exit_on_failure?
8
+ true
9
+ end
10
+ end
11
+
12
+ desc 'create', 'Create ION DID.'
13
+ def create
14
+ recovery_key = Sidetree::Key.generate
15
+ update_key = Sidetree::Key.generate
16
+ signing_key = Sidetree::Key.generate(id: 'signing-key')
17
+ document = Sidetree::Model::Document.new(public_keys: [signing_key])
18
+ did = Sidetree::DID.create(document, update_key, recovery_key, method: Sidetree::Params::METHODS[:ion])
19
+
20
+ puts "DID: #{did.short_form}"
21
+ puts
22
+ puts "Recovery key: #{recovery_key.encoded_private_key}"
23
+ puts "Update key: #{update_key.encoded_private_key}"
24
+ puts "Signing key: #{signing_key.encoded_private_key}"
25
+ puts
26
+
27
+ puts "Long-Form DID: #{did.to_s}"
28
+ op = did.create_op
29
+ puts "DID suffix data: #{op.suffix.to_h.to_json}"
30
+ puts "Document delta: #{op.delta.to_h.to_json}"
31
+ end
32
+ end
33
+
34
+ ION.start(ARGV)
@@ -0,0 +1,91 @@
1
+ module Sidetree
2
+ class DID
3
+ attr_reader :method
4
+ attr_reader :suffix
5
+ attr_reader :long_suffix
6
+
7
+ # @raise [Sidetree::Error]
8
+ def initialize(did)
9
+ if !did.start_with?('did:ion:') && !did.start_with?('did:sidetree:')
10
+ raise Error, 'Expected DID method not given in DID.'
11
+ end
12
+ if did.count(':') > (Sidetree::Params.testnet? ? 4 : 3)
13
+ raise Error, 'Unsupported DID format.'
14
+ end
15
+ if Sidetree::Params.testnet?
16
+ _, @method, _, @suffix, @long_suffix = did.split(':')
17
+ else
18
+ _, @method, @suffix, @long_suffix = did.split(':')
19
+ end
20
+
21
+ if @long_suffix
22
+ unless suffix == create_op.suffix.unique_suffix
23
+ raise Error, 'DID document mismatches short-form DID.'
24
+ end
25
+ end
26
+ end
27
+
28
+ # Create DID from +document+, +update_key+ and +recovery_key+.
29
+ # @param [Sidetree::Model::Document] document
30
+ # @param [Sidetree::Key] update_key update key
31
+ # @param [Sidetree::Key] recovery_key recovery key
32
+ # @param [String] method DID method, default value is sidetree.
33
+ # @raise [Sidetree::Error]
34
+ # @return [Sidetree::DID]
35
+ def self.create(
36
+ document,
37
+ update_key,
38
+ recovery_key,
39
+ method: Sidetree::Params::DEFAULT_METHOD
40
+ )
41
+ unless document.is_a?(Sidetree::Model::Document)
42
+ raise Error, 'document must be Sidetree::Model::Document instance.'
43
+ end
44
+ unless update_key.is_a?(Sidetree::Key)
45
+ raise Error, 'update_key must be Sidetree::Key instance.'
46
+ end
47
+ unless recovery_key.is_a?(Sidetree::Key)
48
+ raise Error, 'recovery_key must be Sidetree::Key instance.'
49
+ end
50
+
51
+ patches = [
52
+ { 'action': OP::PatchAction::REPLACE, 'document': document.to_h }
53
+ ]
54
+ delta = Model::Delta.new(patches, update_key.to_commitment)
55
+ suffix =
56
+ Sidetree::Model::Suffix.new(delta.to_hash, recovery_key.to_commitment)
57
+ DID.new(
58
+ OP::Create.new(suffix, delta).did(method: method, include_long: true)
59
+ )
60
+ end
61
+
62
+ def short_form?
63
+ long_suffix.nil?
64
+ end
65
+
66
+ def long_form?
67
+ !short_form?
68
+ end
69
+
70
+ def create_op
71
+ long_form? ? OP::Create.from_base64(@long_suffix) : nil
72
+ end
73
+
74
+ # Return short form did.
75
+ # @return [String]
76
+ def short_form
77
+ did = "did:#{method}"
78
+ did += ":#{Sidetree::Params.network}" if Sidetree::Params.testnet?
79
+ did += ":#{suffix}"
80
+ did
81
+ end
82
+
83
+ # Return DID string.
84
+ # @return [String]
85
+ def to_s
86
+ did = short_form
87
+ did += ":#{long_suffix}" if long_form?
88
+ did
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,181 @@
1
+ module Sidetree
2
+ class Key
3
+ attr_reader :private_key, :public_key, :id, :purposes, :type
4
+
5
+ # @param [Integer] private_key private key.
6
+ # @param [ECDSA::Point] public_key public key
7
+ # @param [String] id
8
+ # @param [Array[String]] purposes
9
+ # @param [String] type
10
+ def initialize(
11
+ private_key: nil,
12
+ public_key: nil,
13
+ id: nil,
14
+ purposes: [],
15
+ type: nil
16
+ )
17
+ if private_key
18
+ unless Key.valid_private_key?(private_key)
19
+ raise Error, 'private key is invalid range.'
20
+ end
21
+
22
+ @private_key = private_key
23
+ pub = ECDSA::Group::Secp256k1.generator.multiply_by_scalar(private_key)
24
+ if public_key
25
+ unless pub == public_key
26
+ raise Error, 'Public and private keys do not match.'
27
+ end
28
+ else
29
+ public_key = pub
30
+ end
31
+ end
32
+
33
+ unless public_key
34
+ raise Error, 'Specify either the private key or the public key'
35
+ end
36
+ unless public_key.is_a?(ECDSA::Point)
37
+ raise Error, 'public key must be an ECDSA::Point instance.'
38
+ end
39
+ unless ECDSA::Group::Secp256k1.valid_public_key?(public_key)
40
+ raise Error, 'public key is invalid.'
41
+ end
42
+
43
+ @public_key = public_key
44
+
45
+ purposes.each do |purpose|
46
+ if purpose && !Sidetree::OP::PublicKeyPurpose.values.include?(purpose)
47
+ raise Error, "Unknown purpose '#{purpose}' specified."
48
+ end
49
+ end
50
+
51
+ Sidetree::Validator.validate_id!(id) if id
52
+
53
+ @purposes = purposes
54
+ @id = id
55
+ @type = type
56
+ end
57
+
58
+ # Generate Secp256k1 key.
59
+ # @option [String] id Public key ID.
60
+ # @option [String] purpose Purpose for public key. Supported values defined by [Sidetree::PublicKeyPurpose].
61
+ # @return [Sidetree::Key]
62
+ # @raise [Sidetree::Error]
63
+ def self.generate(id: nil, purposes: [])
64
+ private_key =
65
+ 1 + SecureRandom.random_number(ECDSA::Group::Secp256k1.order - 1)
66
+ Key.new(private_key: private_key, purposes: purposes, id: id)
67
+ end
68
+
69
+ # Generate key instance from jwk Hash.
70
+ # @param [Hash] data jwk Hash object.
71
+ # @return [Sidetree::Key]
72
+ # @raise [Sidetree::Error]
73
+ def self.from_hash(data)
74
+ key_data = data['publicKeyJwk'] ? data['publicKeyJwk'] : data
75
+ key_type = key_data['kty']
76
+ curve = key_data['crv']
77
+ if key_type.nil? || key_type != 'EC'
78
+ raise Error, "Unsupported key type '#{key_type}' specified."
79
+ end
80
+ if curve.nil? || curve != 'secp256k1'
81
+ raise Error, "Unsupported curve '#{curve}' specified."
82
+ end
83
+ raise Error, 'x property required.' unless key_data['x']
84
+ raise Error, 'y property required.' unless key_data['y']
85
+
86
+ # `x` and `y` need 43 Base64URL encoded bytes to contain 256 bits.
87
+ unless key_data['x'].length == 43
88
+ raise Error, "Secp256k1 JWK 'x' property must be 43 bytes."
89
+ end
90
+ unless key_data['y'].length == 43
91
+ raise Error, "Secp256k1 JWK 'y' property must be 43 bytes."
92
+ end
93
+
94
+ x = Base64.urlsafe_decode64(key_data['x'])
95
+ y = Base64.urlsafe_decode64(key_data['y'])
96
+ point =
97
+ ECDSA::Format::PointOctetString.decode(
98
+ ['04'].pack('H*') + x + y,
99
+ ECDSA::Group::Secp256k1
100
+ )
101
+ private_key =
102
+ if key_data['d']
103
+ Base64.urlsafe_decode64(key_data['d']).unpack1('H*').to_i(16)
104
+ else
105
+ nil
106
+ end
107
+
108
+ purposes = data['purposes'] ? data['purposes'] : []
109
+ Key.new(
110
+ public_key: point,
111
+ private_key: private_key,
112
+ purposes: purposes,
113
+ id: data['id'],
114
+ type: data['type']
115
+ )
116
+ end
117
+
118
+ # Check whether private is valid range.
119
+ # @param [Integer] private_key
120
+ # @return [Boolean]
121
+ def self.valid_private_key?(private_key)
122
+ 0x01 <= private_key && private_key < ECDSA::Group::Secp256k1.order
123
+ end
124
+
125
+ # Generate JSON::JWK object.
126
+ # @return [JSON::JWK]
127
+ def to_jwk
128
+ jwk =
129
+ JSON::JWK.new(
130
+ kty: 'EC',
131
+ crv: 'secp256k1',
132
+ x:
133
+ Base64.urlsafe_encode64(
134
+ ECDSA::Format::FieldElementOctetString.encode(
135
+ public_key.x,
136
+ public_key.group.field
137
+ ),
138
+ padding: false
139
+ ),
140
+ y:
141
+ Base64.urlsafe_encode64(
142
+ ECDSA::Format::FieldElementOctetString.encode(
143
+ public_key.y,
144
+ public_key.group.field
145
+ ),
146
+ padding: false
147
+ )
148
+ )
149
+ jwk['d'] = encoded_private_key if private_key
150
+ jwk
151
+ end
152
+
153
+ # Generate commitment for this key.
154
+ # @return [String] Base64 encoded commitment.
155
+ def to_commitment
156
+ digest = Digest::SHA256.digest(to_jwk.normalize.to_json_c14n)
157
+
158
+ Sidetree.to_hash(digest)
159
+ end
160
+
161
+ def to_h
162
+ h = { publicKeyJwk: to_jwk.normalize, purposes: purposes }
163
+ h[:id] = id if id
164
+ h[:type] = type if type
165
+ h.stringify_keys
166
+ end
167
+
168
+ # Return base64 encoded private key.
169
+ # @return [String]
170
+ def encoded_private_key
171
+ if private_key
172
+ Base64.urlsafe_encode64(
173
+ [private_key.to_s(16).rjust(32 * 2, '0')].pack('H*'),
174
+ padding: false
175
+ )
176
+ else
177
+ nil
178
+ end
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,28 @@
1
+ module Sidetree
2
+ module Model
3
+ class Delta
4
+ attr_reader :patches, :update_commitment
5
+
6
+ # @param [Array[Hash]] patches
7
+ # @param [String] update_commitment
8
+ # @raise [Sidetree::Error]
9
+ def initialize(patches, update_commitment)
10
+ @patches = patches
11
+ @update_commitment = update_commitment
12
+ end
13
+
14
+ def self.parse(object)
15
+ Sidetree::Validator.validate_delta!(object)
16
+ Delta.new(object[:patches], object[:updateCommitment])
17
+ end
18
+
19
+ def to_h
20
+ { patches: patches, updateCommitment: update_commitment }
21
+ end
22
+
23
+ def to_hash
24
+ Sidetree.to_hash(to_h)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,40 @@
1
+ module Sidetree
2
+ module Model
3
+ # DID document class.
4
+ class Document
5
+ attr_reader :public_keys
6
+ attr_reader :services
7
+
8
+ # @param [Array[Sidetree::Key]] public_keys The array of public keys.
9
+ # @param [Array[Sidetree::Model::Service]] services The array of service.
10
+ def initialize(public_keys: [], services: [])
11
+ public_keys.each do |public_key|
12
+ unless public_key.is_a?(Sidetree::Key)
13
+ raise Error, 'public_keys should be array of Sidetree::Key objects.'
14
+ end
15
+ end
16
+ id_set = public_keys.map(&:id)
17
+ if (id_set.count - id_set.uniq.count) > 0
18
+ raise Error 'Public key id has to be unique.'
19
+ end
20
+ services.each do |service|
21
+ unless service.is_a?(Sidetree::Model::Service)
22
+ raise Error,
23
+ 'services should be array of Sidetree::Model::Service objects.'
24
+ end
25
+ end
26
+ id_set = services.map(&:id)
27
+ if (id_set.count - id_set.uniq.count) > 0
28
+ raise Error 'Service id has to be unique.'
29
+ end
30
+
31
+ @public_keys = public_keys
32
+ @services = services
33
+ end
34
+
35
+ def to_h
36
+ { publicKeys: public_keys.map(&:to_h), services: services.map(&:to_h) }
37
+ end
38
+ end
39
+ end
40
+ end