-
Notifications
You must be signed in to change notification settings - Fork 8
/
priv_verif.example.ts
48 lines (42 loc) · 2.28 KB
/
priv_verif.example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) 2023 Cloudflare, Inc.
// Licensed under the Apache-2.0 license found in the LICENSE file or at https://opensource.org/licenses/Apache-2.0
import { TOKEN_TYPES, TokenChallenge, privateVerif } from '../src/index.js';
const { Client, Issuer, keyGen } = privateVerif;
export async function privateVerifiableTokens(): Promise<void> {
// Protocol Setup
//
// [ Everybody ] agree to use Private-Verifiable Tokens.
const tokenType = TOKEN_TYPES.VOPRF.value;
// [ Issuer ] creates a key pair.
const keys = await keyGen();
const issuer = new Issuer('issuer.com', keys.privateKey, keys.publicKey);
const pkIssuer = issuer.publicKey;
// [ Client ] creates a state.
const client = new Client();
// Online Protocol
//
// +--------+ +--------+ +----------+ +--------+
// | Origin | | Client | | Attester | | Issuer |
// +---+----+ +---+----+ +----+-----+ +---+----+
// | | | |
// |<----- Request ------+ | |
const redemptionContext = crypto.getRandomValues(new Uint8Array(32));
const originInfo = ['origin.example.com', 'origin2.example.com'];
const tokChl = new TokenChallenge(tokenType, issuer.name, redemptionContext, originInfo);
// +-- TokenChallenge -->| | |
// | |<== Attestation ==>| |
// | | | |
const tokReq = await client.createTokenRequest(tokChl, pkIssuer);
// | +--------- TokenRequest ------->|
// | | | |
const tokRes = await issuer.issue(tokReq);
// | |<-------- TokenResponse -------+
// | | | |
const token = await client.finalize(tokRes);
// |<-- Request+Token ---+ | |
// | | | |
const isValid = await issuer.verify(token);
console.log('Private-Verifiable tokens');
console.log(` Suite: ${TOKEN_TYPES.VOPRF.name}`);
console.log(` Valid token: ${isValid}`);
}