Coinbase API: Create a New Bitcoin Wallet for Users with an Email Address
1 answer
This is what I am doing in my Rails application.
def create_account
wallet = Coinbase::Wallet::Client.new(api_key: ENV["COINBASE_KEY"], api_secret: ENV["COINBASE_SECRET"])
mAccount = check_account
if mAccount.nil?
mAccount = wallet.create_account(name: self.email)
end
newaddr = mAccount.create_address
newaddr = newaddr["address"]
account = Account.new
account.address = newaddr
account.user_id = self.id
account.balance = mAccount["balance"]["amount"]
account.account_id = mAccount["id"]
account.save
end
def check_account
begin
wallet = Coinbase::Wallet::Client.new(api_key: ENV["COINBASE_KEY"], api_secret: ENV["COINBASE_SECRET"])
ac = nil
a = wallet.accounts
a.each do |account|
if account["name"] == self.email
ac = wallet.account(account["id"])
end
end
ac
rescue Coinbase::Wallet::NotFoundError
nil
end
end
0
source to share