Created
May 24, 2023 17:49
-
-
Save AdamJLemmon/1a996a0a1d66b24877b8f365e840dac5 to your computer and use it in GitHub Desktop.
MATTR VII Quick Start
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
const axios = require("axios"); | |
const registration = { | |
"audience": "https://vii.mattr.global", | |
"url": "https://auth.mattr.global/oauth/token", | |
"tenant_subdomain": "YOUR_TENANT_SUBDOMAIN", | |
"client_id": "YOUR_CLIENT_ID", | |
"client_secret": "YOUR_CLIENT_SECRET" | |
}; | |
const sendRequest = async ({ | |
method, | |
url, | |
headers, | |
data | |
}) => { | |
try { | |
const response = await axios({ | |
method, | |
url, | |
headers, | |
data | |
}); | |
return response.data; | |
} catch (error) { | |
console.log(error.response.data); | |
} | |
}; | |
const getAccessToken = async () => { | |
const authz = await sendRequest({ | |
method: "post", | |
url: "https://auth.mattr.global/oauth/token", | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
data: { | |
"client_id": registration.client_id, | |
"client_secret": registration.client_secret, | |
"audience": "https://vii.mattr.global", | |
"grant_type": "client_credentials" | |
} | |
}); | |
const access_token = authz.access_token; | |
return access_token; | |
}; | |
const createDID = async (access_token) => { | |
const did = await sendRequest({ | |
method: "post", | |
url: `https://${registration.tenant_subdomain}.vii.mattr.global/v1/dids`, | |
headers: { | |
"Content-Type": "application/json", | |
"Authorization": `Bearer ${access_token}` | |
}, | |
data: { | |
method: "key" | |
} | |
}); | |
return did; | |
}; | |
const getDIDs = async (access_token) => { | |
const dids = await sendRequest({ | |
method: "get", | |
url: `https://${registration.tenant_subdomain}.vii.mattr.global/v1/dids`, | |
headers: { | |
"Content-Type": "application/json", | |
"Authorization": `Bearer ${access_token}` | |
} | |
}); | |
return dids; | |
}; | |
const signCredential = async (access_token, issuerDID) => { | |
const credential = await sendRequest({ | |
url: `https://${registration.tenant_subdomain}.vii.mattr.global/v2/credentials/web-semantic/sign`, | |
method: "post", | |
headers: { | |
"Content-Type": "application/json", | |
"Authorization": `Bearer ${access_token}` | |
}, | |
data: { | |
"payload": { | |
"@context": [ | |
"https://schema.org" | |
], | |
"name": "Course credential", | |
"description": "Course credential description", | |
"type": [ | |
"CourseCredential" | |
], | |
"credentialSubject": { | |
"id": "did:key:z6MkfxQU7dy8eKxyHpG267FV23agZQu9zmokd8BprepfHALi", | |
"givenName": "Chris", | |
"familyName": "Shin", | |
"educationalCredentialAwarded": "Certificate Name" | |
}, | |
"issuer": { | |
"id": issuerDID, | |
"name": "tenant" | |
}, | |
"expirationDate": "2024-02-07T06:44:28.952Z" | |
}, | |
"tag": "identifier123", | |
"persist": false, | |
"revocable": false, | |
} | |
}); | |
return credential; | |
}; | |
const viiQuickStart = async () => { | |
const access_token = await getAccessToken(); | |
if (access_token) { | |
const did = await createDID(access_token); | |
console.log({ | |
did | |
}); | |
const dids = await getDIDs(access_token); | |
console.log({ | |
dids: `${dids.data.length} total DIDs.` | |
}); | |
const issuerDID = did.did; | |
const credential = await signCredential(access_token, issuerDID); | |
return credential | |
} | |
}; | |
// Create a DID and sign a credential | |
viiQuickStart().then((credential) => { | |
console.log(JSON.stringify(credential, null, 3)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment