This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit effacad
Showing
35 changed files
with
2,156 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,31 @@ | ||
/target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
|
||
/.idea/ | ||
/.mvn/ | ||
/mvnw | ||
/classes/ | ||
/mvnw.cmd | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/build/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ |
This file contains 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,165 @@ | ||
# 1. Introduction | ||
Java backend for TrustNet browser plugin | ||
## 1.1 Requirements | ||
1. Access to Indy pool | ||
2. libIndy v1.3.1 | ||
# 2. API | ||
## 2.1 Create new account | ||
Creates new account. Sets up user wallet and cretes first `did`. | ||
```json | ||
{"operation" : "CREATE_ACCOUNT", "params" :{"username" : "xxxx", "password" : "yyyy"}} | ||
``` | ||
Example response: | ||
```json | ||
{"status":"SUCCESS","value":{"username":"xxxxxx","walletname":"eb7a5aaf-9dfa-4e61-b5ed-eff9aaae2d8d"}} | ||
``` | ||
## 2.2 Login to account | ||
Logs in to account, returns `token` | ||
```json | ||
{"operation" : "LOGIN", "params" :{"username" : "xxxxxx", "password" : "yyyy"}} | ||
``` | ||
Example response: | ||
```json | ||
{"status":"SUCCESS","value":{"token":"66bd38e4-0e8d-46af-8b5c-2abada5f7eb7"}} | ||
``` | ||
|
||
## 2.3 Create new DID | ||
Creates new `DID` | ||
```json | ||
{"operation" : "CREATE_DID", "params" :{"token" : "66bd38e4-0e8d-46af-8b5c-2abada5f7eb7"}} | ||
``` | ||
Example response: | ||
```json | ||
{"status":"CREATED","value":{"did":"MsHajCPU2KjqVTYzgSYmwH","verkey":"CNeXKuzqdJMxbeC6sZHZCGyDjARqwQgkMaRTSX8CJ1p9"}} | ||
``` | ||
|
||
## 2.4 Sign data | ||
Signs data. | ||
Example request: | ||
```json | ||
{"operation" : "SIGN_DATA", "params" :{"token" : "66bd38e4-0e8d-46af-8b5c-2abada5f7eb7", "did" : "MsHajCPU2KjqVTYzgSYmwH", "datatosign" :"testdata"}} | ||
``` | ||
Example response: | ||
```json | ||
{"status":"SUCCESS","value":{"dataToSign":"testdata","wallet":"eb7a5aaf-9dfa-4e61-b5ed-eff9aaae2d8d","signature":"laOJ6ACNnBcRIwG+OtG1CE7FWtGK3WCiyndYZ84SN/KIjX5aMQpeyJ7ryfMiZ/0lrCE57jzcP3JC09MXCiHQAQ==","verkey":"CNeXKuzqdJMxbeC6sZHZCGyDjARqwQgkMaRTSX8CJ1p9","did":"MsHajCPU2KjqVTYzgSYmwH"}} | ||
``` | ||
## 2.5 Verify signature | ||
Verifies signature. | ||
Example command: | ||
```json | ||
{"operation" : "VERIFY_SIGNATURE", "params" :{"token" : "66bd38e4-0e8d-46af-8b5c-2abada5f7eb7", "did" : "MsHajCPU2KjqVTYzgSYmwH", "message" :"testdata", "signature" :"laOJ6ACNnBcRIwG+OtG1CE7FWtGK3WCiyndYZ84SN/KIjX5aMQpeyJ7ryfMiZ/0lrCE57jzcP3JC09MXCiHQAQ=="}} | ||
``` | ||
Example response: | ||
```json | ||
{"status":"SUCCESS","value":{"verified":true,"message":"testdata","signature":"laOJ6ACNnBcRIwG+OtG1CE7FWtGK3WCiyndYZ84SN/KIjX5aMQpeyJ7ryfMiZ/0lrCE57jzcP3JC09MXCiHQAQ==","did":"MsHajCPU2KjqVTYzgSYmwH"}} | ||
``` | ||
|
||
## 2.6 Log out | ||
Logs out and invalidates the token. | ||
Example command: | ||
```json | ||
{"operation" : "LOGOUT" , "params" :{"token" : "66bd38e4-0e8d-46af-8b5c-2abada5f7eb7"}} | ||
``` | ||
Example response: | ||
```json | ||
"status":"SUCCESS","value":{"loggedout":true}} | ||
``` | ||
|
||
|
||
# 3. Standalone | ||
Make sure `RUN_AS_EXTENSION = false` in `Configuration.java`. Check that the `NETWORK_NAME` in `Configuration.java` | ||
matches with your Indy pool configuration. | ||
|
||
If `CREATE_USER`or `LOGIN` commands seem to hang decrease `workload`parameter in `userstore.java`. | ||
|
||
|
||
# 4. Browser extension | ||
Make sure `RUN_AS_EXTENSION = true` in `Configuration.java`.' Check that the `NETWORK_NAME` in `Configuration.java` | ||
matches with your Indy pool configuration. | ||
|
||
Build JAR and make it executable or create script that runs the JAR. | ||
## 4.1 Chrome configuration | ||
The following is for enabling for individual user.' | ||
Create file `fi.trustnet.browserplugin.json file` to `~/.config/google-chrome/NativeMessagingHosts' directory. | ||
File contents: | ||
```json | ||
{ | ||
"name": "fi.trustnet.browserplugin", | ||
"description": "TrustNet browser plugin backend", | ||
"path": "PATH_TO_EXECUTABLE_JAR", | ||
"type": "stdio", | ||
"allowed_origins": [ | ||
"chrome-extension://YOUR_EXTENSION_ID/" | ||
] | ||
} | ||
``` | ||
|
||
|
||
## 4.2 Usage with Chrome | ||
### 4.2.1 Demo | ||
directory `example-app` contains example Crome App that can communicate with the native extension. To install it: | ||
1. Open Chrome | ||
2. go to `chrome://extensions/` | ||
3. Make sure Developer mode is enabled | ||
4. Select Load unpacked | ||
5. No the extension ID, make sure it is same as in `fi.trustnet.browserplugin.json` file, if not change the id in the file. | ||
6. Navigate to example-app directory and open it | ||
7. Go to `chrome://apps` and select TrustNet Demo | ||
|
||
### 4.2.1 Extension-background-native communication | ||
|
||
In background script have something like | ||
```code | ||
let port = null | ||
... | ||
chrome.runtime.onMessage.addListener((request) =>{ | ||
if(request.operation==="CONNECT" { | ||
let hostName="fi.trustnet.browserplugin" | ||
port = chrome.runtime,connectNative(hostName) | ||
port.onMessage.addListener(onNativeMessage) | ||
port.onDisconnect.addListener(onDisconnected) | ||
} | ||
if(request.operation==="CREATE_ACCOUNT){ | ||
port.postMessage(request) | ||
} | ||
//Other messages | ||
} | ||
const onNativeMessage = (message) => { | ||
chrome.runtime.sendMessage({from : "background", | ||
subject: "message", | ||
message: "message"}) | ||
} | ||
const onDisconnected = () => { | ||
port=null | ||
chrome.runtime.sendMessage({from : "background", | ||
subject: "message", | ||
message: "DISCONNECTED"}) | ||
} | ||
``` | ||
|
||
Code in Extension side: | ||
```code | ||
chrome.runtime.sendMessage(EXTENSION_ID, MESSAGE} | ||
``` | ||
Examples: | ||
```code | ||
const connect=()=>{ | ||
chrome.runtime.sendMessage("kjhkdhldjldwlmlcwdd", {operation: "CONNECT"}) | ||
} | ||
const createAccount=(username, password) => { | ||
chrome.runtime.sendMessage("kjhkdhldjldwlmlcwdd",{"operation" : "CREATE_ACCOUNT", "params"} :{"username" : username, "password" : password}} | ||
} | ||
``` | ||
Receive messages from background | ||
```code | ||
chrome.runtime.onMessage.addListener((message, sender)=> { | ||
if(sender==="background") { | ||
//handle messages from native extension | ||
} | ||
} | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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,14 @@ | ||
<!DOCTYPE html> | ||
|
||
<html> | ||
<head> | ||
<script src='./main.js'></script> | ||
</head> | ||
<body> | ||
<button id='connect-button'>Connect</button> | ||
<input id='input-text' type='text' maxlength="300" size="200"/> | ||
<button id='send-message-button'>Send</button> | ||
<div id='response'></div> | ||
</body> | ||
</html> | ||
|
This file contains 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,50 @@ | ||
var port = null; | ||
|
||
function appendMessage(text) { | ||
document.getElementById('response').innerHTML += "<p>" + text + "</p>"; | ||
} | ||
|
||
function updateUiState() { | ||
if (port) { | ||
document.getElementById('connect-button').style.display = 'none'; | ||
document.getElementById('input-text').style.display = 'block'; | ||
document.getElementById('send-message-button').style.display = 'block'; | ||
} else { | ||
document.getElementById('connect-button').style.display = 'block'; | ||
document.getElementById('input-text').style.display = 'none'; | ||
document.getElementById('send-message-button').style.display = 'none'; | ||
} | ||
} | ||
|
||
function sendNativeMessage() { | ||
message = JSON.parse(document.getElementById('input-text').value) | ||
port.postMessage(message); | ||
appendMessage("Sent message: <b>" + JSON.stringify(message) + "</b>"); | ||
} | ||
|
||
function onNativeMessage(message) { | ||
appendMessage("Received message: <b>" + JSON.stringify(message) + "</b>"); | ||
} | ||
|
||
function onDisconnected() { | ||
appendMessage("Failed to connect: " + chrome.runtime.lastError.message); | ||
port = null; | ||
updateUiState(); | ||
} | ||
|
||
function connect() { | ||
var hostName = "fi.trustnet.browserplugin"; | ||
appendMessage("Connecting") | ||
port = chrome.runtime.connectNative(hostName); | ||
port.onMessage.addListener(onNativeMessage); | ||
port.onDisconnect.addListener(onDisconnected); | ||
updateUiState(); | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', function () { | ||
document.getElementById('connect-button').addEventListener( | ||
'click', connect); | ||
document.getElementById('send-message-button').addEventListener( | ||
'click', sendNativeMessage); | ||
updateUiState(); | ||
}); |
This file contains 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,17 @@ | ||
{ | ||
"name": "TrustNet Demo", | ||
"version": "1.0", | ||
"manifest_version": 2, | ||
"description": "COmunication with native wallet app", | ||
"app": { | ||
"launch": { | ||
"local_path": "main.html" | ||
} | ||
}, | ||
"icons": { | ||
"128": "icon-128.png" | ||
}, | ||
"permissions": [ | ||
"nativeMessaging" | ||
] | ||
} |
Oops, something went wrong.