Skip to content

Commit

Permalink
feat: support for did-key method (#16)
Browse files Browse the repository at this point in the history
* feat: adds husky, lint-staged and commitlint

* fix: fixes typo in .lintstagedrc.js

* feat: simplifies -eslintrc.js

* fix: update .eslintrc.js and commit-msg

* feat: add audit-ci

* chore: update package.json

* feat: add did:key

* feat: add new utility methods

* refactor: move did_utils to new file

* refactor: move did utils to new file

* feat: add new RPC methods

* fix: fix missing structuredClone func

* feat: add support for mainnet

* fix: enforce available methods

* fix: fixed empty vc_id bug

* refactor: typo

* refactor: double quotes

* fix: add promt to switchMethod

Co-authored-by: martines3000 <domajnko.martin@gmail.com>
Co-authored-by: Martin Domajnko <35891136+martines3000@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 18, 2022
1 parent f74dfd1 commit b4650d9
Show file tree
Hide file tree
Showing 29 changed files with 1,056 additions and 366 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module.exports = {
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:prettier/recommended",
"prettier",
],
parser: "@typescript-eslint/parser",
parserOptions: {
Expand Down
248 changes: 196 additions & 52 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ <h1>Hello, Snaps!</h1>
<li>Please note that:</li>
<ul>
<li>
The <code>snap.manifest.json</code> and <code>package.json</code> must be located in located in the server root directory..
</li>
<li>
The Snap bundle must be hosted at the location specified by the <code>location</code> field of <code>snap.manifest.json</code>.
If you want to generate a VP you need a VC ID. To get it, you need a VC saved in MetaMask state (use Save VC). Then you need to retrieve id of VC by using Get VCS button and searching for "key" property in the console.
</li>
</ul>
</ul>
Expand All @@ -26,37 +23,117 @@ <h1>Hello, Snaps!</h1>

<button class="connect">Connect</button>
<button class="sendHello">Send Hello</button>
<button class="init">Get VP</button>
<button class="isInit">Get VP challenge</button>
<button class="saveVC">Save VC</button>
<button class="init">Init</button>
<br>
<hr>
<button class="getVCs">Get VCS</button>
<button class="getVCAddr">Get VC Addr</button>
<br>
<hr>
<textarea id="VC" rows="30" cols="50"></textarea>
<br>
<button class="saveVC">Save VC</button>
<br>
<hr>
<input type="text" id="vc_id" placeholder="VC ID" />
With challenge
<input type="checkbox" id="withChallenge" placeholder="false" />
<button id="getVP" class="getVP">Get VP</button>
<br>
<hr>
<select id="didMethod">
<option value="did:ethr">did:ethr</option>
<option value="did:key">did:key</option>
<option value="did:github">did:key</option>
</select>
<button class="switchMethod">Switch current method</button>
<br>
<hr>
<button class="getCurrentDid">Get current DID</button>
<button class="getCurrentMethod">Get current method</button>
<button class="getAvailableMethods">Get current network</button>
<br>
<hr>
<button class="togglePopups">TogglePopups</button>
<br>
<hr>
<input type="text" id="infura_token" placeholder="Infura token" />
<button class="changeInfuraToken">ChangeInfuraToken</button>
</body>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>


const snapId = `local:${window.location.href}`;
console.log(snapId)

const placeholderVC = {
"credentialSchema": {
"id": "https://beta.api.schemas.serto.id/v1/public/program-completion-certificate/1.0/json-schema.json",
"type": "JsonSchemaValidator2018"
},
"credentialSubject": {
"accomplishmentType": "Developer Certificate",
"learnerName": "a",
"achievement": "Certified Solidity Developer 2",
"courseProvider": "UM FERI",
"id": "did:ethr:rinkeby:0x6A24687621cDD1C77Bb6aCbBEE910d0C517eB443"
},
"issuer": {
"id": "did:ethr:rinkeby:0x0241abd662da06d0af2f0152a80bc037f65a7f901160cfe1eb35ef3f0c532a2a4d"
},
"type": [
"VerifiableCredential",
"ProgramCompletionCertificate"
],
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://beta.api.schemas.serto.id/v1/public/program-completion-certificate/1.0/ld-context.json"
],
"issuanceDate": "2022-06-13T12:08:10.000Z",
"proof": {
"type": "JwtProof2020",
"jwt": "eyJhbGciOiJFUzI1NksiLCJ0eXAiOiJKV1QifQ.eyJ2YyI6eyJAY29udGV4dCI6WyJodHRwczovL3d3dy53My5vcmcvMjAxOC9jcmVkZW50aWFscy92MSIsImh0dHBzOi8vYmV0YS5hcGkuc2NoZW1hcy5zZXJ0by5pZC92MS9wdWJsaWMvcHJvZ3JhbS1jb21wbGV0aW9uLWNlcnRpZmljYXRlLzEuMC9sZC1jb250ZXh0Lmpzb24iXSwidHlwZSI6WyJWZXJpZmlhYmxlQ3JlZGVudGlhbCIsIlByb2dyYW1Db21wbGV0aW9uQ2VydGlmaWNhdGUiXSwiY3JlZGVudGlhbFN1YmplY3QiOnsiYWNjb21wbGlzaG1lbnRUeXBlIjoiRGV2ZWxvcGVyIENlcnRpZmljYXRlIiwibmFtZSI6ImEiLCJhY2hpZXZlbWVudCI6IkNlcnRpZmllZCBTb2xpZGl0eSBEZXZlbG9wZXIgMiIsImNvdXJzZVByb3ZpZGVyIjoiVU0gRkVSSSJ9LCJjcmVkZW50aWFsU2NoZW1hIjp7ImlkIjoiaHR0cHM6Ly9iZXRhLmFwaS5zY2hlbWFzLnNlcnRvLmlkL3YxL3B1YmxpYy9wcm9ncmFtLWNvbXBsZXRpb24tY2VydGlmaWNhdGUvMS4wL2pzb24tc2NoZW1hLmpzb24iLCJ0eXBlIjoiSnNvblNjaGVtYVZhbGlkYXRvcjIwMTgifX0sInN1YiI6ImRpZDpldGhyOnJpbmtlYnk6MHg2QTI0Njg3NjIxY0REMUM3N0JiNmFDYkJFRTkxMGQwQzUxN2VCNDQzIiwibmJmIjoxNjUyNDQzNjkwLCJpc3MiOiJkaWQ6ZXRocjpyaW5rZWJ5OjB4MDI0MWFiZDY2MmRhMDZkMGFmMmYwMTUyYTgwYmMwMzdmNjVhN2Y5MDExNjBjZmUxZWIzNWVmM2YwYzUzMmEyYTRkIn0.Z4q7kn4vKdFI5QfAyQmqtWa0icAv91HqxSEwn-AMr4_bY3vfD_WeD3W9hgqf9tsUJPx2ru5gY3tLpAx04nk0RQ"
}
}

const connectButton = document.querySelector('button.connect')
const sendButton = document.querySelector('button.sendHello')
const saveBtn = document.querySelector('button.saveVC')
const initButton = document.querySelector('button.init')

const getVCBtn = document.querySelector('button.getVCs')
const getAddrBtn = document.querySelector('button.getVCAddr')
const initializeBtn = document.querySelector('button.init')
const isInitializedBtn = document.querySelector('button.isInit')

const saveBtn = document.querySelector('button.saveVC')

const getVPBtn = document.querySelector('button.getVP')

const switchMethodBtn = document.querySelector('button.switchMethod')

const getDIDBtn = document.querySelector('button.getCurrentDid')
const getMethodBtn = document.querySelector('button.getCurrentMethod')
const getAvailableMethodsBtn = document.querySelector('button.getAvailableMethods')

const popups = document.querySelector('button.togglePopups')
const infura = document.querySelector('button.changeInfuraToken')

$(document).ready(function () {
$('textarea#VC').text(JSON.stringify(placeholderVC));
});

connectButton.addEventListener('click', connect)
sendButton.addEventListener('click', send)
saveBtn.addEventListener('click', saveVC)
getAddrBtn.addEventListener('click', getVCAddr)
initButton.addEventListener('click', init)

getVCBtn.addEventListener('click', getVCs)
initializeBtn.addEventListener('click', getVp)
isInitializedBtn.addEventListener('click', isInit)

saveBtn.addEventListener('click', saveVC)

getVPBtn.addEventListener('click', getVp)

switchMethodBtn.addEventListener('click', switchMethod)

getDIDBtn.addEventListener('click', getDID)
getMethodBtn.addEventListener('click', getMethod)
getAvailableMethodsBtn.addEventListener('click', getAvailableMethods)

popups.addEventListener('click', togglePopups)
infura.addEventListener('click', changeInfuraToken)

Expand All @@ -71,6 +148,84 @@ <h1>Hello, Snaps!</h1>
})
}

async function init() {
try {
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'init'
}]
})
console.log("INIT: ",response)
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}

async function getDID() {
try {
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'getDID'
}]
})
console.log("DID: ",response)
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}

async function getMethod() {
try {
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'getMethod'
}]
})
console.log("Method: ",response)
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}

async function getAvailableMethods() {
try {
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'getAvailableMethods'
}]
})
console.log("AvailableMethods: ",response)
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}

async function switchMethod() {
try {
const method = document.getElementById("didMethod").value
console.log("selected method", method)
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'switchMethod',
params: {didMethod: method}
}]
})
console.log("switch: ",response)
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}

// here we call the snap's "hello" method
async function send () {
try {
Expand All @@ -88,40 +243,13 @@ <h1>Hello, Snaps!</h1>

async function saveVC () {
try {
const vc = JSON.parse(document.getElementById('VC').value)
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'saveVC',
params: {
"verifiableCredential": {
"credentialSchema": {
"id": "https://beta.api.schemas.serto.id/v1/public/program-completion-certificate/1.0/json-schema.json",
"type": "JsonSchemaValidator2018"
},
"credentialSubject": {
"accomplishmentType": "Developer Certificate",
"learnerName": "a",
"achievement": "Certified Solidity Developer 2",
"courseProvider": "UM FERI",
"id": "did:ethr:rinkeby:0x6A24687621cDD1C77Bb6aCbBEE910d0C517eB443"
},
"issuer": {
"id": "did:ethr:rinkeby:0x0241abd662da06d0af2f0152a80bc037f65a7f901160cfe1eb35ef3f0c532a2a4d"
},
"type": [
"VerifiableCredential",
"ProgramCompletionCertificate"
],
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://beta.api.schemas.serto.id/v1/public/program-completion-certificate/1.0/ld-context.json"
],
"issuanceDate": "2022-06-13T12:08:10.000Z",
"proof": {
"type": "JwtProof2020",
"jwt": "eyJhbGciOiJFUzI1NksiLCJ0eXAiOiJKV1QifQ.eyJ2YyI6eyJAY29udGV4dCI6WyJodHRwczovL3d3dy53My5vcmcvMjAx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method: 'wallet_invokeSnap',
params: [snapId, {
method: 'getVCs',
//params: {querry: {}}
params: {querry: {issuer: {id: 'did:ethr:rinkeby:0x0241abd662da06d0af2f0152a80bc037f65a7f901160cfe1eb35ef3f0c532a2a4d'}, issuanceDate: "2022-05-13T12:08:10.000Z"}}
//params: {query: {}}
params: {query: {}}
}]
})
console.log(response)
Expand All @@ -162,14 +290,29 @@ <h1>Hello, Snaps!</h1>

async function getVp () {
try {
const vc_id = document.getElementById('vc_id').value.toString()
const checkBox = document.getElementById('withChallenge').checked
console.log("VC ID", vc_id, checkBox)
if(!checkBox){
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'getVP',
params: [0]
params: {vc_id: vc_id}
}]
})
console.log(response)
}
else{
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'getVP',
params: {vc_id: vc_id, challenge: "61dcecc0-c9df-44e9-8386-473ecb004ecd"}
}]
})
console.log(response)
}
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
Expand All @@ -182,7 +325,7 @@ <h1>Hello, Snaps!</h1>
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'getVP',
params: [0, "did:ethr:rinkeby:0x0241abd662da06d0af2f0152a80bc037f65a7f901160cfe1eb35ef3f0c532a2a4d", "61dcecc0-c9df-44e9-8386-473ecb004ecd"]
params: [0, vc_id, "61dcecc0-c9df-44e9-8386-473ecb004ecd"]
}]
})
console.log(response)
Expand All @@ -194,11 +337,12 @@ <h1>Hello, Snaps!</h1>

async function changeInfuraToken() {
try {
const infuraToken = document.getElementById('infura_token').value.toString()
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'changeInfuraToken',
params: ["newToken"]
params: {infuraToken: infuraToken}
}]
})
console.log(response)
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@
"lint:eslint": "eslint . --ext .js,.ts,.tsx",
"lint:prettier": "prettier . --check",
"lint:tsc": "tsc --noEmit --incremental false",
"prepare": "yarn run clean && husky install"
"prepare": "yarn run clean && husky install",
"postinstall": "patch-package"
},
"dependencies": {
"@babel/runtime": "^7.17.2",
"@blockchain-lab-um/ssi-snap-types": "^1.0.2",
"@blockchain-lab-um/ssi-snap-types": "^1.0.7",
"@blockchain-lab-um/veramo-vc-manager": "^1.0.5",
"@veramo/core": "^3.1.6-next.146",
"@veramo/credential-eip712": "^3.1.6-next.146",
Expand All @@ -69,12 +70,15 @@
"@veramo/selective-disclosure": "^3.1.6-next.146",
"did-jwt-vc": "^2.1.9",
"did-resolver": "^3.2.0",
"ethers": "^5.6.5",
"ethers": "^5.6.9",
"ethr-did": "^2.2.2",
"ethr-did-resolver": "^5.0.4",
"js-base64": "^3.7.2",
"multibase": "^4.0.6",
"multicodec": "^3.2.1",
"patch-package": "^6.4.7",
"postinstall-postinstall": "^2.1.0",
"secp256k1": "^4.0.3",
"uuid": "^8.3.2",
"web-did-resolver": "^2.0.14"
},
Expand Down
2 changes: 1 addition & 1 deletion snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/blockchain-lab-um/ssi-snap.git"
},
"source": {
"shasum": "EjOmxmSY430+Eaf6lcpU5HnCmw1ye/AbSF75TgXPLFA=",
"shasum": "jyPDhv7XlRVlNo5w/Y1bOkzvlF82kDkVu/n36fEchAA=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
1 change: 1 addition & 0 deletions src/did/did-methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const availableMethods = ["did:ethr", "did:key"] as const;
Loading

0 comments on commit b4650d9

Please sign in to comment.