forked from Vero-protocol/vero-core-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallet-connector.ts
More file actions
69 lines (62 loc) · 1.6 KB
/
Copy pathwallet-connector.ts
File metadata and controls
69 lines (62 loc) · 1.6 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {
Keypair,
WebAuth,
} from "@stellar/stellar-sdk";
export interface ChallengeOptions {
serverKeypair: Keypair;
clientAddress: string;
networkPassphrase: string;
domain: string;
timeout?: number;
}
export class WalletConnector {
/**
* Generates a SEP-10 challenge transaction XDR.
*/
static createChallenge(options: ChallengeOptions): string {
const { serverKeypair, clientAddress, networkPassphrase, domain, timeout = 300 } = options;
return WebAuth.buildChallengeTx(
serverKeypair,
clientAddress,
domain,
timeout,
networkPassphrase,
domain // webAuthDomain defaults to homeDomain if not specified
);
}
/**
* Verifies a signed SEP-10 challenge transaction.
*/
static verifyResponse(
xdr: string,
serverAddress: string,
networkPassphrase: string,
domain: string
): string {
const { clientAccountID } = WebAuth.readChallengeTx(
xdr,
serverAddress,
networkPassphrase,
domain,
domain // webAuthDomain
);
let signersFound: string[];
try {
signersFound = WebAuth.verifyChallengeTxThreshold(
xdr,
serverAddress,
networkPassphrase,
1, // threshold
[{ key: clientAccountID, weight: 1, type: "ed25519_public_key" }],
domain,
domain // webAuthDomain
);
} catch (err) {
throw new Error("Invalid signature: client signature missing or incorrect");
}
if (signersFound.length === 0) {
throw new Error("Invalid signature: client signature missing or incorrect");
}
return clientAccountID;
}
}