Include a way to register other test users #4

Merged
anomalist merged 3 commits from test-registration into master 2 years ago
  1. 34
      README.md
  2. 4
      src/libs/crypto/index.ts
  3. 60
      src/test/index.ts
  4. 4
      src/views/AccountViewView.vue

34
README.md

@ -20,6 +20,40 @@ npm run build
npm run lint
```
### Clear data & restart
Clear cache for localhost, then go to http://localhost:8080/start (because it'll regenerate if you start on the `/account` page).
### Test key contents
See [this page](openssl_signing_console.rst)
### Register new user on test server
New users require registration. This can be done with a claim payload like this by an existing user:
```
const vcClaim = {
"@context": "https://schema.org",
"@type": "RegisterAction",
agent: { did: identity0.did },
object: SERVICE_ID,
participant: { did: newIdentity.did },
};
```
On the test server, the user starting 0x000 has rights to register others, and the keys for that test User #0 are found in the codebase. You can invoke registration by User #0 on the `/account` page:
* Edit the `src/views/AccountViewView.vue` file and uncomment the lines referring to "test".
* Use the [Vue Devtools browser extension](https://devtools.vuejs.org/) and type this into the console: `$vm.ctx.testRegisterUser()`
### Create keys with alternate tools
See [this page](openssl_signing_console.rst)
### Customize configuration
See [Configuration Reference](https://cli.vuejs.org/config/).

4
src/libs/crypto/index.ts

@ -122,7 +122,7 @@ export const accessToken = async (identifier: IIdentifier) => {
: privateKeyHex;
const privateKeyBytes = u8a.fromString(input.toLowerCase(), "base16");
const signer = didJwt.ES256KSigner(privateKeyBytes, true);
const signer = didJwt.SimpleSigner(privateKeyHex);
const nowEpoch = Math.floor(Date.now() / 1000);
const endEpoch = nowEpoch + 60; // add one minute
@ -143,7 +143,7 @@ export const sign = async (privateKeyHex: string) => {
: privateKeyHex;
const privateKeyBytes = u8a.fromString(input.toLowerCase(), "base16");
const signer = didJwt.ES256KSigner(privateKeyBytes, true);
const signer = didJwt.SimpleSigner(privateKeyHex);
return signer;
};

60
src/test/index.ts

@ -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);
}

4
src/views/AccountViewView.vue

@ -178,6 +178,7 @@ import { createIdentifier, deriveAddress, newIdentifier } from "../libs/crypto";
import { db } from "../db";
import { useAppStore } from "@/store/app";
import { ref } from "vue";
//import { testServerRegisterUser } from "../test";
@Options({
components: {},
@ -191,6 +192,9 @@ export default class AccountViewView extends Vue {
source = ref("Hello");
copy = useClipboard().copy;
// This registers current user in vue plugin with: $vm.ctx.testRegisterUser()
//testRegisterUser = testServerRegisterUser;
// 'created' hook runs when the Vue instance is first created
async created() {
const appCondition = useAppStore().condition;

Loading…
Cancel
Save