Class: X402::RemoteWallet

Inherits:
Object
  • Object
show all
Defined in:
lib/x402/remote_wallet.rb

Overview

HTTP client adapter implementing the duck-typed wallet interface by calling a remote @bsv/simple server wallet API.

Provides the same interface as BSV::Wallet::WalletClient:

  • +#get_public_key(identity_key:)+ — fetches the identity key
  • +#get_public_key(protocol_id:, key_id:, ...)+ — derives a key
  • +#internalize_action(tx:, outputs:, description:)+ — relays payment

Thread-safe: all mutable state is protected by a Mutex.

Constant Summary collapse

DEFAULT_TIMEOUT =

seconds

10

Instance Method Summary collapse

Constructor Details

#initialize(url:, timeout: DEFAULT_TIMEOUT) ⇒ RemoteWallet

Returns a new instance of RemoteWallet.

Parameters:

  • url (String)

    base URL of the remote wallet API

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    HTTP timeout in seconds (default 10)



22
23
24
25
26
27
28
29
30
31
# File 'lib/x402/remote_wallet.rb', line 22

def initialize(url:, timeout: DEFAULT_TIMEOUT)
  raise ConfigurationError, "remote wallet url is required" if url.nil? || url.to_s.empty?

  @base_uri = URI.parse(url)
  @timeout = timeout
  @identity_key = nil
  @mutex = Mutex.new
rescue URI::InvalidURIError => e
  raise ConfigurationError, "invalid remote wallet url: #{e.message}"
end

Instance Method Details

#get_public_key(args = {}, **kwargs) ⇒ Hash

Fetch a public key from the remote wallet.

When +identity_key: true+, returns the wallet's identity public key (cached after first call). Otherwise, delegates to the remote wallet for BRC-42/43 key derivation.

Matches ProtoWallet interface: accepts a positional Hash of arguments. Also supports keyword arguments for convenience (used by tests and direct callers).

Parameters:

  • args (Hash) (defaults to: {})

    arguments hash (ProtoWallet style)

Returns:

  • (Hash)

    +{ public_key: "hex" }+



45
46
47
48
49
50
51
52
# File 'lib/x402/remote_wallet.rb', line 45

def get_public_key(args = {}, **kwargs)
  args = kwargs unless kwargs.empty?
  if args[:identity_key]
    fetch_identity_key
  else
    fetch_derived_key(protocol_id: args[:protocol_id], key_id: args[:key_id], counterparty: args[:counterparty])
  end
end

#internalize_action(args = {}, **kwargs) ⇒ Hash

Relay a settled payment to the remote wallet for internalisation.

Matches ProtoWallet interface: accepts a positional Hash of arguments. Also supports keyword arguments for convenience.

Parameters:

  • args (Hash) (defaults to: {})

    arguments hash (ProtoWallet style)

Returns:

  • (Hash)

    the wallet's response



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/x402/remote_wallet.rb', line 61

def internalize_action(args = {}, **kwargs)
  args = kwargs unless kwargs.empty?
  output = args[:outputs]&.first || {}
  remittance = output[:payment_remittance] || {}

  body = {
    tx: args[:tx],
    description: args[:description] || "",
    senderIdentityKey: remittance[:sender_identity_key],
    derivationPrefix: remittance[:derivation_prefix],
    derivationSuffix: remittance[:derivation_suffix],
    outputIndex: output[:output_index]
  }

  response = post_request("receive", body)
  parse_json_response!(response, context: "internalize_action")
end