forked from unicitynetwork/state-transition-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpubkey_challenge.js
More file actions
35 lines (29 loc) · 1.21 KB
/
Copy pathpubkey_challenge.js
File metadata and controls
35 lines (29 loc) · 1.21 KB
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
"use strict";
const { UnicityProvider, verifyInclusionProofs, calculateRequestId } = require('@unicitylabs/shared/provider/UnicityProvider.js');
const { calculateStateHash } = require("@unicitylabs/shared");
const { hash } = require("@unicitylabs/shared/hasher/sha256hasher.js").SHA256Hasher;
const { OK, INP_MISMATCH } = require('@unicitylabs/shared');
class ChallengePubkey {
constructor(tokenClass, tokenId, sign_alg, hash_alg, pubkey, nonce) {
this.tokenClass = tokenClass;
this.tokenId = tokenId;
this.sign_alg = sign_alg;
this.hash_alg = hash_alg;
this.pubkey = pubkey;
this.nonce = nonce;
}
verify(input, stateHash){
const requestId = calculateRequestId(hash, this.pubkey, stateHash);
const status = verifyInclusionProofs(input.path, requestId);
if(status != OK)return status;
const l = input.path.length-1;
if((input.path[l].authenticator.pubkey != this.pubkey)||
(input.path[l].authenticator.state != stateHash))return INP_MISMATCH;
return OK;
}
getHexDigest(){
return calculateStateHash({token_class_id: this.tokenClass, token_id: this.tokenId, sign_alg: this.sign_alg,
hash_alg: this.hash_alg, pubkey: this.pubkey, nonce: this.nonce});
}
}
module.exports = { ChallengePubkey }