-
Notifications
You must be signed in to change notification settings - Fork 5
Apl 2090 create apollo web crypto doc #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0486
wants to merge
4
commits into
stage
Choose a base branch
from
APL-2090-Create-Apollo-web-crypto-doc
base: stage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <html><head><meta content="text/html; charset=UTF-8" http-equiv="content-type"><style type="text/css">@import url('https://themes.googleusercontent.com/fonts/css?kit=lhDjYqiy3mZ0x6ROQEUoUw');ol{margin:0;padding:0}table td,table th{padding:0}.c13{border-right-style:solid;padding:5pt 5pt 5pt 5pt;border-bottom-color:#000000;border-top-width:0pt;border-right-width:0pt;border-left-color:#000000;vertical-align:top;border-right-color:#000000;border-left-width:0pt;border-top-style:solid;background-color:#333333;border-left-style:solid;border-bottom-width:0pt;width:468pt;border-top-color:#000000;border-bottom-style:solid}.c7{color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:16pt;font-family:"Arial";font-style:normal}.c3{padding-top:18pt;padding-bottom:6pt;line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}.c8{padding-top:16pt;padding-bottom:4pt;line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}.c9{color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:"Arial";font-style:normal}.c0{color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:"Courier New";font-style:normal}.c23{color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:20pt;font-family:"Arial";font-style:normal}.c22{padding-top:20pt;padding-bottom:6pt;line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}.c20{color:#434343;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:14pt;font-family:"Arial";font-style:normal}.c2{padding-top:0pt;padding-bottom:0pt;line-height:1.15;orphans:2;widows:2;text-align:left}.c19{background-color:#333333;font-family:"Consolas";color:#888888;font-weight:400}.c11{background-color:#333333;font-family:"Consolas";color:#ffffaa;font-weight:400}.c14{background-color:#333333;font-family:"Consolas";color:#a2fca2;font-weight:400}.c6{background-color:#333333;font-family:"Consolas";color:#fcc28c;font-weight:400}.c4{background-color:#333333;font-family:"Consolas";color:#ffffff;font-weight:400}.c12{padding-top:0pt;padding-bottom:0pt;line-height:1.15;text-align:left}.c10{border-spacing:0;border-collapse:collapse;margin-right:auto}.c17{background-color:#333333;font-family:"Consolas";color:#d36363;font-weight:400}.c18{text-decoration:none;vertical-align:baseline;font-size:11pt;font-style:normal}.c16{background-color:#ffffff;max-width:468pt;padding:72pt 72pt 72pt 72pt}.c15{font-weight:400;font-family:"Courier New"}.c1{height:0pt}.c5{height:11pt}.c21{height:0.6pt}.title{padding-top:0pt;color:#000000;font-size:26pt;padding-bottom:3pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}.subtitle{padding-top:0pt;color:#666666;font-size:15pt;padding-bottom:16pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}li{color:#000000;font-size:11pt;font-family:"Arial"}p{margin:0;color:#000000;font-size:11pt;font-family:"Arial"}h1{padding-top:20pt;color:#000000;font-size:20pt;padding-bottom:6pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h2{padding-top:18pt;color:#000000;font-size:16pt;padding-bottom:6pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h3{padding-top:16pt;color:#434343;font-size:14pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h4{padding-top:14pt;color:#666666;font-size:12pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h5{padding-top:12pt;color:#666666;font-size:11pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h6{padding-top:12pt;color:#666666;font-size:11pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;font-style:italic;orphans:2;widows:2;text-align:left}</style></head><body class="c16"><h1 class="c22" id="h.vp1iv35cz2m7"><span class="c23">Apollo Web Crypto typescript library</span></h1><h2 class="c3" id="h.y24qtcnfpfsy"><span class="c7">Introduction</span></h2><p class="c2"><span class="c9">Apollo web crypto library is used for offline transaction processing - create and sign transactions with no secretPhrase or secret key disclosing in communication with Apollo Blockchain node.</span></p><p class="c2 c5"><span class="c9"></span></p><h2 class="c3" id="h.g30nw3unjyhm"><span class="c7">Quick Start Guide</span></h2><h3 class="c8" id="h.eqp10lcsk8ku"><span class="c20">Installation</span></h3><p class="c2 c5"><span class="c9"></span></p><p class="c2"><span>First, You need to install the apl-web-crypto</span><span class="c15"> </span><span class="c9">nodejs package.</span></p><p class="c2 c5"><span class="c0"></span></p><p class="c2"><span class="c0">npm install apl-web-crypto</span></p><p class="c2 c5"><span class="c0"></span></p><p class="c2"><span class="c9">Second, we need to configure the Apollo Node we have to use.</span></p><p class="c2"><span class="c9">Apollo web crypto uses dotenv module for configuration:</span></p><h3 class="c8" id="h.97uxxxwbsp64"><span>Project setup</span></h3><p class="c2"><span>Create a</span><span class="c15"> .env </span><span class="c9">file at the root of the project add the following line into it.</span></p><p class="c2 c5"><span class="c0"></span></p><a id="t.d9dae14da08ed2b33ee7c11a5b34a9ff694e49a5"></a><a id="t.0"></a><table class="c10"><tbody><tr class="c1"><td class="c13" colspan="1" rowspan="1"><p class="c12"><span class="c4">APL_SERVER=https:</span><span class="c19">//wallet.test.apollowallet.org</span></p></td></tr></tbody></table><p class="c2 c5"><span class="c0"></span></p><p class="c2"><span class="c9">Then we will create an example JavaScript file, simpleTransactionSign.js</span></p><p class="c2"><span>Add following lines into simpleTransactionSign.js file</span><span class="c0"> </span></p><p class="c2 c5"><span class="c0"></span></p><a id="t.ada6b36c7f517d6363759ee5bda8cf13dd2e225a"></a><a id="t.1"></a><table class="c10"><tbody><tr class="c1"><td class="c13" colspan="1" rowspan="1"><p class="c12"><span class="c6">import</span><span class="c4"> { Transaction } </span><span class="c6">from</span><span class="c4"> </span><span class="c14">"apl-web-crypto"</span><span class="c4">;<br></span><span class="c11">require</span><span class="c4">(</span><span class="c14">"dotenv"</span><span class="c4">).config()</span></p></td></tr></tbody></table><p class="c2 c5"><span class="c0"></span></p><h3 class="c8" id="h.hd4r8eso1kyr"><span>Transaction creation</span></h3><p class="c2"><span class="c9">To create transactions in Apollo, we have to add to transaction data several fields. In this document, we will use sendMoney transaction. For sendMoney, we have to fill following fields: amountATM, deadline, recipient, feeATM. One of publicKey or secretPhrase. For offline signing, we do not want to disclose our secretPhrase, so we will fill publicKey. It has to be sender’s publicKey</span></p><p class="c2 c5"><span class="c9"></span></p><a id="t.fa906cc147294db76354052cbc84c384e8d9b4cb"></a><a id="t.2"></a><table class="c10"><tbody><tr class="c1"><td class="c13" colspan="1" rowspan="1"><p class="c12"><span class="c19">// Apollo has 8 decimals</span><span class="c4"><br></span><span class="c6">const</span><span class="c4"> ONE_APL = </span><span class="c17">100000000</span><span class="c4">;<br></span><span class="c6">const</span><span class="c4"> data = {<br> requestType: </span><span class="c14">'sendMoney'</span><span class="c4">,<br> publicKey: </span><span class="c14">"24a88cc13c9a3a9f06c636b58c5b607fcb7cff14d449325691fa448933e6b870"</span><span class="c4">,<br> amountATM: </span><span class="c17">2</span><span class="c4"> * ONE_APL,<br> feeATM: ONE_APL,<br> deadline: </span><span class="c17">60</span><span class="c4">,<br> recipient: </span><span class="c14">"APL-RQTU-56W2-AAMY-7MTLB"</span><span class="c4"><br> };</span></p></td></tr></tbody></table><p class="c2 c5"><span class="c0"></span></p><h3 class="c8" id="h.yf9jnrvaerr7"><span>Get not signed transaction bytes</span></h3><p class="c2"><span class="c9">At first, we can get Transaction Bytes from the server, and then we will perform offline signing. Note, that this way we can get Transaction Bytes for any transaction type.</span></p><p class="c2 c5"><span class="c9"></span></p><a id="t.db16cb23db65f0290c48246686c5bf70431f7b60"></a><a id="t.3"></a><table class="c10"><tbody><tr class="c1"><td class="c13" colspan="1" rowspan="1"><p class="c12"><span class="c19">// function notSign used to get transaction bytes without </span><span class="c4"><br></span><span class="c19">// signing</span><span class="c4"><br></span><span class="c6">async</span><span class="c4"> </span><span class="c6">function</span><span class="c4"> </span><span class="c11">getNotSignedData</span><span class="c4">(data)<br>{<br> </span><span class="c6">const</span><span class="c4"> result = </span><span class="c6">await</span><span class="c4"> Transaction.sendNotSign(data);<br> </span><span class="c6">return</span><span class="c4"> result;<br>}<br><br></span><span class="c6">let</span><span class="c4"> unsignedTransactionData = getNotSignedData(data)<br>.then(unsignedResponse => {<br> </span><span class="c19">/* process unsigned transaction here */</span><span class="c4"><br> </span><span class="c11">console</span><span class="c4">.log(unsignedResponse);<br>}).catch();</span></p></td></tr></tbody></table><p class="c2 c5"><span class="c0"></span></p><p class="c2"><span class="c9">This way we will get unsigned transaction data from the server.</span></p><h3 class="c8" id="h.7dow9o7bfwia"><span>Sign transaction</span></h3><p class="c2 c5"><span class="c0"></span></p><p class="c2"><span class="c9">To sign transaction we have to add sign function:</span></p><p class="c2 c5"><span class="c0"></span></p><a id="t.b7799e3b3e1c4299abadc212a2845193fbf2953b"></a><a id="t.4"></a><table class="c10"><tbody><tr class="c1"><td class="c13" colspan="1" rowspan="1"><p class="c12"><span class="c6">async</span><span class="c4"> </span><span class="c6">function</span><span class="c4"> </span><span class="c11">sign</span><span class="c4">(secretPhrase, unsignedResponse)<br>{<br> </span><span class="c6">const</span><span class="c4"> sendData = {secretPhrase: secretPhrase };<br> </span><span class="c6">const</span><span class="c4"> result = </span><span class="c6">await</span><span class="c4"> Transaction.processOfflineSign(sendData, unsignedResponse);<br> </span><span class="c6">return</span><span class="c4"> result;<br>}</span></p></td></tr></tbody></table><p class="c2 c5"><span class="c0"></span></p><p class="c2"><span class="c9">Then, we can modify the code, to add transaction signing, and processing (broadcast):</span></p><p class="c2 c5"><span class="c9"></span></p><a id="t.04787b757644cf7213a025f74ca01c8aadc0b4c2"></a><a id="t.5"></a><table class="c10"><tbody><tr class="c1"><td class="c13" colspan="1" rowspan="1"><p class="c12"><span class="c4">getNotSignedData(data)<br>.then(unsignedResponse => {<br> </span><span class="c19">/* process unsigned transaction here */</span></p><p class="c12"><span class="c11"> console</span><span class="c4">.log(unsignedResponse);<br> sign(</span><span class="c14">"10"</span><span class="c4">, unsignedResponse).then(signedBytes => {<br> </span><span class="c19">/* process signed transaction here */</span></p><p class="c12"><span class="c11"> console</span><span class="c4">.log(signedBytes);<br> }).catch();<br> <br>}).catch();</span></p></td></tr><tr class="c21"><td class="c13" colspan="1" rowspan="1"><p class="c12 c5"><span class="c4 c18"></span></p></td></tr></tbody></table><p class="c2 c5"><span class="c9"></span></p><p class="c2 c5"><span class="c9"></span></p><p class="c2 c5"><span class="c0"></span></p><p class="c2 c5"><span class="c9"></span></p><p class="c2 c5"><span class="c0"></span></p><p class="c2 c5"><span class="c0"></span></p><p class="c2 c5"><span class="c9"></span></p><p class="c2 c5"><span class="c9"></span></p><p class="c2 c5"><span class="c9"></span></p><p class="c2 c5"><span class="c9"></span></p></body></html> | ||
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| APL_SERVER=http://localhost:7876 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "name": "SignTransactionSample", | ||
| "version": "1.0.0", | ||
| "description": "Sign transaction sample", | ||
| "main": "server.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1", | ||
| "start": "node signTransaction.js" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://firstb@bitbucket.org/firstbridge_company/apollo-faucet.git" | ||
| }, | ||
| "author": "Rostyslav Golda", | ||
| "license": "GPL-2.0-or-later", | ||
| "homepage": "https://bitbucket.org/firstbridge_company/apollo-faucet#readme", | ||
| "dependencies": { | ||
| "apl-web-crypto": "^1.0.0", | ||
| "request": "^2.88.2", | ||
| "sqlite3": "^4.0.8", | ||
| "typescript": "^4.4.3" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^16.9.4", | ||
| "dotenv": "^10.0.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| const { Crypto, Transaction } = require('apl-web-crypto'); | ||
| require('dotenv').config(); | ||
|
|
||
| (async () => { | ||
| // Apollo has 8 decimals | ||
| const ONE_APL = 100000000; | ||
| const secretPhrase = '10'; | ||
| const publicKey = Crypto.getPublicKey(secretPhrase); | ||
| const data = { | ||
| requestType: 'sendMoney', | ||
| publicKey, | ||
| amountATM: 2 * ONE_APL, | ||
| feeATM: ONE_APL, | ||
| deadline: 60, | ||
| recipient: "APL-RQTU-56W2-AAMY-7MTLB", | ||
| secretPhrase | ||
| }; | ||
|
|
||
| const signAndSend = async (dataObj) => { | ||
| return await Transaction.sendWithOfflineSign(dataObj); | ||
| } | ||
|
|
||
| try { | ||
| const serverResponse = await signAndSend(data) | ||
| console.log(serverResponse); | ||
| } catch (e) { | ||
| console.log(e); | ||
| } | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| const { Crypto, Transaction } = require('apl-web-crypto'); | ||
| require('dotenv').config(); | ||
|
|
||
| (async () => { | ||
| // Apollo has 8 decimals | ||
| const ONE_APL = 100000000; | ||
| const secretPhrase = '10'; | ||
| const publicKey = Crypto.getPublicKey(secretPhrase); | ||
| const data = { | ||
| requestType: 'sendMoney', | ||
| publicKey, | ||
| amountATM: 2 * ONE_APL, | ||
| feeATM: ONE_APL, | ||
| deadline: 60, | ||
| recipient: 'APL-RQTU-56W2-AAMY-7MTLB', | ||
| }; | ||
|
|
||
| // function notSign used to get transaction bytes without signing | ||
| const getNotSignedData = async (dataObj) => { | ||
| return await Transaction.sendNotSign(dataObj); | ||
| }; | ||
|
|
||
| const sign = async (secretPhrase, unsignedResponse) => { | ||
| const sendData = { secretPhrase }; | ||
| return await Transaction.processOfflineSign(sendData, unsignedResponse); | ||
| }; | ||
|
|
||
| try { | ||
| // process unsigned transaction here | ||
| const unsignedResponse = await getNotSignedData(data); | ||
| console.log(unsignedResponse); | ||
|
|
||
| // process signed transaction here | ||
| const signedBytes = await sign(secretPhrase, unsignedResponse); | ||
| console.log(signedBytes); | ||
| } catch (e) { | ||
| console.log(e); | ||
| } | ||
| })(); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double dot in file name - .......tlibrary..html