# Bridge Integration ## Wellcome Login Flow - Challenge Metamask - Create User (uid) - After signature -> JWT { sub: serial/uuid/custom, aud:} - BridgeAPI.getAddress/JWT/srcchain/chainId -> address - WebView QR Code / Handle Tronlink + Metamask ERC20 Transaction ## Nuances 1.) BSC USDT 18 denom (everybody else is 6) 2.) Bridge in = centralized Bridge Service 3.) Bridge Out = AminoX Dapp ### API ``` // Requires CORS host reflection #[get("/partners/depositAddress/{chain_type}/{chain_id}")] ``` ### AminoX Bridge OUT ABI ```solidity= pragma solidity ^0.8.0; interface IBridge { function transferBridgedOut(address to,uint256 value,uint256 targetChainId,uint256 targetChainType) external returns (bool); function transferBridgedIn(address to,uint256 value,uint256 sourceChainId,uint256 sourceChainType) external returns (bool); event Transfer( address indexed from, address indexed to, uint256 value ); event TransferBridgedOut( address indexed from, address indexed bridgeAddress, address indexed to, uint256 amount, uint256 targetChainId, uint256 targetChainType ); event TransferBridgedIn( address indexed from, address indexed to, uint256 amount, uint256 sourceChainId, uint256 sourceChainType ); event BridgeFee( address indexed from, uint256 amount ); } ``` ```javascript= try { const node = await window.tronLink.tronWeb.trx.getNodeInfo(); // console.log(node, 'node'); // 1 = shasta ,11111 = tron mainnet if ( Number(node.configNodeInfo.p2pVersion) === fromChain.chainId ) { const contract = await window.tronLink.tronWeb .contract() .at(ETronTokens.TronShasta); const result = await contract .transfer( formik.values.toAddress, //address _to parseInt( ethers.utils.parseUnits( values.amount, 6, )._hex, ), //amount ) .send({ feeLimit: 15000000, }) .then((output: any) => { // console.log('- Output:', output, '\n'); transfer.onSend(output, isTron); }) .catch((e: any) => { setIsModalOpen(true); setModalContent(SendWarning); transfer.onError(undefined, isTron); }); } ``` ## Denis Helper - Try to Provide Front End Tronlink/Metamask examples ### Guide to interact with TronLink - [Use TronLink with frontend](https://hackmd.io/j-lB0LkhQj6lsYV7Xo_8sg) ### Guide to withdraw - [Use Withdrawal with frontend](https://hackmd.io/@ut9ReZihS6KARIW05y3-Ew/HJfHbUsUo) ## Summary - [NEXT] Need to Convert to RSA-SHA256 asymetric JWT Signature - [NEXT] Needs to provide User's Post Challenge Wallet Address in JWT Claims (eg. { sub: 0xBEEF.. }) - [NEXT] Needs to be aware that BSC USDT is 18 denominations - [NEXT] Needs to develop the Web3/Tronweb triggers for direct wallet deposits and develop QR Code display for "getDepositAddress" response - [AC] Needs to provide Front end WEB3/Tronweb examples - [AC] Needs to provide API calling documentation - [AC] Needs to ensure CORS reflects all hosts for the "getDepositAddress" API route ### Next Staging RSA Pub Make sure to clean up newlines when copying over to .pem file ```next.stagingkey.pem -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvhKa49WtnGQxf0gW468e kpKE08vbJhzuHLWcI5Kuv5NgvtgrCqiEVXtac8Fv8sNKWKAUyTGw7v2vl5/lK4CJ Eo0ioD3gDxULk1LG90NRdqMETKy0aHat526w51qonOjKuFD7OnYuapOuWsARiTV6 lZ2mbDwYVtFSKmDEP1pC2hzsMyAZhaEPn0qqdONWECuG4Lq7JDiqExktx76BVRGa lMxbI66iNyRqSPwKf0p4k0o1RfyiPTwIXAf0Fn3wVtP6HAABmyzrU8D/oEgxNLwH 99vzmbVxqitzDFxvsQhXwrwH4BTboDxRhE8du0rCxCgDB9pLud1JdGtKmfUJidFR 4QIDAQAB -----END PUBLIC KEY----- ``` ### Sample JWT Payload, use jwt.io to read ``` eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIweDJCNzQzRDhhNzIyN0E2YzUwN0IzOTk2NkI2MTM2ZTU0MDVkNEFhRDIiLCJjdXN0SWQiOiIxIiwic2l0ZUlkIjoiMzM0NTY3OCIsIm5iZiI6MTY2NzM1NzYwMCwiZXhwIjoxNjc1OTk3NjAwfQ.WF6Zydw7i4TSP7VLw1-3G7NdP9r7slrNAlzqjuR6RWdwS2Sc1bvHvGXZqYhvir5N1Ua2T86cxdn7KUujgHJIaIaNNOOzQu3n6U0ZzWt5gqM2oLtxcmchiYcHtz4mPnTTU_yuZq8rMs-pwqkyjhEaLMLwrGxsz6DlN1h4u5trTX6_Qj6vWIw5pTJCnOZoZmjptI6deL_sLa3OydN-NBsNO-eQKSXW8BePDdm3LoV2gB41csHlN-rZJLJbdipGUAIgkE0CKe3HNKjPWCMokbsw5GEICNteTLt8DZ5bmtlptbjnDVDLXOlbqTd7XJDRfVkiFy6wkqEKw9mJgcYKghuPvQ ``` ### Get Deposit Address at Bridge ``` curl -X GET 'https://staging.bridge.sapi.alphacarbon.network/secure/partners/depositAddress/tron/1' -H 'Authorization:Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIweDJCNzQzRDhhNzIyN0E2YzUwN0IzOTk2NkI2MTM2ZTU0MDVkNEFhRDIiLCJjdXN0SWQiOiIxIiwic2l0ZUlkIjoiMzM0NTY3OCIsIm5iZiI6MTY2NzM1NzYwMCwiZXhwIjoxNjc1OTk3NjAwfQ.WF6Zydw7i4TSP7VLw1-3G7NdP9r7slrNAlzqjuR6RWdwS2Sc1bvHvGXZqYhvir5N1Ua2T86cxdn7KUujgHJIaIaNNOOzQu3n6U0ZzWt5gqM2oLtxcmchiYcHtz4mPnTTU_yuZq8rMs-pwqkyjhEaLMLwrGxsz6DlN1h4u5trTX6_Qj6vWIw5pTJCnOZoZmjptI6deL_sLa3OydN-NBsNO-eQKSXW8BePDdm3LoV2gB41csHlN-rZJLJbdipGUAIgkE0CKe3HNKjPWCMokbsw5GEICNteTLt8DZ5bmtlptbjnDVDLXOlbqTd7XJDRfVkiFy6wkqEKw9mJgcYKghuPvQ' -H "Origin: https://example.com" GET /secure/partners/depositAddress/:chainType/:chainId Host: bridge.api.alphacarbon.network Origin: * Authorization: Bearer ${jwt_token} ``` | Parameter | Type | Description | | -------- | -------- | ----------------------------------- | | chainType | Text | Chain type of the deposit address. | | chainId | Number | Chain id of the deposit address. | Response ``` HTTP/1.1 200 OK Content-Type: application/json Access-Control-Allow-Origin: * { "depositAddress": "0x1cd87be2489c0d4fc0d8705887e4fd5f0d3261c5" } ``` | Parameter | Type | Description | | -------- | -------- | ------------------------------------------- | | depositAddress | Text | Generated deposit address on target chain. | Supported Chain Now | chainType | chainId | | -------- | -------- | | tron | 11111 | | binance | 56 | Possible errors | Error code | Description | | ------------------| --------------- | | 400 Bad Request | Chain not supported. | | 401 Unauthorized | JWT token is invalid. | #### Staging URL ``` https://staging.bridge.sapi.alphacarbon.network/secure/partners/depositAddress/:chainType/:chainId ``` #### Production URL ``` https://bridge.api.alphacarbon.network/secure/partners/depositAddress/:chainType/:chainId ``` #### Curl Testing for CORS ``` curl -H "Origin: http://any.domain.com" \ -H "Access-Control-Request-Method: GET" \ -H "Access-Control-Request-Headers: authorization,connection,user-agent,dnt,content-type" \ -X OPTIONS --verbose \ https://staging.bridge.sapi.alphacarbon.network/secure/partners/depositAddress/eth/1 * Trying 35.220.175.183:443... * Connected to staging.bridge.sapi.alphacarbon.network (35.220.175.183) port 443 (#0) * ALPN, offering h2 * ALPN, offering http/1.1 * successfully set certificate verify locations: * CAfile: /etc/ssl/cert.pem * CApath: none * (304) (OUT), TLS handshake, Client hello (1): * (304) (IN), TLS handshake, Server hello (2): * (304) (IN), TLS handshake, Unknown (8): * (304) (IN), TLS handshake, Certificate (11): * (304) (IN), TLS handshake, CERT verify (15): * (304) (IN), TLS handshake, Finished (20): * (304) (OUT), TLS handshake, Finished (20): * SSL connection using TLSv1.3 / AEAD-CHACHA20-POLY1305-SHA256 * ALPN, server accepted to use h2 * Server certificate: * subject: CN=staging.bridge.sapi.alphacarbon.network * start date: Sep 30 07:52:30 2022 GMT * expire date: Dec 29 07:52:29 2022 GMT * subjectAltName: host "staging.bridge.sapi.alphacarbon.network" matched cert's "staging.bridge.sapi.alphacarbon.network" * issuer: C=US; O=Let's Encrypt; CN=R3 * SSL certificate verify ok. * Using HTTP2, server supports multiplexing * Connection state changed (HTTP/2 confirmed) * Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0 * Using Stream ID: 1 (easy handle 0x13d811000) > OPTIONS /secure/partners/depositAddress/eth/1 HTTP/2 > Host: staging.bridge.sapi.alphacarbon.network > user-agent: curl/7.79.1 > accept: */* > origin: http://any.domain.com > access-control-request-method: GET > access-control-request-headers: authorization > * Connection state changed (MAX_CONCURRENT_STREAMS == 250)! < HTTP/2 200 < access-control-allow-headers: content-type, accept, authorization < access-control-allow-methods: GET, POST, DELETE < access-control-allow-origin: http://any.domain.com < access-control-max-age: 3600 < date: Mon, 07 Nov 2022 18:00:10 GMT < vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers < content-length: 0 < * Connection #0 to host staging.bridge.sapi.alphacarbon.network left intact ```