Class: MixinBot::API

Inherits:
Object
  • Object
show all
Includes:
App, Asset, Attachment, Auth, Blaze, Collectible, Conversation, EncryptedMessage, Me, Message, Multisig, Payment, Pin, Rpc, Snapshot, Transaction, Transfer, User, Withdraw
Defined in:
lib/mixin_bot/api.rb,
lib/mixin_bot/api/me.rb,
lib/mixin_bot/api/app.rb,
lib/mixin_bot/api/pin.rb,
lib/mixin_bot/api/rpc.rb,
lib/mixin_bot/api/auth.rb,
lib/mixin_bot/api/user.rb,
lib/mixin_bot/api/asset.rb,
lib/mixin_bot/api/blaze.rb,
lib/mixin_bot/api/message.rb,
lib/mixin_bot/api/payment.rb,
lib/mixin_bot/api/multisig.rb,
lib/mixin_bot/api/snapshot.rb,
lib/mixin_bot/api/transfer.rb,
lib/mixin_bot/api/withdraw.rb,
lib/mixin_bot/api/attachment.rb,
lib/mixin_bot/api/collectible.rb,
lib/mixin_bot/api/transaction.rb,
lib/mixin_bot/api/conversation.rb,
lib/mixin_bot/api/encrypted_message.rb

Defined Under Namespace

Modules: App, Asset, Attachment, Auth, Blaze, Collectible, Conversation, EncryptedMessage, Me, Message, Multisig, Payment, Pin, Rpc, Snapshot, Transaction, Transfer, User, Withdraw

Constant Summary collapse

COLLECTIBLE_TRANSACTION_ARGUMENTS =

collectible =

type: 'non_fungible_output',
user_id: '',
output_id: '',
token_id: '',
transaction_hash: '',
output_index: '',
amount: 1,
senders: [],
sender_threshold: 1,
receivers: [],
receivers_threshold: 1,
state: 'unspent'

i[collectible nfo receivers receivers_threshold].freeze

Constants included from Transfer

Transfer::TRANSFER_ARGUMENTS

Constants included from Transaction

Transaction::MAINNET_TRANSACTION_ARGUMENTS, Transaction::MULTISIG_TRANSACTION_ARGUMENTS

Constants included from Multisig

Multisig::MULTISIG_REQUEST_ACTIONS, Multisig::RAW_TRANSACTION_ARGUMENTS

Constants included from Collectible

Collectible::COLLECTABLE_REQUEST_ACTIONS, Collectible::NFT_ASSET_MIXIN_ID

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Withdraw

#create_withdraw_address, #delete_withdraw_address, #get_withdraw_address, #withdrawals

Methods included from User

#create_user, #fetch_users, #generate_ed25519_key, #generate_rsa_key, #read_user, #search_user

Methods included from Transfer

#create_transfer, #transfer

Methods included from Transaction

#create_mainnet_transaction, #create_multisig_transaction, #transactions

Methods included from Snapshot

#network_snapshot, #network_snapshots, #snapshots

Methods included from Rpc

#get_snapshot, #get_transaction, #get_utxo, #list_mint_distributions, #list_mint_works, #list_snapshots, #rpc_proxy, #send_raw_transaction

Methods included from Pin

#decrypt_pin, #encrypt_pin, #update_pin, #verify_pin

Methods included from Payment

#pay_url, #verify_payment

Methods included from Multisig

#build_output, #build_raw_transaction, #build_threshold_script, #cancel_multisig_request, #create_multisig_request, #create_output, #create_payment, #create_sign_multisig_request, #create_unlock_multisig_request, #generate_trace_from_hash, #outputs, #sign_multisig_request, #str_to_bin, #unlock_multisig_request, #verify_multisig

Methods included from Message

#acknowledge_message_receipt, #app_button_group, #app_card, #base_message_params, #list_pending_message, #plain_audio, #plain_contact, #plain_data, #plain_image, #plain_post, #plain_sticker, #plain_text, #plain_video, #read_ws_message, #recall_message, #recall_message_params, #send_app_button_group_message, #send_app_card_message, #send_contact_message, #send_file_message, #send_image_message, #send_message, #send_plain_messages, #send_post_message, #send_text_message, #write_ws_message

Methods included from Me

#friends, #me, #update_me

Methods included from EncryptedMessage

#base_encrypted_message_params, #decrypt_message, #encrypt_message, #encrypted_audio, #encrypted_contact, #encrypted_data, #encrypted_image, #encrypted_post, #encrypted_sticker, #encrypted_text, #encrypted_video, #send_encrypted_audio_message, #send_encrypted_contact_message, #send_encrypted_data_message, #send_encrypted_image_message, #send_encrypted_message, #send_encrypted_messages, #send_encrypted_post_message, #send_encrypted_sticker_message, #send_encrypted_text_message, #send_encrypted_video_message

Methods included from Conversation

#add_conversation_participants, #conversation, #conversation_by_user_id, #create_contact_conversation, #create_conversation, #create_group_conversation, #exit_conversation, #remove_conversation_participants, #rotate_conversation, #unique_uuid, #update_conversation_participants_role, #update_group_conversation_announcement, #update_group_conversation_name

Methods included from Collectible

#cancel_collectible_request, #collectible, #collectible_outputs, #collection, #create_collectible_request, #create_sign_collectible_request, #create_unlock_collectible_request, #sign_collectible_request, #unlock_collectible_request

Methods included from Blaze

#blaze, #start_blaze_connect

Methods included from Auth

#access_token, #oauth_token, #request_oauth

Methods included from Attachment

#create_attachment, #read_attachment, #upload_attachment

Methods included from Asset

#asset, #assets, #ticker

Methods included from App

#add_favorite_app, #favorite_apps, #remove_favorite_app

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mixin_bot/api.rb', line 28

def initialize(options = {})
  @client_id = options[:client_id] || MixinBot.client_id
  @client_secret = options[:client_secret] || MixinBot.client_secret
  @session_id = options[:session_id] || MixinBot.session_id
  @client = Client.new(MixinBot.api_host || 'api.mixin.one')
  @blaze_host = MixinBot.blaze_host || 'blaze.mixin.one'
  @pin_token =
    begin
      Base64.urlsafe_decode64 options[:pin_token] || MixinBot.pin_token
    rescue StandardError
      ''
    end
  _private_key = options[:private_key] || MixinBot.private_key
  if /^-----BEGIN RSA PRIVATE KEY-----/.match? _private_key
    @private_key = _private_key.gsub('\\r\\n', "\n").gsub("\r\n", "\n")
    @key_type = :rsa
  else
    @private_key = Base64.urlsafe_decode64 _private_key
    @key_type = :ed25519
  end
end

Instance Attribute Details

#blaze_hostObject (readonly)

Returns the value of attribute blaze_host.



26
27
28
# File 'lib/mixin_bot/api.rb', line 26

def blaze_host
  @blaze_host
end

#clientObject (readonly)

Returns the value of attribute client.



26
27
28
# File 'lib/mixin_bot/api.rb', line 26

def client
  @client
end

#client_idObject (readonly)

Returns the value of attribute client_id.



26
27
28
# File 'lib/mixin_bot/api.rb', line 26

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



26
27
28
# File 'lib/mixin_bot/api.rb', line 26

def client_secret
  @client_secret
end

#key_typeObject (readonly)

Returns the value of attribute key_type.



26
27
28
# File 'lib/mixin_bot/api.rb', line 26

def key_type
  @key_type
end

#pin_tokenObject (readonly)

Returns the value of attribute pin_token.



26
27
28
# File 'lib/mixin_bot/api.rb', line 26

def pin_token
  @pin_token
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



26
27
28
# File 'lib/mixin_bot/api.rb', line 26

def private_key
  @private_key
end

#session_idObject (readonly)

Returns the value of attribute session_id.



26
27
28
# File 'lib/mixin_bot/api.rb', line 26

def session_id
  @session_id
end

Instance Method Details

#_generate_aes_keyObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mixin_bot/api/pin.rb', line 77

def _generate_aes_key
  if pin_token.size == 32
    JOSE::JWA::X25519.x25519(
      JOSE::JWA::Ed25519.secret_to_curve25519(private_key[0..31]),
      pin_token
    )
  else
    JOSE::JWA::PKCS1.rsaes_oaep_decrypt(
      'SHA256',
      pin_token,
      OpenSSL::PKey::RSA.new(private_key),
      session_id
    )
  end
end

#build_collectible_transaction(**kwargs) ⇒ Object

Raises:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mixin_bot/api/collectible.rb', line 113

def build_collectible_transaction(**kwargs)
  raise ArgumentError, "#{COLLECTIBLE_TRANSACTION_ARGUMENTS.join(', ')} are needed for build collectible transaction" unless COLLECTIBLE_TRANSACTION_ARGUMENTS.all? { |param| kwargs.keys.include? param }

  kwargs = kwargs.with_indifferent_access
  collectible = kwargs['collectible']
  raise 'collectible is spent' if collectible['state'] == 'spent'

  build_raw_transaction(
    utxos: [collectible],
    senders: collectible['receivers'],
    senders_threshold: collectible['receivers_threshold'],
    receivers: kwargs['receivers'],
    receivers_threshold: kwargs['receivers_threshold'],
    extra: kwargs['nfo'],
    amount: 1,
    asset_mixin_id: NFT_ASSET_MIXIN_ID,
    access_token: kwargs['access_token'],
    hint: kwargs['hint']
  )
end

#decode_raw_transaction(raw) ⇒ Object



54
55
56
# File 'lib/mixin_bot/api.rb', line 54

def decode_raw_transaction(raw)
  MixinBot::Utils.decode_raw_transaction raw
end

#decode_raw_transaction_native(raw) ⇒ Object

Use a mixin software to implement transaction build



70
71
72
73
74
75
76
77
78
# File 'lib/mixin_bot/api.rb', line 70

def decode_raw_transaction_native(raw)
  ensure_mixin_command_exist
  command = format("mixin decoderawtransaction --raw '%<arg>s'", arg: raw)

  output, error = Open3.capture3(command)
  raise error unless error.empty?

  JSON.parse output.chomp
end

#nft_memo(collection, token_id, meta) ⇒ Object



134
135
136
# File 'lib/mixin_bot/api/collectible.rb', line 134

def nft_memo(collection, token_id, meta)
  MixinBot::Utils.nft_memo collection, token_id, meta
end

#sign_raw_transaction(tx) ⇒ Object



50
51
52
# File 'lib/mixin_bot/api.rb', line 50

def sign_raw_transaction(tx)
  MixinBot::Utils.sign_raw_transaction tx
end

#sign_raw_transaction_native(json) ⇒ Object

Use a mixin software to implement transaction build



59
60
61
62
63
64
65
66
67
# File 'lib/mixin_bot/api.rb', line 59

def sign_raw_transaction_native(json)
  ensure_mixin_command_exist
  command = format("mixin signrawtransaction --raw '%<arg>s'", arg: json)

  output, error = Open3.capture3(command)
  raise error unless error.empty?

  output.chomp
end