Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PSDK-54] Add method for configuring from file #25

Merged
merged 6 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow disabling debug tracing
- Error specifications

## [0.0.3] - 2024-05-08

### Added

- Coinbase.configure_from_file

## [0.0.2] - 2024-05-01

### Added
Expand Down
36 changes: 35 additions & 1 deletion lib/coinbase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,72 @@
require_relative 'coinbase/transfer'
require_relative 'coinbase/user'
require_relative 'coinbase/wallet'
require 'json'

# The Coinbase SDK.
module Coinbase
class InvalidConfiguration < StandardError; end

# Returns the configuration object.
# @return [Configuration] the configuration object
def self.configuration
@configuration ||= Configuration.new
end

# Configures the Coinbase SDK.
def self.configure
yield(configuration)

raise InvalidConfiguration, 'API key private key is not set' unless configuration.api_key_private_key
raise InvalidConfiguration, 'API key name is not set' unless configuration.api_key_name
end

# Configuration object for the Coinbase SDK
# Configures the Coinbase SDK from the given CDP API Key JSON file.
# @param file_path [String] (Optional) the path to the CDP API Key JSON file
# file in the root directory by default.
def self.configure_from_json(file_path = 'coinbase_cloud_api_key.json')
configuration.from_json(file_path)

raise InvalidConfiguration, 'API key private key is not set' unless configuration.api_key_private_key
raise InvalidConfiguration, 'API key name is not set' unless configuration.api_key_name
end

# Configuration object for the Coinbase SDK.
class Configuration
attr_reader :base_sepolia_rpc_url, :base_sepolia_client
attr_accessor :api_url, :api_key_name, :api_key_private_key

# Initializes the configuration object.
def initialize
@base_sepolia_rpc_url = 'https://sepolia.base.org'
@base_sepolia_client = Jimson::Client.new(@base_sepolia_rpc_url)
@api_url = 'https://api.cdp.coinbase.com'
end

# Sets configuration values based on the provided CDP API Key JSON file.
# @param file_path [String] (Optional) the path to the CDP API Key JSON file
# file in the root directory by default.
def from_json(file_path = 'coinbase_cloud_api_key.json')
# Expand paths to respect shortcuts like ~.
file_path = File.expand_path(file_path)

raise InvalidConfiguration, 'Invalid configuration file type' unless file_path.end_with?('.json')

file = File.read(file_path)
data = JSON.parse(file)
@api_key_name = data['name']
@api_key_private_key = data['privateKey']
end

# Sets the base Sepolia RPC URL.
# @param new_base_sepolia_rpc_url [String] the new base Sepolia RPC URL
def base_sepolia_rpc_url=(new_base_sepolia_rpc_url)
@base_sepolia_rpc_url = new_base_sepolia_rpc_url
@base_sepolia_client = Jimson::Client.new(@base_sepolia_rpc_url)
end

# Returns the API client.
# @return [Coinbase::Client::ApiClient] the API client
def api_client
@api_client ||= Coinbase::Client::ApiClient.new(Middleware.config)
end
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
API_KEY_NAME="organizations/e0716da6-47ce-4c68-b3da-d551b5a8671b/apiKeys/3688b1ac-d453-42db-814e-c425746dc967"
API_KEY_PRIVATE_KEY="-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIFxG95q+feSkhWSnAKze+rWB3eo7oDNYKXpkiX45h3QcoAoGCCqGSM49\nAwEHoUQDQgAEyVDroLdmHJEpDAzJX6YJYQjMiWGYzEdDzu2TNyVUAP6w4ayA8qGe\nuJROGr7n7QT7MGg/IaA+MkhqO7lhgWLw2A==\n-----END EC PRIVATE KEY-----\n"
14 changes: 14 additions & 0 deletions spec/unit/coinbase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
end
end

describe '#configure_from_json' do
let(:file_path) { 'spec/fixtures/cdp_api_key.json' }

subject do
Coinbase.configure_from_json(file_path)
end

it 'correctly configures the API key' do
expect { subject }.not_to raise_error
expect(Coinbase.configuration.api_key_private_key).not_to be_nil
expect(Coinbase.configuration.api_key_name).not_to be_nil
end
end

describe '#default_user' do
let(:users_api) { double Coinbase::Client::UsersApi }
let(:user_model) { double 'User Model' }
Expand Down
Loading