<commit_before>require_relative "setup"

require "term/ansicolor"
String.send :include, Term::ANSIColor

# colored output to make it easier to see structure
def log(message, data=nil)
  if data.is_a? String
    puts "#{message.yellow} => #{data.dump.cyan}"
  elsif data.nil?
    puts "#{message.yellow}"
  else
    begin
      puts "#{message.yellow} => #{JSON.pretty_generate(data).cyan}"
    rescue
      puts "#{message.yellow} => #{data.inspect.cyan}"
    end
  end
  puts
end

def self.mask(hash, *keys)
  out = {}
  keys.each do |key|
    out[key] = hash[key]
  end
  out[:etc] = "..."
  out
end


include BitVault::Encodings
include BitVault::Crypto

MultiWallet = BitVault::Bitcoin::MultiWallet


## API discovery
#
# The BitVault server provides a JSON description of its API that allows
# the client to generate all necessary resource classes at runtime.
# We initialize the BitVault client with a block that returns an object
# that will be used as a "context", a place to store needful things.
# At present, the only requirement for a context object is that it
# implements a method named `authorizer`, which must return a credential
# for use in the HTTP Authorization headers.

service_url = ARGV[0] || "http://localhost:8999/"
BV = BitVault::Client.discover(service_url) { BitVault::Client::Context.new }

## Create a "sub-client" with its own context

client = BV.spawn


# Create a user
#
# The objects in `client.resources` are resource instances created at
# discovery-time.  Each has action methods defined based on the JSON
# API definition.  Action methods perform the actual HTTP requests
# and wrap the results in further resource instances when appropriate.


users = client.resources.users

email = "<EMAIL>"

user = users.create(
  :email => email,
  :first_name => "Matthew",
  :last_name => "King",
  :password => "<PASSWORD>"
)
log "Create a user with", mask(user, :email, :first_name, :last_name)

# The create action returned a User Resource which has:
#
# * action methods (get, update, reset)
# * attributes (email, first_name, etc.)
# * associated resources (applications)


## Simulate a later session

client = BV.spawn

# Supply the client with the user password, required to manage the user
# and its applications.  The context class used here determines which
# credential to use based on the authorization scheme.

client.context.password = "<PASSWORD>"

# Retrieve the user resource

#user = client.resources.user(user.url).get

user = client.resources.login(:email => email).get


## Create an application.
#
# Wallets belong to applications, not directly
# to users. The optional callback_url attribute specifies a URL where BitVault
# can POST event information such as confirmed transactions.

application = user.applications.create(
  :name => "bitcoin_emporium",
  :callback_url => "https://api.bitcoin-emporium.io/events"
)

log "Created an application for the user", mask(application, :name, :api_token, :callback_url)

# Applications use API tokens for authentication, rather than
# requiring the user password.  Tokens can be reset easily,
# password resets pose a major inconvenience to the user.

# Supply the client with the authentication credential
client.context.api_token = application.api_token

# List applications
list = user.applications.list
log "List the user applications", (list.map do |app|
  mask(app, :name, :api_token, :callback_url)
end)


## Reset or delete the application
#

reset = application.reset

log "Reset an application's api token", {
  :previous_token => application.api_token,<commit_msg>fixed demo script for proper basic_auth<commit_after>require_relative "setup"

require "term/ansicolor"
String.send :include, Term::ANSIColor

# colored output to make it easier to see structure
def log(message, data=nil)
  if data.is_a? String
    puts "#{message.yellow} => #{data.dump.cyan}"
  elsif data.nil?
    puts "#{message.yellow}"
  else
    begin
      puts "#{message.yellow} => #{JSON.pretty_generate(data).cyan}"
    rescue
      puts "#{message.yellow} => #{data.inspect.cyan}"
    end
  end
  puts
end

def self.mask(hash, *keys)
  out = {}
  keys.each do |key|
    out[key] = hash[key]
  end
  out[:etc] = "..."
  out
end


include BitVault::Encodings
include BitVault::Crypto

MultiWallet = BitVault::Bitcoin::MultiWallet


## API discovery
#
# The BitVault server provides a JSON description of its API that allows
# the client to generate all necessary resource classes at runtime.
# We initialize the BitVault client with a block that returns an object
# that will be used as a "context", a place to store needful things.
# At present, the only requirement for a context object is that it
# implements a method named `authorizer`, which must return a credential
# for use in the HTTP Authorization headers.

service_url = ARGV[0] || "http://localhost:8999/"
BV = BitVault::Client.discover(service_url) { BitVault::Client::Context.new }

## Create a "sub-client" with its own context

client = BV.spawn


# Create a user
#
# The objects in `client.resources` are resource instances created at
# discovery-time.  Each has action methods defined based on the JSON
# API definition.  Action methods perform the actual HTTP requests
# and wrap the results in further resource instances when appropriate.


users = client.resources.users

email = "<EMAIL>"

user = users.create(
  :email => email,
  :first_name => "Matthew",
  :last_name => "King",
  :password => "<PASSWORD>"
)
log "Create a user with", mask(user, :email, :first_name, :last_name)

# The create action returned a User Resource which has:
#
# * action methods (get, update, reset)
# * attributes (email, first_name, etc.)
# * associated resources (applications)


## Simulate a later session

client = BV.spawn

# Supply the client with the user password, required to manage the user
# and its applications.  The context class used here determines which
# credential to use based on the authorization scheme.

client.context.set_basic(email, "incredibly_secure")

# Retrieve the user resource

#user = client.resources.user(user.url).get

user = client.resources.login(:email => email).get


## Create an application.
#
# Wallets belong to applications, not directly
# to users. The optional callback_url attribute specifies a URL where BitVault
# can POST event information such as confirmed transactions.

application = user.applications.create(
  :name => "bitcoin_emporium",
  :callback_url => "https://api.bitcoin-emporium.io/events"
)

log "Created an application for the user", mask(application, :name, :api_token, :callback_url)

# Applications use API tokens for authentication, rather than
# requiring the user password.  Tokens can be reset easily,
# password resets pose a major inconvenience to the user.

# Supply the client with the authentication credential
client.context.set_token(application.api_token)

# List applications
list = user.applications.list
log "List the user applications", (list.map do |app|
  mask(app, :name, :api_token, :callback_url)
end)


## Reset or delete the application
#

reset = application.reset

log "Reset an application's api token", {
  :previous_token => application.api_token,
{'fraction_non_alphanumeric': 0.10032002226241825, 'fraction_numerical': 0.0013914011409489355, 'mean_word_length': 4.433106575963719, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 30, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '311329', 'n_tokens_mistral': 2124, 'n_tokens_neox': 2083, 'n_words': 799}