nostrbase 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4252ebff99188df89893e516ba40d718e8a99baaba91612c1e23a3c07bd77c54
4
+ data.tar.gz: 3ab947cbfdf72ff1f9a0ed773b5cb37a91dd9f93f64798cbc9b234728db87e9e
5
+ SHA512:
6
+ metadata.gz: 878e1eddc9932fc90afff2c0936f8b86ba2adbb0b751d5d313f75bf96bad92d8c13f1597b7bce3dd615eb78343598895d61376d5999fd2f47ce8b18a1f9935ab
7
+ data.tar.gz: dd3400c561d7945e038a03c84fdc80880ce087091814753ccd9352ac5e90e2046a7173a05d93a7654f58df027ce8db4874e6dd5012dc2548e734a1625852e7ee
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 3.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-11-27
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Viktor Vsk
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,23 @@
1
+ # Nostrbase
2
+
3
+ TODO:
4
+
5
+ ## Installation
6
+
7
+ TODO:
8
+
9
+ ## Usage
10
+
11
+ TODO:
12
+
13
+ ## Development
14
+
15
+ TODO:
16
+
17
+ ## Contributing
18
+
19
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nostrbase.
20
+
21
+ ## License
22
+
23
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
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
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,92 @@
1
+ module Nostrbase
2
+ class Event
3
+ attr_reader :id, :pubkey, :created_at, :kind, :tags, :content, :sig, :errors
4
+
5
+ def self.sign_keys
6
+ @keys ||= Keys.new
7
+ end
8
+
9
+ def self.sign_secret=(secret_hex)
10
+ @keys = Keys.new(secret_hex)
11
+ end
12
+
13
+ def initialize(hash_or_json_payload)
14
+ payload = hash_or_json_payload.is_a?(Hash) ? hash_or_json_payload.map { |key, v| [key.to_s, v] }.to_h : JSON.parse(hash_or_json_payload)
15
+
16
+ @id = payload["id"]
17
+ @pubkey = payload["pubkey"]
18
+ @created_at = payload["created_at"]
19
+ @kind = payload["kind"]
20
+ @tags = payload["tags"]
21
+ @content = payload["content"]
22
+ @sig = payload["sig"]
23
+
24
+ @raw_payload = hash_or_json_payload
25
+ @errors = []
26
+ end
27
+
28
+ def signed?
29
+ sig.to_s.length.positive? && id.to_s.length.positive? && pubkey.to_s.length.positive?
30
+ end
31
+
32
+ def sign(allow_resign: false, keys: nil)
33
+ return false if signed? && !allow_resign
34
+
35
+ keys = self.class.sign_keys if keys.nil?
36
+
37
+ @pubkey = keys.public
38
+ @id = Digest::SHA256.hexdigest(JSON.dump(to_nostr_serialized))
39
+ @sig = keys.sign(id)
40
+ end
41
+
42
+ def sign!(keys: nil)
43
+ raise AlreadySigned, "Can't signed event that already has sig, id or pubkey present" if signed?
44
+ sign(keys: keys)
45
+ end
46
+
47
+ def resign(keys: nil)
48
+ sign(allow_resign: true, keys: keys)
49
+ end
50
+
51
+ def valid_schema?
52
+ @errors = NostrEventSchema.validate(@raw_payload)
53
+ @errors.size.negative?
54
+ end
55
+
56
+ def valid_id?
57
+ Digest::SHA256.hexdigest(JSON.dump(to_nostr_serialized)) === id
58
+ end
59
+
60
+ def valid_signature?
61
+ schnorr_sig = Secp256k1::SchnorrSignature.from_data([sig].pack("H*"))
62
+ schnorr_pubkey = Secp256k1::XOnlyPublicKey.from_data([pubkey].pack("H*"))
63
+
64
+ schnorr_sig.verify([id].pack("H*"), schnorr_pubkey)
65
+ rescue Secp256k1::DeserializationError
66
+ false
67
+ end
68
+
69
+ def to_nostr_serialized
70
+ [
71
+ 0,
72
+ pubkey,
73
+ created_at.to_i,
74
+ kind.to_i,
75
+ tags,
76
+ content.to_s
77
+ ]
78
+ end
79
+
80
+ def as_json
81
+ {
82
+ id:,
83
+ pubkey:,
84
+ created_at:,
85
+ kind:,
86
+ tags:,
87
+ content:,
88
+ sig:
89
+ }
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,20 @@
1
+ module Nostrbase
2
+ class Keys
3
+ attr_reader :secret, :public
4
+
5
+ CONTEXT = Secp256k1::Context.create
6
+
7
+ def initialize(secret_hex = nil)
8
+ private_key_data = secret_hex ? Secp256k1::Util.hex_to_bin(secret_hex) : SecureRandom.random_bytes(32)
9
+ @secret = Secp256k1::Util.bin_to_hex(Secp256k1::PrivateKey.from_data(private_key_data).data)
10
+ @key_pair = CONTEXT.key_pair_from_private_key(private_key_data)
11
+
12
+ # Nostr ignores first byte of pubkey, more details here https://bips.xyz/340
13
+ @public = Secp256k1::Util.bin_to_hex(@key_pair.public_key.compressed)[2..]
14
+ end
15
+
16
+ def sign(message)
17
+ CONTEXT.sign_schnorr(@key_pair, [message].pack("H*")).serialized.unpack1("H*")
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,68 @@
1
+ module Nostrbase
2
+ class NostrEventSchema
3
+ SCHEMA = JSONSchemer.schema({
4
+ "$schema": "http://json-schema.org/draft-07/schema#",
5
+ id: "nostr/nips/01/commands/client/EVENT/event/",
6
+ type: "object",
7
+ required: %w[content created_at id kind pubkey sig tags],
8
+ properties: {
9
+ content: {
10
+ type: "string",
11
+ id: "content"
12
+ },
13
+ created_at: {
14
+ type: "intger",
15
+ id: "created_at",
16
+ minimum: 0
17
+ },
18
+ id: {
19
+ type: "string",
20
+ id: "id",
21
+ minLength: 64,
22
+ maxLength: 64
23
+ },
24
+ kind: {
25
+ type: "integer",
26
+ id: "kind",
27
+ minimum: 0,
28
+ maxumum: 65535
29
+ },
30
+ pubkey: {
31
+ type: "string",
32
+ id: "pubkey",
33
+ minLength: 64,
34
+ maxLength: 64
35
+ },
36
+ sig: {
37
+ type: "string",
38
+ id: "sig",
39
+ minLength: 128,
40
+ maxLength: 128
41
+ },
42
+ tags: {
43
+ type: "array",
44
+ id: "tags/",
45
+ items: [
46
+ {
47
+ type: "array",
48
+ prefixItems: {
49
+ type: "string",
50
+ minLength: 1
51
+ },
52
+ minLength: 1
53
+ }
54
+ ]
55
+ }
56
+ }
57
+
58
+ }.to_json)
59
+
60
+ def self.validate(payload)
61
+ errors = []
62
+
63
+ SCHEMA.validate(payload).each { |error| errors.push(key: error["data_pointer"], value: JSONSchemer::Errors.pretty(error)) }
64
+
65
+ errors
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nostrbase
4
+ VERSION = "0.1.0"
5
+ end
data/lib/nostrbase.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "digest"
5
+ require "json_schemer"
6
+ require "rbsecp256k1"
7
+
8
+ require_relative "nostrbase/version"
9
+
10
+ require_relative "nostrbase/nostr_event_schema"
11
+ require_relative "nostrbase/event"
12
+ require_relative "nostrbase/keys"
13
+
14
+ module Nostrbase
15
+ class AlreadySigned < StandardError; end
16
+ end
data/sig/nostrbase.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Nostrbase
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nostrbase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Viktor Vsk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rbsecp256k1
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json_schemer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2'
41
+ description: Create private and public keys, sign events and validate their signature
42
+ and schema
43
+ email:
44
+ - me@viktorvsk.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".rspec"
50
+ - ".standard.yml"
51
+ - CHANGELOG.md
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/nostrbase.rb
56
+ - lib/nostrbase/event.rb
57
+ - lib/nostrbase/keys.rb
58
+ - lib/nostrbase/nostr_event_schema.rb
59
+ - lib/nostrbase/version.rb
60
+ - sig/nostrbase.rbs
61
+ homepage: https://saltivka.org
62
+ licenses:
63
+ - MIT
64
+ metadata:
65
+ homepage_uri: https://saltivka.org
66
+ source_code_uri: https://saltivka.org
67
+ changelog_uri: https://saltivka.org
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 3.2.2
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.5.0.dev
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Simple Nostr keys and events management
87
+ test_files: []