Giter Club home page Giter Club logo

telegram-passport's Introduction

Telegram Passport

Parse/Decrypt incoming Telegram Passport data

* Note: All the type definitions on this library are in compliance with those defined in the Telegram API specification

What does this library do?

Provided your Bot's Private Key, this library will:

  • Decrypt the EncryptedCredentials object from the credentials field in PassportData
  • Parse the fields on each EncryptedPassportElement from the data field in PassportData
  • Decrypt de data field (if present) from the EncryptedPassportElement
  • Validate the integrity of the decryted data

What doesn't this library do?

  • Get the encrypted files corresponding to the requested fields
    * Download the encrypted files using the getFile API endpoint, then use the decryptData method to decrypt them

Usage

  • First, create a new instance of the TelegramPassport class
const telegramPassport = new TelegramPassport("<bot_private_key>");
  • Parse and decryp de data of all the elements shared with the bot
const data = telegramPassport.decryptPassportData(
    update.message.passport_data
);

// the nonce is retuned within the RequestedFields object

const nonce = data.nonce;

* update is the object representing the incoming Update that was sent to the Bot

  • Decryting files
/*
get the data corresponding to the file you want to decryp
for example, the front side of the id card
*/

const id_frontSide = data.identity_card.front_side;

// download the file using the getFile API endpoint

...

// decryp the file

const file = telegramPassport.decryptData(
  downloaded_file_data,
  id_fronSide.secret,
  id_fronSide.file_hash,
);
  • Depending on the fields you requested, you might not need to process the whole PassportData object; for example, the "phone_number" and "email" fields are not encrypted. Thus, you only need to decrypt the credentials to obtain the nonce, then, you can get "phone_number" and "email" from passport_data.data
/*
in this case, data will look like this

data: [
  {
    "type": "phone_number",
    "phone_number": "XXXXXXXXXXX",
    "hash": "the_base64-encoded_hash",
  },
  {
    "type": "email",
    "email": "[email protected]",
    "hash": "the_base64-encoded_hash"
  },
]
*/

// decrypt the credentials

const credentials = telegramPassport.decryptPassportCredentials(
  update.message.passport_data.credentials,
);

* update is the object representing the incoming Update that was sent to the Bot

What can be inproved?

  • The type "handling" in the decryptData method
    * Need a TS guru that can give me a hand with that, go check the code

Found a bug?

Open an issue (PRs are welcome)

* be patient, I might be busy

Stay in touch

telegram-passport's People

Contributors

yn4v4s avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.