Class: TruthID::Client
- Inherits:
-
Object
- Object
- TruthID::Client
- Defined in:
- lib/truthid/client.rb
Constant Summary collapse
- RPC_URLS =
{ "base-sepolia" => "https://sepolia.base.org", "base-mainnet" => "https://mainnet.base.org" }.freeze
Instance Method Summary collapse
- #check_device_status(device_pub_key) ⇒ Object
-
#compute_smart_account_address(ledger_address, index: 0) ⇒ Object
Prevê o endereço da smart account (controller) pra uma owner key via CREATE2, antes da conta ser de fato deployada on-chain — computação local pura, sem chamada de rede.
- #create_challenge(origin) ⇒ Object
-
#initialize(network: "base-mainnet", rpc_url: nil) ⇒ Client
constructor
A new instance of Client.
- #verify_auth_response(challenge, response, ttl_ms: 30_000) ⇒ Object
- #verify_session(session_hash) ⇒ Object
Constructor Details
#initialize(network: "base-mainnet", rpc_url: nil) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/truthid/client.rb', line 15 def initialize(network: "base-mainnet", rpc_url: nil) @network = network url = rpc_url || RPC_URLS.fetch(network) @rpc = Eth::Client.create(url) @devices = Eth::Contract.from_abi( name: "DeviceRegistry", address: Contracts::DEVICE_REGISTRY_ADDRESSES.fetch(network), abi: Contracts::DEVICE_REGISTRY_ABI ) @sessions = Eth::Contract.from_abi( name: "SessionRegistry", address: Contracts::SESSION_REGISTRY_ADDRESSES.fetch(network), abi: Contracts::SESSION_REGISTRY_ABI ) end |
Instance Method Details
#check_device_status(device_pub_key) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/truthid/client.rb', line 128 def check_device_status(device_pub_key) device = @rpc.call(@devices, "getDevice", device_pub_key) return DeviceStatus.new(exists: false, active: false) unless device[5] # exists DeviceStatus.new( exists: true, active: !device[4], # revoked → active é o inverso label: device[2], identity_id: device[0], added_at: Time.at(device[3]).utc ) end |
#compute_smart_account_address(ledger_address, index: 0) ⇒ Object
Prevê o endereço da smart account (controller) pra uma owner key via CREATE2, antes da conta ser de fato deployada on-chain — computação local pura, sem chamada de rede. Ver smart_account.rb pro algoritmo.
124 125 126 |
# File 'lib/truthid/client.rb', line 124 def compute_smart_account_address(ledger_address, index: 0) TruthID.compute_smart_account_address(ledger_address, network: @network, index: index) end |
#create_challenge(origin) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/truthid/client.rb', line 31 def create_challenge(origin) AuthChallenge.new( type: "challenge", nonce: SecureRandom.uuid, issued_at: (Time.now.to_f * 1000).to_i, origin: origin ) end |
#verify_auth_response(challenge, response, ttl_ms: 30_000) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/truthid/client.rb', line 40 def verify_auth_response(challenge, response, ttl_ms: 30_000) # 1. Usuário recusou unless response.approved return VerifyAuthResult.new(valid: false, reason: "User rejected the login request") end # 2. TTL expirado now_ms = (Time.now.to_f * 1000).to_i if now_ms - challenge.issued_at > ttl_ms return VerifyAuthResult.new(valid: false, reason: "Challenge expired") end # 3. Nonce bate com o challenge original if challenge.nonce != response.nonce return VerifyAuthResult.new(valid: false, reason: "Nonce mismatch") end # 4. Verificar assinatura # JSON.generate já produz JSON compacto (sem espaços) — compatível com Dart e JS = JSON.generate(challenge.to_h) begin # personal_recover devolve a chave pública descomprimida (65 bytes), # não um endereço — precisa do passo extra de derivar o endereço # (keccak256 da chave pública) antes de comparar. Achado real: sem # esse passo, essa comparação nunca batia com nenhum device_address # de verdade, rejeitando toda assinatura válida. public_key = Eth::Signature.personal_recover(, response.signature) signer = Eth::Util.public_key_to_address(public_key).to_s rescue => e return VerifyAuthResult.new(valid: false, reason: "Invalid signature format") end if signer.downcase != response.device_address.downcase return VerifyAuthResult.new(valid: false, reason: "Signature does not match device address") end # 5. Device ativo na blockchain is_active = @rpc.call(@devices, "isDeviceActive", response.device_address) unless is_active return VerifyAuthResult.new(valid: false, reason: "Device is not active or has been revoked") end # 6. Buscar identityId do device device = @rpc.call(@devices, "getDevice", response.device_address) VerifyAuthResult.new( valid: true, identity_id: device[0], device_address: response.device_address ) end |
#verify_session(session_hash) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/truthid/client.rb', line 103 def verify_session(session_hash) # bytes32: converte hex string → 32 bytes binários hash_bytes = [session_hash.delete_prefix("0x")].pack("H*") session = read_session(hash_bytes) return SessionInfo.new(exists: false, revoked: false) if session.nil? revoked = @rpc.call(@sessions, "isSessionRevoked", hash_bytes) SessionInfo.new( exists: true, revoked: revoked, identity_id: session[0], device_pub_key: session[1], created_at: Time.at(session[2]).utc ) end |