Module: MixinBot::API::Pin

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

Instance Method Summary collapse

Instance Method Details

#decrypt_pin(msg) ⇒ Object

decrypt the encrpted pin, just for test



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mixin_bot/api/pin.rb', line 34

def decrypt_pin(msg)
  msg = Base64.strict_decode64 msg
  iv = msg[0..15]
  cipher = msg[16..47]
  alg = 'AES-256-CBC'
  decode_cipher = OpenSSL::Cipher.new(alg)
  decode_cipher.decrypt
  decode_cipher.iv = iv
  decode_cipher.key = _generate_aes_key
  decoded = decode_cipher.update(cipher)
  decoded[0..5]
end

#encrypt_pin(pin_code, iterator: nil) ⇒ Object

developers.mixin.one/api/alpha-mixin-network/encrypted-pin/ use timestamp(timestamp) for iterator as default: must be bigger than the previous, the first time must be greater than 0. After a new session created, it will be reset to 0.



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
# File 'lib/mixin_bot/api/pin.rb', line 49

def encrypt_pin(pin_code, iterator: nil)
  iterator ||= Time.now.utc.to_i
  tszero = iterator % 0x100
  tsone = (iterator % 0x10000) >> 8
  tstwo = (iterator % 0x1000000) >> 16
  tsthree = (iterator % 0x100000000) >> 24
  tsstring = "#{tszero.chr}#{tsone.chr}#{tstwo.chr}#{tsthree.chr}\u0000\u0000\u0000\u0000"
  encrypt_content = pin_code + tsstring + tsstring
  pad_count = 16 - encrypt_content.length % 16
  padded_content =
    if pad_count.positive?
      encrypt_content + pad_count.chr * pad_count
    else
      encrypt_content
    end

  alg = 'AES-256-CBC'
  aes = OpenSSL::Cipher.new(alg)
  iv = OpenSSL::Cipher.new(alg).random_iv
  aes.encrypt
  aes.key = _generate_aes_key
  aes.iv = iv
  cipher = aes.update(padded_content)
  msg = iv + cipher
  Base64.strict_encode64 msg
end

#update_pin(old_pin:, pin:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mixin_bot/api/pin.rb', line 19

def update_pin(old_pin:, pin:)
  path = '/pin/update'
  encrypted_old_pin = old_pin.nil? ? '' : encrypt_pin(old_pin, iterator: Time.now.utc.to_i)
  encrypted_pin = encrypt_pin(pin, iterator: Time.now.utc.to_i + 1)
  payload = {
    old_pin: encrypted_old_pin,
    pin: encrypted_pin
  }

  access_token = access_token('POST', path, payload.to_json)
  authorization = format('Bearer %<access_token>s', access_token: access_token)
  client.post(path, headers: { 'Authorization': authorization }, json: payload)
end

#verify_pin(pin_code) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/mixin_bot/api/pin.rb', line 7

def verify_pin(pin_code)
  path = '/pin/verify'
  payload = {
    pin: encrypt_pin(pin_code)
  }

  access_token = access_token('POST', path, payload.to_json)
  authorization = format('Bearer %<access_token>s', access_token: access_token)
  client.post(path, headers: { 'Authorization': authorization }, json: payload)
end