Module: MixinBot::API::User

Included in:
MixinBot::API
Defined in:
lib/mixin_bot/api/user.rb

Instance Method Summary collapse

Instance Method Details

#create_safe_user(name, private_key: nil, spend_key: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mixin_bot/api/user.rb', line 40

def create_safe_user(name, private_key: nil, spend_key: nil)
  private_keypair = JOSE::JWA::Ed25519.keypair private_key
  private_key = private_keypair[1].unpack1('H*')

  spend_keypair = JOSE::JWA::Ed25519.keypair spend_key
  spend_key = spend_keypair[1].unpack1('H*')

  user = create_user name, key: private_keypair[1][...32]

  keystore = {
    app_id: user['data']['user_id'],
    session_id: user['data']['session_id'],
    session_private_key: private_key,
    server_public_key: user['data']['pin_token_base64'],
    spend_key:
  }
  user_api = MixinBot::API.new(**keystore)

  user_api.update_pin pin: MixinBot.utils.tip_public_key(spend_keypair[0], counter: user['data']['tip_counter'])

  # wait for tip pin update in server
  sleep 1

  @__retry__ = 0
  begin
    user_api.safe_register keystore[:spend_key]
  rescue MixinBot::Error => e
    @__retry__ += 1
    raise e if @__retry__ > 3

    sleep 1 + @__retry__
    retry
  end

  keystore
end

#create_user(full_name, key: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mixin_bot/api/user.rb', line 11

def create_user(full_name, key: nil)
  keypair = JOSE::JWA::Ed25519.keypair key
  session_secret = Base64.urlsafe_encode64 keypair[0], padding: false
  private_key = keypair[1].unpack1('H*')

  path = '/users'
  payload = {
    full_name:,
    session_secret:
  }

  res = client.post path, **payload
  res.merge(private_key:).with_indifferent_access
end

#fetch_users(user_ids) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/mixin_bot/api/user.rb', line 32

def fetch_users(user_ids)
  path = '/users/fetch'
  user_ids = [user_ids] if user_ids.is_a? String
  payload = user_ids

  client.post path, *payload
end

#migrate_to_safe(spend_key:, pin: nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mixin_bot/api/user.rb', line 98

def migrate_to_safe(spend_key:, pin: nil)
  profile = me['data']
  return true if profile['has_safe']

  spend_keypair = JOSE::JWA::Ed25519.keypair spend_key
  spend_key = spend_keypair[1].unpack1('H*')

  if profile['tip_key_base64'].blank?
    new_pin = MixinBot.utils.tip_public_key(spend_keypair[0], counter: profile['tip_counter'])
    update_pin(pin: new_pin, old_pin: pin)

    pin = new_pin
  end

  # wait for tip pin update in server
  sleep 1

  @__retry__ = 0
  begin
    safe_register pin, spend_key
  rescue MixinBot::Error => e
    @__retry__ += 1
    raise e if @__retry__ > 3

    sleep 1 + @__retry__
    retry
  end

  {
    spend_key:
  }.with_indifferent_access
end

#safe_register(pin, spend_key: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mixin_bot/api/user.rb', line 77

def safe_register(pin, spend_key: nil)
  path = '/safe/users'

  spend_key ||= MixinBot.utils.decode_key pin
  key = JOSE::JWA::Ed25519.keypair spend_key[...32]
  public_key = key[0].unpack1('H*')

  hex = SHA3::Digest::SHA256.hexdigest config.app_id
  signature = Base64.urlsafe_encode64 JOSE::JWA::Ed25519.sign([hex].pack('H*'), key[1]), padding: false

  pin_base64 = encrypt_tip_pin pin, 'SEQUENCER:REGISTER:', config.app_id, public_key

  payload = {
    public_key:,
    signature:,
    pin_base64:
  }

  client.post path, **payload
end

#search_user(query, access_token: nil) ⇒ Object



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

def search_user(query, access_token: nil)
  path = format('/search/%<query>s', query:)

  client.get path, access_token:
end

#user(user_id, access_token: nil) ⇒ Object



6
7
8
9
# File 'lib/mixin_bot/api/user.rb', line 6

def user(user_id, access_token: nil)
  path = format('/users/%<user_id>s', user_id:)
  client.get path, access_token:
end