truthid-sdk 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: c3de2db41a1a8ad39fefae599250e957b77ae4f8b37ed27cf7db90331a6b2d38
4
+ data.tar.gz: 3df13b1958fbb7cd0daa4896e3e74a97e28103fcfdf550b1ab36d527d135b6ae
5
+ SHA512:
6
+ metadata.gz: ee362147ab288e09c41a734d1206f4b3dcf48c440cec13c28bbdff54da1ceb420ff66723eb3a1d6a92adba8f817171bfde9c25a48b84c19274848e35cdc986b5
7
+ data.tar.gz: 41d7cb1bbf8bfc650802963923635bebfab7314707306cea33ee96a868e6a07dfad1c7e83b79deb7f481c98f9d0fedab1e8d477cbdf3e250299082bf2078202c
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 masterlxz
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # truthid-sdk
2
+
3
+ TruthID passwordless, decentralized authentication SDK for Ruby.
4
+
5
+ Integrate passwordless, decentralized authentication into your app in minutes — no TruthID-operated server, no passwords, no third-party login.
6
+
7
+ ```bash
8
+ gem install truthid-sdk
9
+ ```
10
+
11
+ Full documentation, how it works, and usage examples: [github.com/masterlxz/truthid/tree/main/sdk](https://github.com/masterlxz/truthid/tree/main/sdk#readme)
12
+
13
+ ## License
14
+
15
+ MIT — see [LICENSE](./LICENSE).
@@ -0,0 +1,116 @@
1
+ require "json"
2
+ require "securerandom"
3
+ require "eth"
4
+
5
+ require_relative "contracts"
6
+ require_relative "types"
7
+
8
+ module TruthID
9
+ class Client
10
+ RPC_URLS = {
11
+ "base-sepolia" => "https://sepolia.base.org",
12
+ "base-mainnet" => "https://mainnet.base.org"
13
+ }.freeze
14
+
15
+ def initialize(network: "base-mainnet", rpc_url: nil)
16
+ url = rpc_url || RPC_URLS.fetch(network)
17
+ @rpc = Eth::Client.create(url)
18
+ @devices = Eth::Contract.from_abi(
19
+ name: "DeviceRegistry",
20
+ address: Contracts::DEVICE_REGISTRY_ADDRESSES.fetch(network),
21
+ abi: Contracts::DEVICE_REGISTRY_ABI
22
+ )
23
+ @sessions = Eth::Contract.from_abi(
24
+ name: "SessionRegistry",
25
+ address: Contracts::SESSION_REGISTRY_ADDRESSES.fetch(network),
26
+ abi: Contracts::SESSION_REGISTRY_ABI
27
+ )
28
+ end
29
+
30
+ def create_challenge(origin)
31
+ AuthChallenge.new(
32
+ type: "challenge",
33
+ nonce: SecureRandom.uuid,
34
+ issued_at: (Time.now.to_f * 1000).to_i,
35
+ origin: origin
36
+ )
37
+ end
38
+
39
+ def verify_auth_response(challenge, response, ttl_ms: 30_000)
40
+ # 1. Usuário recusou
41
+ unless response.approved
42
+ return VerifyAuthResult.new(valid: false, reason: "User rejected the login request")
43
+ end
44
+
45
+ # 2. TTL expirado
46
+ now_ms = (Time.now.to_f * 1000).to_i
47
+ if now_ms - challenge.issued_at > ttl_ms
48
+ return VerifyAuthResult.new(valid: false, reason: "Challenge expired")
49
+ end
50
+
51
+ # 3. Nonce bate com o challenge original
52
+ if challenge.nonce != response.nonce
53
+ return VerifyAuthResult.new(valid: false, reason: "Nonce mismatch")
54
+ end
55
+
56
+ # 4. Verificar assinatura
57
+ # JSON.generate já produz JSON compacto (sem espaços) — compatível com Dart e JS
58
+ message = JSON.generate(challenge.to_h)
59
+ begin
60
+ signer = Eth::Signature.personal_recover(message, response.signature)
61
+ rescue => e
62
+ return VerifyAuthResult.new(valid: false, reason: "Invalid signature format")
63
+ end
64
+
65
+ if signer.downcase != response.device_address.downcase
66
+ return VerifyAuthResult.new(valid: false, reason: "Signature does not match device address")
67
+ end
68
+
69
+ # 5. Device ativo na blockchain
70
+ is_active = @rpc.call(@devices, "isDeviceActive", response.device_address)
71
+ unless is_active
72
+ return VerifyAuthResult.new(valid: false, reason: "Device is not active or has been revoked")
73
+ end
74
+
75
+ # 6. Buscar identityId do device
76
+ device = @rpc.call(@devices, "getDevice", response.device_address)
77
+
78
+ VerifyAuthResult.new(
79
+ valid: true,
80
+ identity_id: device[0],
81
+ device_address: response.device_address
82
+ )
83
+ end
84
+
85
+ def verify_session(session_hash)
86
+ # bytes32: converte hex string → 32 bytes binários
87
+ hash_bytes = [session_hash.delete_prefix("0x")].pack("H*")
88
+
89
+ session = @rpc.call(@sessions, "getSession", hash_bytes)
90
+ return SessionInfo.new(exists: false, revoked: false) unless session[4] # exists
91
+
92
+ revoked = @rpc.call(@sessions, "isSessionRevoked", hash_bytes)
93
+
94
+ SessionInfo.new(
95
+ exists: true,
96
+ revoked: revoked,
97
+ identity_id: session[0],
98
+ device_pub_key: session[1],
99
+ created_at: Time.at(session[2]).utc
100
+ )
101
+ end
102
+
103
+ def check_device_status(device_pub_key)
104
+ device = @rpc.call(@devices, "getDevice", device_pub_key)
105
+ return DeviceStatus.new(exists: false, active: false) unless device[5] # exists
106
+
107
+ DeviceStatus.new(
108
+ exists: true,
109
+ active: !device[4], # revoked → active é o inverso
110
+ label: device[2],
111
+ identity_id: device[0],
112
+ added_at: Time.at(device[3]).utc
113
+ )
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,70 @@
1
+ module TruthID
2
+ module Contracts
3
+ DEVICE_REGISTRY_ADDRESSES = {
4
+ "base-sepolia" => "0x225c67a98c9D675fE595ae05a2F9249C34d9C60a",
5
+ "base-mainnet" => "0x4A7a307cb6872bde24BAf3E9de2BeC3Ddd03e144"
6
+ }.freeze
7
+ DEVICE_REGISTRY_ABI = [
8
+ {
9
+ "type" => "function",
10
+ "name" => "isDeviceActive",
11
+ "inputs" => [{ "name" => "devicePubKey", "type" => "address" }],
12
+ "outputs" => [{ "name" => "", "type" => "bool" }],
13
+ "stateMutability" => "view"
14
+ },
15
+ {
16
+ "type" => "function",
17
+ "name" => "getDevice",
18
+ "inputs" => [{ "name" => "devicePubKey", "type" => "address" }],
19
+ "outputs" => [
20
+ {
21
+ "name" => "",
22
+ "type" => "tuple",
23
+ "components" => [
24
+ { "name" => "identityId", "type" => "uint256" },
25
+ { "name" => "pubKey", "type" => "address" },
26
+ { "name" => "label", "type" => "string" },
27
+ { "name" => "addedAt", "type" => "uint256" },
28
+ { "name" => "revoked", "type" => "bool" },
29
+ { "name" => "exists", "type" => "bool" }
30
+ ]
31
+ }
32
+ ],
33
+ "stateMutability" => "view"
34
+ }
35
+ ].freeze
36
+
37
+ SESSION_REGISTRY_ADDRESSES = {
38
+ "base-sepolia" => "0xdeD2Ad865069CA6546172926540D3A3Aa73C1CA6",
39
+ "base-mainnet" => "0x24074587a2aFB3aa5491361BB0a5eBee90797D1B"
40
+ }.freeze
41
+ SESSION_REGISTRY_ABI = [
42
+ {
43
+ "type" => "function",
44
+ "name" => "isSessionRevoked",
45
+ "inputs" => [{ "name" => "hash", "type" => "bytes32" }],
46
+ "outputs" => [{ "name" => "", "type" => "bool" }],
47
+ "stateMutability" => "view"
48
+ },
49
+ {
50
+ "type" => "function",
51
+ "name" => "getSession",
52
+ "inputs" => [{ "name" => "hash", "type" => "bytes32" }],
53
+ "outputs" => [
54
+ {
55
+ "name" => "",
56
+ "type" => "tuple",
57
+ "components" => [
58
+ { "name" => "identityId", "type" => "uint256" },
59
+ { "name" => "devicePubKey","type" => "address" },
60
+ { "name" => "createdAt", "type" => "uint256" },
61
+ { "name" => "revoked", "type" => "bool" },
62
+ { "name" => "exists", "type" => "bool" }
63
+ ]
64
+ }
65
+ ],
66
+ "stateMutability" => "view"
67
+ }
68
+ ].freeze
69
+ end
70
+ end
@@ -0,0 +1,50 @@
1
+ require "json"
2
+
3
+ module TruthID
4
+ # AuthChallenge precisa de to_h com camelCase — por isso classe manual, não Struct
5
+ class AuthChallenge
6
+ attr_reader :type, :nonce, :issued_at, :origin
7
+
8
+ def initialize(type:, nonce:, issued_at:, origin:)
9
+ @type = type
10
+ @nonce = nonce
11
+ @issued_at = issued_at
12
+ @origin = origin
13
+ end
14
+
15
+ # JSON.generate(challenge.to_h) → formato exato que o mobile assina
16
+ def to_h
17
+ { "type" => @type, "nonce" => @nonce, "issuedAt" => @issued_at, "origin" => @origin }
18
+ end
19
+
20
+ def to_json(*args)
21
+ JSON.generate(to_h)
22
+ end
23
+ end
24
+
25
+ # AuthResponse é o que chega do mobile (chaves camelCase do JSON mapeadas manualmente)
26
+ class AuthResponse
27
+ attr_reader :approved, :nonce, :signature, :device_address
28
+
29
+ def initialize(approved:, nonce:, signature:, device_address:)
30
+ @approved = approved
31
+ @nonce = nonce
32
+ @signature = signature
33
+ @device_address = device_address
34
+ end
35
+
36
+ def self.from_hash(h)
37
+ new(
38
+ approved: h["approved"],
39
+ nonce: h["nonce"],
40
+ signature: h["signature"],
41
+ device_address: h["deviceAddress"]
42
+ )
43
+ end
44
+ end
45
+
46
+ # Tipos de resultado: snake_case, sem necessidade de conversão JSON
47
+ VerifyAuthResult = Struct.new(:valid, :identity_id, :device_address, :reason, keyword_init: true)
48
+ SessionInfo = Struct.new(:exists, :revoked, :identity_id, :device_pub_key, :created_at, keyword_init: true)
49
+ DeviceStatus = Struct.new(:exists, :active, :label, :identity_id, :added_at, keyword_init: true)
50
+ end
data/lib/truthid.rb ADDED
@@ -0,0 +1,13 @@
1
+ require_relative "truthid/contracts"
2
+ require_relative "truthid/types"
3
+ require_relative "truthid/client"
4
+
5
+ module TruthID
6
+ # Alias para manter a API consistente com os outros SDKs
7
+ # TypeScript: new TruthIDClient(...)
8
+ # Python: TruthIDClient(...)
9
+ # Ruby: TruthID::Client.new(...) ou TruthID.new_client(...)
10
+ def self.new_client(network: "base-mainnet", rpc_url: nil)
11
+ Client.new(network: network, rpc_url: rpc_url)
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: truthid-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - masterlxz
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: eth
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '0.5'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '0.5'
26
+ description: TruthID passwordless, decentralized authentication SDK for Ruby. No TruthID-operated
27
+ server, no passwords, no third-party login.
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - LICENSE
33
+ - README.md
34
+ - lib/truthid.rb
35
+ - lib/truthid/client.rb
36
+ - lib/truthid/contracts.rb
37
+ - lib/truthid/types.rb
38
+ homepage: https://github.com/masterlxz/truthid/tree/main/sdk/ruby#readme
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ homepage_uri: https://github.com/masterlxz/truthid/tree/main/sdk/ruby#readme
43
+ source_code_uri: https://github.com/masterlxz/truthid
44
+ bug_tracker_uri: https://github.com/masterlxz/truthid/issues
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '3.0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.6.9
60
+ specification_version: 4
61
+ summary: TruthID authentication SDK for Ruby
62
+ test_files: []