Authorization Service
The authorization service provides cryptographic message signing and validation using Ed25519 signatures. It's automatically available on the client and adapts based on whether a wallet is configured.
Behavior
- With Wallet: When a wallet is set, the authorization service provides all methods including
generate,validate,generateHeaders, andvalidateHeaders. - Without Wallet: When no wallet is set, the authorization service only provides
validateandvalidateHeadersmethods (read-only validation).
Methods
Generate Message Signature
ts
generate(message: string | Uint8Array, options?: GenerateOptions): Promise<string>1
Generating message signatures (requires wallet).
Validate Message Signature
ts
validate(
message: string | Uint8Array,
signature: string | Uint8Array,
publicKey?: string | Uint8Array
): Promise<boolean>1
2
3
4
5
2
3
4
5
Validate message signatures.
Generate HTTP Header Signatures
ts
generateHeaders(
method: string,
path: string,
body?: string | Uint8Array,
options?: GenerateHeaderOptions
): Promise<Headers>1
2
3
4
5
6
2
3
4
5
6
Signing HTTP headers requires wallet.
Validate HTTP Header Signatures
ts
validateHeaders(headers: Headers | Record<string, string>): Promise<boolean>1
Validate HTTP header signatures.
Examples
ts
// Set wallet first to enable signing
client.wallet = myWallet;
// Generate message signature
const messageToSign = 'Hello, Nosana!';
const messageSignature: string = await client.authorization.generate(messageToSign);
console.log('Message signature:', messageSignature);
// Validate message signature (requires validationString with message+signature, and publicKey)
// Note: validationString typically contains both message and signature separated
const validationString = `${messageToSign}:${messageSignature}`; // Format may vary
// Get publicKey from wallet (in real usage, extract from wallet's public key)
const publicKey = new Uint8Array(32); // Placeholder - extract actual public key from wallet
const isValid: boolean = client.authorization.validate(validationString, publicKey);
console.log('Message is valid:', isValid);
// Generate HTTP header signatures for API requests
// generateHeaders takes a message string and optional options
const requestMessage = 'POST /api/jobs';
const headers: Headers = await client.authorization.generateHeaders(requestMessage, {
expiry: Date.now() + 3600000, // 1 hour from now
key: 'authorization',
includeTime: true,
separator: ':',
});
// Use headers in HTTP request
fetch('https://api.nosana.com/api/jobs', {
method: 'POST',
headers: headers,
body: JSON.stringify({ data: 'example' }),
});
// Validate incoming HTTP header signatures (requires headers as IncomingHttpHeaders and publicKey)
const publicKeyForValidation = new Uint8Array(32); // Placeholder - extract actual public key
// Convert Headers to IncomingHttpHeaders format (Record<string, string | string[] | undefined>)
const isValidRequest: boolean = client.authorization.validateHeaders(requestHeaders, publicKeyForValidation);
if (!isValidRequest) {
throw new Error('Invalid authorization');
}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
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
Use Cases
- API Authentication: Sign requests to Nosana APIs using message signatures
- Message Verification: Verify message signatures from other parties
- Secure Communication: Establish authenticated communication channels
- Request Authorization: Validate incoming API requests