Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
feat: Create QRCode for Verifiable credential
Browse files Browse the repository at this point in the history
closes #46

Signed-off-by: talwinder.kaur <[email protected]>
  • Loading branch information
talwinder50 committed Feb 27, 2020
1 parent 194d051 commit 75481fe
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/issuer-agent/wasm/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 h1:J9b7z+QKAm
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086/go.mod h1:PLPIyL7ikehBD1OAjmKKiOEhbvWyHGaNDjquXMcYABo=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/square/go-jose/v3 v3.0.0-20191119004800-96c717272387 h1:PjfQbTWDEoNh4v+4NNirclXoCIxjjLXsqSAP1iYxuOM=
Expand Down
1 change: 1 addition & 0 deletions cmd/rp-agent/wasm/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 h1:J9b7z+QKAm
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086/go.mod h1:PLPIyL7ikehBD1OAjmKKiOEhbvWyHGaNDjquXMcYABo=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/square/go-jose/v3 v3.0.0-20191119004800-96c717272387 h1:PjfQbTWDEoNh4v+4NNirclXoCIxjjLXsqSAP1iYxuOM=
Expand Down
1 change: 1 addition & 0 deletions cmd/user-agent/wasm/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 h1:J9b7z+QKAm
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086/go.mod h1:PLPIyL7ikehBD1OAjmKKiOEhbvWyHGaNDjquXMcYABo=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/square/go-jose/v3 v3.0.0-20191119004800-96c717272387 h1:PjfQbTWDEoNh4v+4NNirclXoCIxjjLXsqSAP1iYxuOM=
Expand Down
1 change: 1 addition & 0 deletions cmd/user-agent/web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SPDX-License-Identifier: Apache-2.0
<li><router-link to="/" exact>Homepage</router-link></li>
<li><router-link to="/AcceptInvitation">Accept Invitation</router-link></li>
<li><router-link to="/RegisterWallet">Register Wallet</router-link></li>
<li><router-link to="/MyVC">My Credentials</router-link></li>
</ul>
<router-view></router-view>
</div>
Expand Down
5 changes: 5 additions & 0 deletions cmd/user-agent/web/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export default new Router({
path: "/GetVC",
component: () => import("@/views/GetVC")
},
{
name: "myVC",
path: "/MyVC",
component: () => import("@/views/myVC")
},
{
name: "registerWallet",
path: "/RegisterWallet",
Expand Down
51 changes: 51 additions & 0 deletions cmd/user-agent/web/src/views/myVC.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

<template>
<div>
<div> Create QR Code </div>
<br>
<div>
<select id="selectVC">
<option value="0" >Select VC</option>
</select>
<br>
<br>
<button id='getVCBtn'>Get QR Code</button>
<br>
<br>
<img src="" id="qr-result"/>
</div>
</div>
</template>
<script>
async function generateQRCode() {
window.onload = function () {
document.getElementById('getVCBtn').addEventListener('click', () => {
var e = document.getElementById("selectVC");
var vcValue = e.options[e.selectedIndex].value;
if (vcValue == 0) {
alert("please select vc")
return
}
window.createQRCode(vcValue);
});
}
}
export default{
beforeCreate:function(){
// Add an event listener
document.addEventListener("afterLoadingWasm", function() {
var select = document.getElementById("selectVC");
window.populateVC(select)
});
window.$webCredentialHandler=this.$webCredentialHandler
generateQRCode()
},
}
</script>
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ go 1.13

require (
github.com/hyperledger/aries-framework-go v0.1.2
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086
github.com/stretchr/testify v1.4.0

)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/hyperledger/aries-framework-go v0.1.2-0.20200220221823-fb5c1d69a845 h1:bWjirDFdao/xtNxXWrZYoSaM+v2hMfPP9r/dtPcFHII=
github.com/hyperledger/aries-framework-go v0.1.2-0.20200220221823-fb5c1d69a845/go.mod h1:/Fy3iGC4G2sD4HgKDC17CGcLLFFBTTgR5lEp2INNXbw=
github.com/hyperledger/aries-framework-go v0.1.2-0.20200226031908-1aef12b7b826 h1:SKdGTcCWO5FyNh2eUdL0b00sN11UOCBBqd8mutCesYY=
github.com/hyperledger/aries-framework-go v0.1.2-0.20200226031908-1aef12b7b826/go.mod h1:e2VM3jE+OQgICspFVhUkV6n1MmiHXaD+6xG7TxdHaGI=
github.com/hyperledger/aries-framework-go v0.1.2 h1:7KIj/6Z9xYb+M951/GlIE48LqFAVbTBju1rB/e4zCgk=
github.com/hyperledger/aries-framework-go v0.1.2/go.mod h1:p7zzPz5BTer52kgbbEaLuNFtZ83Q7GnW1cUnKkIRDc0=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down Expand Up @@ -104,6 +106,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 h1:J9b7z+QKAmPf4YLrFg6oQUotqHQeUNWwkvo7jZp1GLU=
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086 h1:RYiqpb2ii2Z6J4x0wxK46kvPBbFuZcdhS+CIztmYgZs=
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086/go.mod h1:PLPIyL7ikehBD1OAjmKKiOEhbvWyHGaNDjquXMcYABo=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/square/go-jose/v3 v3.0.0-20191119004800-96c717272387 h1:PjfQbTWDEoNh4v+4NNirclXoCIxjjLXsqSAP1iYxuOM=
Expand Down
62 changes: 60 additions & 2 deletions pkg/store/vc/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ SPDX-License-Identifier: Apache-2.0
package vc

import (
"encoding/base64"
"fmt"
"strings"
"syscall/js"

qrcode "github.com/skip2/go-qrcode"

"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
"github.com/hyperledger/aries-framework-go/pkg/storage"
verifiablestore "github.com/hyperledger/aries-framework-go/pkg/store/verifiable"
)

const (
friendlyNamePreFix = "fr_"
responseType = "Response"
)

type provider interface {
Expand Down Expand Up @@ -46,6 +50,7 @@ func RegisterHandleJSCallback(ctx provider) error {
js.Global().Set("storeVC", js.FuncOf(c.storeVC))
js.Global().Set("populateVC", js.FuncOf(c.populateVC))
js.Global().Set("getVC", js.FuncOf(c.getVC))
js.Global().Set("createQRCode", js.FuncOf(c.createQRCode))

return nil
}
Expand All @@ -60,7 +65,7 @@ func (c *callback) storeVC(this js.Value, inputs []js.Value) interface{} {
// https://github.com/golang/go/issues/26382
go func() {
m := make(map[string]interface{})
m["dataType"] = "Response"
m["dataType"] = responseType
m["data"] = "success"
vcData := inputs[0].Get("credential").Get("data").String()
vc, err := verifiable.NewUnverifiedCredential([]byte(vcData))
Expand Down Expand Up @@ -108,7 +113,7 @@ func (c *callback) getVC(this js.Value, inputs []js.Value) interface{} {
// https://github.com/golang/go/issues/26382
go func() {
m := make(map[string]interface{})
m["dataType"] = "Response"
m["dataType"] = responseType

vcID, err := c.vcFriendlyNameStore.Get(friendlyNamePreFix + inputs[1].String())
if err != nil {
Expand Down Expand Up @@ -137,3 +142,56 @@ func (c *callback) getVC(this js.Value, inputs []js.Value) interface{} {

return nil
}

func (c *callback) createQRCode(this js.Value, inputs []js.Value) interface{} {
// https://github.com/golang/go/issues/26382
// todo unit-test this function https://github.com/trustbloc/edge-agent/issues/54
go func() {
m := make(map[string]interface{})
m["dataType"] = responseType

vcID, err := c.vcFriendlyNameStore.Get(friendlyNamePreFix + inputs[0].String())
if err != nil {
m["data"] = fmt.Sprintf("failed to get in vc friendly name store: %s", err.Error())
return
}

vc, err := c.store.GetVC(string(vcID))
if err != nil {
m["data"] = fmt.Sprintf("failed to get in vc store: %s", err.Error())
return
}
vcBytes, err := vc.MarshalJSON()
if err != nil {
m["data"] = fmt.Sprintf("failed to marshal vc: %s", err.Error())
return
}
generateQRCode(string(vcBytes))
}()

return nil
}

func generateQRCode(data string) {
var generateQRCode js.Func

document := js.Global().Get("document")

generateQRCode = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if len(data) > 0 {
fmt.Println("Building QR Code for", data)

png, err := qrcode.Encode(data, qrcode.Medium, 256)
if err != nil {
fmt.Println("Failed generating QR Code. Error=", err.Error())
}

b64Encoded := base64.StdEncoding.EncodeToString(png)
image := document.Call("getElementById", "qr-result")
image.Call("setAttribute", "src", "data:image/png;base64, "+b64Encoded)
}
return nil
})

document.Call("getElementById", "getVCBtn").Call("addEventListener", "click", generateQRCode)
}

0 comments on commit 75481fe

Please sign in to comment.