Module: MixinBot::API::Pin
- Included in:
- MixinBot::API
- Defined in:
- lib/mixin_bot/api/pin.rb
Instance Method Summary collapse
- #decrypt_pin(msg) ⇒ Object
- #encrypt_pin(pin, iterator: nil, shared_key: nil) ⇒ Object
- #generate_shared_key_with_server ⇒ Object
- #prepare_tip_key(counter = 0) ⇒ Object
- #update_pin(pin:, old_pin: nil) ⇒ Object
- #update_tip_pin(pin, pub_tip) ⇒ Object
- #verify_pin(pin = nil) ⇒ Object (also: #verify_pin_tip)
Instance Method Details
#decrypt_pin(msg) ⇒ Object
77 78 79 |
# File 'lib/mixin_bot/api/pin.rb', line 77 def decrypt_pin(msg) MixinBot.utils.decrypt_pin msg, shared_key: generate_shared_key_with_server end |
#encrypt_pin(pin, iterator: nil, shared_key: nil) ⇒ Object
72 73 74 75 |
# File 'lib/mixin_bot/api/pin.rb', line 72 def encrypt_pin(pin, iterator: nil, shared_key: nil) sk = shared_key.presence || generate_shared_key_with_server MixinBot.utils.encrypt_pin(pin, iterator:, shared_key: sk) end |
#generate_shared_key_with_server ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/mixin_bot/api/pin.rb', line 81 def generate_shared_key_with_server if config.server_public_key.size == 32 JOSE::JWA::X25519.x25519( config.session_private_key_curve25519, config.server_public_key_curve25519 ) else JOSE::JWA::PKCS1.rsaes_oaep_decrypt( 'SHA256', config.server_public_key, OpenSSL::PKey::RSA.new(config.session_private_key), session_id ) end end |
#prepare_tip_key(counter = 0) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mixin_bot/api/pin.rb', line 60 def prepare_tip_key(counter = 0) ed25519_key = JOSE::JWA::Ed25519.keypair private_key = ed25519_key[1].unpack1('H*') public_key = (ed25519_key[0].bytes + MixinBot::Utils.encode_uint64(counter + 1)).pack('c*').unpack1('H*') { private_key:, public_key: } end |
#update_pin(pin:, old_pin: nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mixin_bot/api/pin.rb', line 33 def update_pin(pin:, old_pin: nil) # old_pin ||= MixinBot.config.pin raise ArgumentError, 'invalid old pin' if old_pin.present? && old_pin.length != 6 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_base64: encrypted_old_pin, pin_base64: encrypted_pin } client.post path, **payload end |
#update_tip_pin(pin, pub_tip) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mixin_bot/api/pin.rb', line 49 def update_tip_pin(pin, pub_tip) old_encrypted = encrypt_pin(pin, iterator: (Time.now.utc.to_f * 1e9).to_i) pub_buf = [pub_tip].pack('H*') raise ArgumentError, 'invalid public key' unless pub_buf.bytesize == 32 pub_tip_buf = pub_buf + [1].pack('Q>') encrypted_pin = encrypt_pin(pub_tip_buf.unpack1('H*'), iterator: (Time.now.utc.to_f * 1e9).to_i + 1) path = '/pin/update' client.post path, old_pin_base64: old_encrypted, pin_base64: encrypted_pin end |
#verify_pin(pin = nil) ⇒ Object Also known as: verify_pin_tip
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mixin_bot/api/pin.rb', line 7 def verify_pin(pin = nil) pin ||= MixinBot.config.pin raise ArgumentError, 'invalid pin' if pin.blank? path = '/pin/verify' payload = if pin.length > 6 = (Time.now.utc.to_f * 1e9).to_i pin_base64 = encrypt_tip_pin pin, 'TIP:VERIFY:', .to_s.rjust(32, '0') { pin_base64:, timestamp: } else { pin: encrypt_pin(pin) } end client.post path, **payload end |