forked from StellarLend/Stellarlend-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-stellar-auth.ts
More file actions
59 lines (48 loc) · 1.48 KB
/
Copy pathtest-stellar-auth.ts
File metadata and controls
59 lines (48 loc) · 1.48 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
import { Keypair, Networks, TransactionBuilder, Utils } from '@stellar/stellar-sdk';
import * as jose from 'jose';
async function test() {
console.log('Testing Stellar SDK and Jose');
const serverKeypair = Keypair.random();
const clientKeypair = Keypair.random();
console.log('Server Public Key:', serverKeypair.publicKey());
console.log('Client Public Key:', clientKeypair.publicKey());
try {
const challengeTx = Utils.buildChallengeTx(
serverKeypair,
clientKeypair.publicKey(),
'testnet',
300,
'Stellarlend'
);
console.log('Challenge TX built');
// Sign with client
const tx = Utils.readChallengeTx(
challengeTx,
serverKeypair.publicKey(),
'testnet',
'Stellarlend'
);
tx.sign(clientKeypair);
console.log('Challenge TX signed');
const valid = Utils.verifyChallengeTxThreshold(
tx.toXDR(),
serverKeypair.publicKey(),
'testnet',
'Stellarlend',
serverKeypair.publicKey()
);
console.log('Verify Result:', valid);
const secret = new TextEncoder().encode('my-secret');
const jwt = await new jose.SignJWT({ 'urn:example:claim': true })
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setIssuer('urn:example:issuer')
.setAudience('urn:example:audience')
.setExpirationTime('2h')
.sign(secret);
console.log('JWT:', jwt);
} catch (e) {
console.error('Error:', e);
}
}
test();