Include a way to register other test users #4
Merged
anomalist
merged 3 commits from test-registration
into master
2 years ago
4 changed files with 100 additions and 2 deletions
@ -0,0 +1,60 @@ |
|||||
|
import axios from "axios"; |
||||
|
import * as didJwt from "did-jwt"; |
||||
|
import { AppString } from "@/constants/app"; |
||||
|
import { db } from "../db"; |
||||
|
import { SERVICE_ID } from "../libs/veramo/setup"; |
||||
|
import { deriveAddress, newIdentifier } from "../libs/crypto"; |
||||
|
|
||||
|
export async function testServerRegisterUser() { |
||||
|
const testUser0Mnem = |
||||
|
"seminar accuse mystery assist delay law thing deal image undo guard initial shallow wrestle list fragile borrow velvet tomorrow awake explain test offer control"; |
||||
|
|
||||
|
const [addr, privateHex, publicHex, deriPath] = deriveAddress(testUser0Mnem); |
||||
|
|
||||
|
const identity0 = newIdentifier(addr, publicHex, privateHex, deriPath); |
||||
|
|
||||
|
await db.open(); |
||||
|
const accounts = await db.accounts.toArray(); |
||||
|
const thisIdentity = JSON.parse(accounts[0].identity); |
||||
|
|
||||
|
// Make a claim
|
||||
|
const vcClaim = { |
||||
|
"@context": "https://schema.org", |
||||
|
"@type": "RegisterAction", |
||||
|
agent: { did: identity0.did }, |
||||
|
object: SERVICE_ID, |
||||
|
participant: { did: thisIdentity.did }, |
||||
|
}; |
||||
|
// Make a payload for the claim
|
||||
|
const vcPayload = { |
||||
|
sub: "RegisterAction", |
||||
|
vc: { |
||||
|
"@context": ["https://www.w3.org/2018/credentials/v1"], |
||||
|
type: ["VerifiableCredential"], |
||||
|
credentialSubject: vcClaim, |
||||
|
}, |
||||
|
}; |
||||
|
// create a signature using private key of identity
|
||||
|
// eslint-disable-next-line
|
||||
|
const privateKeyHex: string = identity0.keys[0].privateKeyHex!; |
||||
|
const signer = await didJwt.SimpleSigner(privateKeyHex); |
||||
|
const alg = undefined; |
||||
|
// create a JWT for the request
|
||||
|
const vcJwt: string = await didJwt.createJWT(vcPayload, { |
||||
|
alg: alg, |
||||
|
issuer: identity0.did, |
||||
|
signer: signer, |
||||
|
}); |
||||
|
|
||||
|
// Make the xhr request payload
|
||||
|
|
||||
|
const payload = JSON.stringify({ jwtEncoded: vcJwt }); |
||||
|
const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER; |
||||
|
const url = endorserApiServer + "/api/claim"; |
||||
|
const headers = { |
||||
|
"Content-Type": "application/json", |
||||
|
}; |
||||
|
|
||||
|
const resp = await axios.post(url, payload, { headers }); |
||||
|
console.log("Result:", resp); |
||||
|
} |
Loading…
Reference in new issue