Skip to main content

This is a Python library to help you build servers/services within Sygna Bridge Ecosystem.

Project description

Python Sygna Bridge Util

This is a Python library to help you build servers/services within Sygna Bridge Ecosystem. For more detail information, please see Sygna Bridge.

Installation

pip install sygna-bridge-util

Crypto

Dealing with encoding, decoding, signing and verifying in Sygna Bridge.

ECIES Encoding an Decoding

During the communication of VASPs, there are some private information that must be encrypted. We use ECIES(Elliptic Curve Integrated Encryption Scheme) to securely encrypt these private data so that they can only be accessed by the recipient.

We're using IVMS101 (interVASP Messaging Standard) as our private information format.

We also provide IVMS101 Python Utility to construct data payload.

sensitive_data = {
  "originator": {
    "originator_persons": [
      {
        "natural_person": {
          "name": {
            "name_identifiers": [
              {
                "primary_identifier": "Wu Xinli",
                "name_identifier_type": "LEGL"
              }
            ]
          },
          "national_identification": {
            "national_identifier": "446005",
            "national_identifier_type": "RAID",
            "registration_authority": "RA000553"
          },
          "country_of_residence": "TZ"
        }
      }
    ],
    "account_numbers": [
      "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV"
    ]
  },
  "beneficiary": {
    "beneficiary_persons": [
      {
        "legal_person": {
          "name": {
            "name_identifiers": [
              {
                "legal_person_name": "ABC Limited",
                "legal_person_name_identifier_type": "LEGL"
              }
            ]
          }
        }
      }
    ],
    "account_numbers": [
      "rAPERVgXZavGgiGv6xBgtiZurirW2yAmY"
    ]
  }
}

private_info = sygna_bridge_util.crypto.encrypt_private_data(
    sensitive_data, 
    recipient_public_key
)
decoded_priv_info = sygna_bridge_util.crypto.decrypt_private_data(
    private_info, 
    recipient_private_key
)

Sign and Verify

In Sygna Bridge, we use secp256k1 ECDSA over sha256 of utf-8 json string to create signature on every API call. Since you need to provide the identical utf-8 string during verification, the order of key-value pair you put into the object is important.

The following example is the snippet of originator's signing process of permissionRequest API call. If you put the key transaction before private_info in the object, the verification will fail in the central server.

transaction = {
    "originator_vasp": {
        "vasp_code": "VASPUSNY1",
        "addrs": [
          {
            "address": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
            "addr_extra_info": []
          }
        ]
      },
    "beneficiary_vasp": {
        "vasp_code": "VASPUSNY2",
        "addrs": [
          {
            "address": "rAPERVgXZavGgiGv6xBgtiZurirW2yAmY",
            "addr_extra_info": [
              {
                "tag": "abc"
              }
            ]
          }
        ]
      },
    "currency_id": "sygna:0x80000090",
    "amount": "4.51120135938784"
}

data_dt = "2019-07-29T06:28:00Z"

# using sign_data to get a valid signed object (with signature attached)

data_to_sign = {
    "private_info":private_info,
    "transaction":transaction,
    "data_dt":data_dt
}

sygna_bridge_util.crypto.sign_data(data_to_sign, originator_private_key)

valid = sygna_bridge_util.crypto.verify_data(obj, originator_public_Key)

# or you can use the method that's built for `transfer` request:
signed_data = sygna_bridge_util.crypto.sign_permission_request(
    data_to_sign, 
    originator_private_key
)

valid = sygna_bridge_util.crypto.verify_data(
    signed_data, 
    originator_public_Key
)

We provide different methods like sign_permission_request, sign_callback() to sign different objects(or parameters) we specified in our api doc. You can also find more examples in the following section.

API

API calls to communicate with Sygna Bridge server.

We use basic auth with all the API calls. To simplify the process, we provide a API class to deal with authentication and post/ get request format.

sb_server = "https://api.sygna.io/"
sb_api_instance = sygna_bridge_util.API("api-key", sb_server)

After you create the API instance, you can use it to make any API call to communicate with Sygna Bridge central server.

Get VASP Information

# Get List of VASPs associated with public keys.
verify = True # set verify to true to verify the signature attached with api response automatically.
vasps = sb_api_instance.get_vasp_list(verify)

# Or call use get_vasp_public_key() to directly get public key for a specific VASP.
public_key =  sb_api_instance.get_vasp_public_key("10298", verify)

For Originator

There are two API calls from transaction originator to Sygna Bridge Server defined in the protocol, which are post_permission_request and post_transaction_id.

The full logic of originator would be like the following:

# originator.py
recipient_public_key = sb_api_instance.get_vasp_public_key("10298")
private_info = sygna_bridge_util.crypto.sygna_encrypt_private_data(
    # example from above
    sensitive_data, 
    recipient_public_key
)

data_dt = "2019-07-29T07:29:80Z"

data_to_sign = {
    "private_info":private_info,
    # from example above
    "transaction":transaction,
    "data_dt":data_dt
}

transfer_data = sygna_bridge_util.crypto.sign_permission_request(
    data_to_sign, 
    sender_privKey
)

callback_url = "https://81f7d956.ngrok.io/v2/originator/transaction/premission"
callback_data = sygna_bridge_util.crypto.sign_callback(
    {
        "callback_url":callback_url
    }, 
    sender_privKey
)

response = sb_api_instance.post_permission_request(
    {
        "data":transfer_data,
        "callback":callback_data
    }
)

# Broadcast your transaction to blockchain after got and api response at your api server.
txid = "1a0c9bef489a136f7e05671f7f7fada2b9d96ac9f44598e1bcaa4779ac564dcd"

# Inform Sygna Bridge that a specific transfer is successfully broadcasted to the blockchain.

send_tx_id_data = sygna_bridge_util.crypto.sign_transaction_id(
    {
        "transfer_id":response["transfer_id"], 
        "txid":txid
    }, 
    sender_privKey
)
post_tx_id_response = sb_api_instance.post_transaction_id(send_tx_id_data)

For Beneficiary

There is only one api for Beneficiary VASP to call, which is post_permission. After the beneficiary server confirm their legitimacy of a transfer request, they will sign { transfer_id, permission_status } using sign_permission() function, and send the result with signature to Sygna Bridge Central Server.

permission_status = "ACCEPTED" # or "REJECTED"
permission_data = sygna_bridge_util.crypto.sign_permission(
    {
        "transfer_id":response["transfer_id"],         
        "permission_status":permission_status
    }, 
    beneficiary_private_key
)
final_result = sb_api_instance.post_permission(permission_data)

For more complete example, please refer to Example file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sygna_bridge_util-2.0.4.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sygna_bridge_util-2.0.4-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file sygna_bridge_util-2.0.4.tar.gz.

File metadata

  • Download URL: sygna_bridge_util-2.0.4.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/8.8.0 pkginfo/1.12.1.2 requests/2.31.0 requests-toolbelt/1.0.0 tqdm/4.67.3 CPython/3.11.15

File hashes

Hashes for sygna_bridge_util-2.0.4.tar.gz
Algorithm Hash digest
SHA256 e8db30818c97bf352cda88be0cf7e8ccfd8d2478910cef8b6cfd77a1f332ae46
MD5 8bfa3b35b3b4963527471b93953eb161
BLAKE2b-256 9b6aaa879c53a23c65dbd8647ab202204134108c1b655b42a3a6e7ee5dfe0a6b

See more details on using hashes here.

File details

Details for the file sygna_bridge_util-2.0.4-py3-none-any.whl.

File metadata

  • Download URL: sygna_bridge_util-2.0.4-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/8.8.0 pkginfo/1.12.1.2 requests/2.31.0 requests-toolbelt/1.0.0 tqdm/4.67.3 CPython/3.11.15

File hashes

Hashes for sygna_bridge_util-2.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1baf51fafd6a10cf3e3f8914d436288dbbac0add09dd82ea6f4ea20f87a3e817
MD5 9cb14e3bb5e3e0092b09738979d6c910
BLAKE2b-256 e0af337839fc2345d9362fdc3ff79f30b0c469f81e669eaec84d2a3841914df1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page