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 2 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
2 changes: 1 addition & 1 deletion coinbase.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Gem::Specification.new do |spec|

spec.metadata['rubygems_mfa_required'] = 'true'

spec.add_runtime_dependency 'dotenv'
spec.add_runtime_dependency 'eth'
spec.add_runtime_dependency 'faraday'
spec.add_runtime_dependency 'faraday-multipart'
Expand All @@ -23,7 +24,6 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'marcel'
spec.add_runtime_dependency 'money-tree'

spec.add_development_dependency 'dotenv'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
Expand Down
48 changes: 47 additions & 1 deletion lib/coinbase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,84 @@
require_relative 'coinbase/transfer'
require_relative 'coinbase/user'
require_relative 'coinbase/wallet'
require 'dotenv'
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 file. Currently, CDP API Key JSON files and
# .env files are supported.
# @param file_path [String] (Optional) the path to the configuration file. Assumes a CDP API Key JSON
# file in the root directory by default.
def self.configure_from_file(file_path = 'coinbase_cloud_api_key.json')
configuration.from_file(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 file. Currently,
# CDP API Key JSON files and .env files and are supported.
# @param file_path [String] (Optional) the path to the configuration file. Assumes a CDP API Key JSON
# file in the root directory by default.
def from_file(file_path = 'coinbase_cloud_api_key.json')
# Expand paths to respect shortcuts like ~.
file_path = File.expand_path(file_path)

if 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']
elsif File.basename(file_path).start_with?('.env')
Dotenv.load(file_path)
@api_key_name = ENV['API_KEY_NAME']
@api_key_private_key = ENV['API_KEY_PRIVATE_KEY']
@base_sepolia_rpc_url = ENV.fetch('BASE_SEPOLIA_RPC_URL', 'https://sepolia.base.org')
@base_sepolia_client = Jimson::Client.new(@base_sepolia_rpc_url)
@api_url = ENV.fetch('API_URL', 'https://api.cdp.coinbase.com')
else
raise InvalidConfiguration, 'Invalid configuration file type'
end
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"
30 changes: 30 additions & 0 deletions spec/unit/coinbase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@
end
end

describe '#configure_from_file' do
context 'when the file is a CDP API key JSON file' do
let(:file_path) { 'spec/fixtures/cdp_api_key.json' }

subject do
Coinbase.configure_from_file(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

context 'when the file is a .env file' do
let(:file_path) { 'spec/fixtures/.env.test' }

subject do
Coinbase.configure_from_file(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
end

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