@bananalink-sdk/protocol 1.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +604 -0
- package/dist/chunk-32OWUOZ3.js +308 -0
- package/dist/chunk-32OWUOZ3.js.map +1 -0
- package/dist/chunk-65HNHRJK.cjs +123 -0
- package/dist/chunk-65HNHRJK.cjs.map +1 -0
- package/dist/chunk-7KYDLL3B.js +480 -0
- package/dist/chunk-7KYDLL3B.js.map +1 -0
- package/dist/chunk-A6FLEJ7R.cjs +62 -0
- package/dist/chunk-A6FLEJ7R.cjs.map +1 -0
- package/dist/chunk-CUJK7ZTS.js +217 -0
- package/dist/chunk-CUJK7ZTS.js.map +1 -0
- package/dist/chunk-GI3BUPIH.cjs +236 -0
- package/dist/chunk-GI3BUPIH.cjs.map +1 -0
- package/dist/chunk-JXHV66Q4.js +106 -0
- package/dist/chunk-JXHV66Q4.js.map +1 -0
- package/dist/chunk-KNGZKGRS.cjs +552 -0
- package/dist/chunk-KNGZKGRS.cjs.map +1 -0
- package/dist/chunk-LELPCIE7.js +840 -0
- package/dist/chunk-LELPCIE7.js.map +1 -0
- package/dist/chunk-MCZG7QEM.cjs +310 -0
- package/dist/chunk-MCZG7QEM.cjs.map +1 -0
- package/dist/chunk-TCVKC227.js +56 -0
- package/dist/chunk-TCVKC227.js.map +1 -0
- package/dist/chunk-VXLUSU5B.cjs +856 -0
- package/dist/chunk-VXLUSU5B.cjs.map +1 -0
- package/dist/chunk-WCQVDF3K.js +12 -0
- package/dist/chunk-WCQVDF3K.js.map +1 -0
- package/dist/chunk-WGEGR3DF.cjs +15 -0
- package/dist/chunk-WGEGR3DF.cjs.map +1 -0
- package/dist/client-session-claim-3QF3noOr.d.ts +197 -0
- package/dist/client-session-claim-C4lUik3b.d.cts +197 -0
- package/dist/core-DMhuNfoz.d.cts +62 -0
- package/dist/core-DMhuNfoz.d.ts +62 -0
- package/dist/crypto/providers/noble-provider.cjs +14 -0
- package/dist/crypto/providers/noble-provider.cjs.map +1 -0
- package/dist/crypto/providers/noble-provider.d.cts +30 -0
- package/dist/crypto/providers/noble-provider.d.ts +30 -0
- package/dist/crypto/providers/noble-provider.js +5 -0
- package/dist/crypto/providers/noble-provider.js.map +1 -0
- package/dist/crypto/providers/node-provider.cjs +308 -0
- package/dist/crypto/providers/node-provider.cjs.map +1 -0
- package/dist/crypto/providers/node-provider.d.cts +32 -0
- package/dist/crypto/providers/node-provider.d.ts +32 -0
- package/dist/crypto/providers/node-provider.js +306 -0
- package/dist/crypto/providers/node-provider.js.map +1 -0
- package/dist/crypto/providers/quickcrypto-provider.cjs +339 -0
- package/dist/crypto/providers/quickcrypto-provider.cjs.map +1 -0
- package/dist/crypto/providers/quickcrypto-provider.d.cts +34 -0
- package/dist/crypto/providers/quickcrypto-provider.d.ts +34 -0
- package/dist/crypto/providers/quickcrypto-provider.js +337 -0
- package/dist/crypto/providers/quickcrypto-provider.js.map +1 -0
- package/dist/crypto/providers/webcrypto-provider.cjs +310 -0
- package/dist/crypto/providers/webcrypto-provider.cjs.map +1 -0
- package/dist/crypto/providers/webcrypto-provider.d.cts +30 -0
- package/dist/crypto/providers/webcrypto-provider.d.ts +30 -0
- package/dist/crypto/providers/webcrypto-provider.js +308 -0
- package/dist/crypto/providers/webcrypto-provider.js.map +1 -0
- package/dist/crypto-BUS06Qz-.d.cts +40 -0
- package/dist/crypto-BUS06Qz-.d.ts +40 -0
- package/dist/crypto-export.cjs +790 -0
- package/dist/crypto-export.cjs.map +1 -0
- package/dist/crypto-export.d.cts +257 -0
- package/dist/crypto-export.d.ts +257 -0
- package/dist/crypto-export.js +709 -0
- package/dist/crypto-export.js.map +1 -0
- package/dist/crypto-provider-deYoVIxi.d.cts +36 -0
- package/dist/crypto-provider-deYoVIxi.d.ts +36 -0
- package/dist/index.cjs +615 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +379 -0
- package/dist/index.d.ts +379 -0
- package/dist/index.js +504 -0
- package/dist/index.js.map +1 -0
- package/dist/schemas-export.cjs +294 -0
- package/dist/schemas-export.cjs.map +1 -0
- package/dist/schemas-export.d.cts +1598 -0
- package/dist/schemas-export.d.ts +1598 -0
- package/dist/schemas-export.js +5 -0
- package/dist/schemas-export.js.map +1 -0
- package/dist/siwe-export.cjs +237 -0
- package/dist/siwe-export.cjs.map +1 -0
- package/dist/siwe-export.d.cts +27 -0
- package/dist/siwe-export.d.ts +27 -0
- package/dist/siwe-export.js +228 -0
- package/dist/siwe-export.js.map +1 -0
- package/dist/testing.cjs +54 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +20 -0
- package/dist/testing.d.ts +20 -0
- package/dist/testing.js +51 -0
- package/dist/testing.js.map +1 -0
- package/dist/validation-export.cjs +359 -0
- package/dist/validation-export.cjs.map +1 -0
- package/dist/validation-export.d.cts +3 -0
- package/dist/validation-export.d.ts +3 -0
- package/dist/validation-export.js +6 -0
- package/dist/validation-export.js.map +1 -0
- package/dist/validators-export.cjs +73 -0
- package/dist/validators-export.cjs.map +1 -0
- package/dist/validators-export.d.cts +37 -0
- package/dist/validators-export.d.ts +37 -0
- package/dist/validators-export.js +4 -0
- package/dist/validators-export.js.map +1 -0
- package/package.json +140 -0
- package/src/constants/index.ts +205 -0
- package/src/crypto/context.ts +228 -0
- package/src/crypto/diagnostics.ts +772 -0
- package/src/crypto/errors.ts +114 -0
- package/src/crypto/index.ts +89 -0
- package/src/crypto/payload-handler.ts +102 -0
- package/src/crypto/providers/compliance-provider.ts +579 -0
- package/src/crypto/providers/factory.ts +204 -0
- package/src/crypto/providers/index.ts +44 -0
- package/src/crypto/providers/noble-provider.ts +392 -0
- package/src/crypto/providers/node-provider.ts +433 -0
- package/src/crypto/providers/quickcrypto-provider.ts +483 -0
- package/src/crypto/providers/registry.ts +129 -0
- package/src/crypto/providers/webcrypto-provider.ts +364 -0
- package/src/crypto/session-security.ts +185 -0
- package/src/crypto/types.ts +93 -0
- package/src/crypto/utils.ts +190 -0
- package/src/crypto-export.ts +21 -0
- package/src/index.ts +38 -0
- package/src/schemas/auth.ts +60 -0
- package/src/schemas/client-messages.ts +57 -0
- package/src/schemas/core.ts +144 -0
- package/src/schemas/crypto.ts +65 -0
- package/src/schemas/discovery.ts +79 -0
- package/src/schemas/index.ts +239 -0
- package/src/schemas/relay-messages.ts +45 -0
- package/src/schemas/wallet-messages.ts +177 -0
- package/src/schemas-export.ts +23 -0
- package/src/siwe-export.ts +27 -0
- package/src/testing.ts +71 -0
- package/src/types/auth.ts +60 -0
- package/src/types/client-messages.ts +84 -0
- package/src/types/core.ts +131 -0
- package/src/types/crypto-provider.ts +264 -0
- package/src/types/crypto.ts +90 -0
- package/src/types/discovery.ts +50 -0
- package/src/types/errors.ts +87 -0
- package/src/types/index.ts +197 -0
- package/src/types/post-auth-operations.ts +363 -0
- package/src/types/providers.ts +72 -0
- package/src/types/relay-messages.ts +60 -0
- package/src/types/request-lifecycle.ts +161 -0
- package/src/types/signing-operations.ts +99 -0
- package/src/types/wallet-messages.ts +251 -0
- package/src/utils/client-session-claim.ts +188 -0
- package/src/utils/index.ts +54 -0
- package/src/utils/public-keys.ts +49 -0
- package/src/utils/siwe.ts +362 -0
- package/src/utils/url-decoding.ts +126 -0
- package/src/utils/url-encoding.ts +144 -0
- package/src/utils/wallet-session-claim.ts +188 -0
- package/src/validation-export.ts +32 -0
- package/src/validators/index.ts +222 -0
- package/src/validators-export.ts +8 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client message types for BananaLink protocol v2.0
|
|
3
|
+
* These types define the communication between dApp clients and the relay server
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
SignMessageRequestPayload,
|
|
8
|
+
SignTypedDataRequestPayload,
|
|
9
|
+
SignTransactionRequestPayload,
|
|
10
|
+
} from './post-auth-operations';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Client session claim credential for authentication and reconnection
|
|
14
|
+
* Stored by client, validated by relay server
|
|
15
|
+
*/
|
|
16
|
+
export interface ClientSessionClaim {
|
|
17
|
+
/**
|
|
18
|
+
* Unique nonce for this client session
|
|
19
|
+
* - Generated once by client during session creation
|
|
20
|
+
* - Stored locally by client for reconnection
|
|
21
|
+
* - Used by relay to validate client ownership
|
|
22
|
+
* - Different for each session
|
|
23
|
+
*/
|
|
24
|
+
claimNonce: string;
|
|
25
|
+
|
|
26
|
+
/** Claim generation timestamp */
|
|
27
|
+
timestamp: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Client reconnect payload
|
|
32
|
+
* Used to reconnect after network interruption
|
|
33
|
+
*/
|
|
34
|
+
export interface ClientReconnectPayload {
|
|
35
|
+
type: 'client_reconnect';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Close session payload
|
|
40
|
+
* Used to gracefully close a session (bidirectional)
|
|
41
|
+
*/
|
|
42
|
+
export interface CloseSessionPayload {
|
|
43
|
+
type: 'close_session';
|
|
44
|
+
/** Optional reason for closing the session */
|
|
45
|
+
reason?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Union type for all client message payloads
|
|
50
|
+
*/
|
|
51
|
+
export type ClientMessagePayload =
|
|
52
|
+
// Session management payloads
|
|
53
|
+
| ClientReconnectPayload
|
|
54
|
+
| CloseSessionPayload
|
|
55
|
+
// Post-authentication operation request payloads (v2.1+)
|
|
56
|
+
| SignMessageRequestPayload
|
|
57
|
+
| SignTypedDataRequestPayload
|
|
58
|
+
| SignTransactionRequestPayload;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Client message envelope structure
|
|
62
|
+
* All client-to-relay messages use this envelope
|
|
63
|
+
*/
|
|
64
|
+
export interface ClientMessageEnvelope {
|
|
65
|
+
/** Session identifier */
|
|
66
|
+
sessionId: string;
|
|
67
|
+
|
|
68
|
+
/** Client session claim - validated by relay server */
|
|
69
|
+
clientSessionClaim: ClientSessionClaim;
|
|
70
|
+
|
|
71
|
+
/** Actual message payload */
|
|
72
|
+
payload: ClientMessagePayload;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Type guards for client messages
|
|
77
|
+
*/
|
|
78
|
+
export function isClientReconnectPayload(payload: ClientMessagePayload): payload is ClientReconnectPayload {
|
|
79
|
+
return payload?.type === 'client_reconnect';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function isCloseSessionPayload(payload: ClientMessagePayload): payload is CloseSessionPayload {
|
|
83
|
+
return payload?.type === 'close_session';
|
|
84
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for the BananaLink protocol
|
|
3
|
+
* Based on ERC-4361 SIWE format with BananaLink extensions
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Base SIWE types from ERC-4361
|
|
7
|
+
export interface SIWEFields {
|
|
8
|
+
/** The URI scheme of the origin of the request */
|
|
9
|
+
scheme?: string;
|
|
10
|
+
/** The domain that is requesting the signing */
|
|
11
|
+
domain: string;
|
|
12
|
+
/** The Ethereum address performing the signing */
|
|
13
|
+
address: string;
|
|
14
|
+
/** A human-readable ASCII assertion that the user will sign */
|
|
15
|
+
statement?: string;
|
|
16
|
+
/** An RFC 3986 URI referring to the resource that is the subject of the signing */
|
|
17
|
+
uri: string;
|
|
18
|
+
/** The current version of the SIWE Message, which MUST be 1 */
|
|
19
|
+
version: string;
|
|
20
|
+
/** The EIP-155 Chain ID to which the session is bound */
|
|
21
|
+
chainId: number;
|
|
22
|
+
/** A random string used to prevent replay attacks, at least 8 alphanumeric characters */
|
|
23
|
+
nonce: string;
|
|
24
|
+
/** The time when the message was generated, ISO 8601 datetime string */
|
|
25
|
+
issuedAt: string;
|
|
26
|
+
/** The time when the signed authentication message is no longer valid */
|
|
27
|
+
expirationTime?: string;
|
|
28
|
+
/** The time when the signed authentication message will become valid */
|
|
29
|
+
notBefore?: string;
|
|
30
|
+
/** A system-specific identifier that MAY be used to uniquely refer to the sign-in request */
|
|
31
|
+
requestId?: string;
|
|
32
|
+
/** A list of information or references to information the user wishes to have resolved */
|
|
33
|
+
resources?: string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// DApp metadata for registration and discovery
|
|
37
|
+
export interface DAppMetadata {
|
|
38
|
+
/** DApp unique identifier */
|
|
39
|
+
dappId?: string;
|
|
40
|
+
/** Name of the dApp */
|
|
41
|
+
name: string;
|
|
42
|
+
/** Description of the dApp */
|
|
43
|
+
description?: string;
|
|
44
|
+
/** URL of the dApp */
|
|
45
|
+
url: string;
|
|
46
|
+
/** Array of icon URLs */
|
|
47
|
+
icons?: string[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Session-specific configuration
|
|
51
|
+
export interface SessionConfig {
|
|
52
|
+
/** URL to redirect to after session completion */
|
|
53
|
+
returnUrl?: string;
|
|
54
|
+
/** Human-readable session identifier */
|
|
55
|
+
sessionName?: string;
|
|
56
|
+
/** Session-specific expiration override */
|
|
57
|
+
expiresAt?: string;
|
|
58
|
+
/** Custom session data */
|
|
59
|
+
customData?: Record<string, unknown>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Session creation options
|
|
63
|
+
export interface SessionOptions {
|
|
64
|
+
/** Session-specific configuration */
|
|
65
|
+
config?: SessionConfig;
|
|
66
|
+
/** Security policy overrides */
|
|
67
|
+
securityPolicy?: Partial<SecurityPolicy>;
|
|
68
|
+
/** Relay server URL override */
|
|
69
|
+
relayUrl?: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Security policy configuration
|
|
73
|
+
export interface SecurityPolicy {
|
|
74
|
+
/** Session timeout in milliseconds */
|
|
75
|
+
sessionTimeout: number;
|
|
76
|
+
/** Request timeout in milliseconds */
|
|
77
|
+
requestTimeout: number;
|
|
78
|
+
/** Maximum concurrent sessions */
|
|
79
|
+
maxConcurrentSessions: number;
|
|
80
|
+
/** Require origin proof */
|
|
81
|
+
requireOriginProof: boolean;
|
|
82
|
+
/** Allowed domains whitelist */
|
|
83
|
+
allowedDomains?: string[];
|
|
84
|
+
/** Blocked domains blacklist */
|
|
85
|
+
blockedDomains?: string[];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Session creation API types (HTTP Request/Response)
|
|
89
|
+
/**
|
|
90
|
+
* Session metadata for session creation
|
|
91
|
+
* Simplified cryptographic metadata
|
|
92
|
+
*/
|
|
93
|
+
export interface SessionMetadata {
|
|
94
|
+
/** Encryption algorithm */
|
|
95
|
+
encryptionAlgorithm: 'AES-GCM' | 'plaintext';
|
|
96
|
+
/** Session identifier (exchange session ID) */
|
|
97
|
+
sessionId: string;
|
|
98
|
+
/** Base64-encoded public key (required for AES-GCM) */
|
|
99
|
+
publicKey?: string;
|
|
100
|
+
/** Timestamp when metadata was created */
|
|
101
|
+
timestamp: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Session creation request (Client → API Gateway)
|
|
106
|
+
*/
|
|
107
|
+
export interface CreateSessionRequest {
|
|
108
|
+
/** DApp unique identifier */
|
|
109
|
+
dappId: string;
|
|
110
|
+
/** Session cryptographic metadata */
|
|
111
|
+
metadata: SessionMetadata;
|
|
112
|
+
/** Optional session configuration */
|
|
113
|
+
sessionConfig?: SessionConfig;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Session creation response (API Gateway → Client)
|
|
118
|
+
*/
|
|
119
|
+
export interface CreateSessionResponse {
|
|
120
|
+
/** Generated session identifier */
|
|
121
|
+
sessionId: string;
|
|
122
|
+
/** Client session claim for reconnection */
|
|
123
|
+
clientSessionClaim: {
|
|
124
|
+
/** Unique claim nonce */
|
|
125
|
+
claimNonce: string;
|
|
126
|
+
/** Claim timestamp */
|
|
127
|
+
timestamp: number;
|
|
128
|
+
};
|
|
129
|
+
/** Session time-to-live in seconds */
|
|
130
|
+
ttl: number;
|
|
131
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Crypto Provider Interface Types
|
|
3
|
+
*
|
|
4
|
+
* Pure TypeScript type definitions for environment-agnostic cryptographic operations.
|
|
5
|
+
* These types define the contract that crypto providers must implement.
|
|
6
|
+
*
|
|
7
|
+
* IMPORTANT: This file contains ONLY type definitions with zero runtime code.
|
|
8
|
+
* Actual implementations will be in the crypto/ directory.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Generic crypto key interface compatible across different crypto providers
|
|
13
|
+
* Provides a minimal contract that works with Web Crypto API, Node.js crypto, and pure JS implementations
|
|
14
|
+
*/
|
|
15
|
+
export interface CryptoKeyLike {
|
|
16
|
+
readonly type: 'public' | 'private' | 'secret';
|
|
17
|
+
readonly algorithm: string;
|
|
18
|
+
readonly extractable: boolean;
|
|
19
|
+
readonly usages: readonly string[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Key pair structure for asymmetric cryptography
|
|
24
|
+
*/
|
|
25
|
+
export interface KeyPairLike {
|
|
26
|
+
publicKey: CryptoKeyLike;
|
|
27
|
+
privateKey: CryptoKeyLike;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Provider-specific key pair (alias for compatibility)
|
|
32
|
+
*/
|
|
33
|
+
export interface ProviderKeyPair {
|
|
34
|
+
publicKey: CryptoKeyLike;
|
|
35
|
+
privateKey: CryptoKeyLike;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Environment-agnostic crypto provider interface
|
|
40
|
+
*
|
|
41
|
+
* This interface abstracts cryptographic operations to work across different environments:
|
|
42
|
+
* - Browser (Web Crypto API)
|
|
43
|
+
* - Node.js (crypto module / QuickCrypto)
|
|
44
|
+
* - React Native (@noble/curves fallback)
|
|
45
|
+
*
|
|
46
|
+
* All implementations must support:
|
|
47
|
+
* - ECDH P-256 for key exchange
|
|
48
|
+
* - AES-256-GCM for encryption
|
|
49
|
+
* - HKDF-SHA256 for key derivation
|
|
50
|
+
* - HMAC-SHA256 for authentication
|
|
51
|
+
*/
|
|
52
|
+
export interface CryptoProvider {
|
|
53
|
+
/** Provider name for identification and debugging */
|
|
54
|
+
readonly name: string;
|
|
55
|
+
|
|
56
|
+
/** Whether this provider is available in the current environment */
|
|
57
|
+
readonly isAvailable: boolean;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Generate ECDH P-256 key pair for key exchange
|
|
61
|
+
* @returns Promise resolving to public/private key pair
|
|
62
|
+
*/
|
|
63
|
+
generateKeyPair(): Promise<ProviderKeyPair>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Export public key to raw ArrayBuffer format (65 bytes uncompressed)
|
|
67
|
+
* @param publicKey - Public key to export
|
|
68
|
+
* @returns Promise resolving to raw key bytes
|
|
69
|
+
*/
|
|
70
|
+
exportPublicKey(publicKey: CryptoKeyLike): Promise<ArrayBuffer>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Export private key to raw ArrayBuffer format (32 bytes)
|
|
74
|
+
* @param privateKey - Private key to export
|
|
75
|
+
* @returns Promise resolving to raw key bytes
|
|
76
|
+
*/
|
|
77
|
+
exportPrivateKey(privateKey: CryptoKeyLike): Promise<ArrayBuffer>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Import public key from raw ArrayBuffer format
|
|
81
|
+
* @param keyData - Raw key bytes (65 bytes uncompressed)
|
|
82
|
+
* @returns Promise resolving to imported public key
|
|
83
|
+
*/
|
|
84
|
+
importPublicKey(keyData: ArrayBuffer): Promise<CryptoKeyLike>;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Import private key from raw ArrayBuffer format
|
|
88
|
+
* @param keyData - Raw key bytes (32 bytes)
|
|
89
|
+
* @returns Promise resolving to imported private key
|
|
90
|
+
*/
|
|
91
|
+
importPrivateKey(keyData: ArrayBuffer): Promise<CryptoKeyLike>;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Derive shared secret from ECDH key agreement
|
|
95
|
+
* @param privateKey - Local private key
|
|
96
|
+
* @param publicKey - Remote public key
|
|
97
|
+
* @returns Promise resolving to shared secret (raw key material)
|
|
98
|
+
*/
|
|
99
|
+
deriveSharedSecret(
|
|
100
|
+
privateKey: CryptoKeyLike,
|
|
101
|
+
publicKey: CryptoKeyLike,
|
|
102
|
+
): Promise<CryptoKeyLike>;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Derive AES-GCM encryption key from shared secret using HKDF
|
|
106
|
+
* @param sharedSecret - The shared secret from ECDH
|
|
107
|
+
* @param salt - Random salt for HKDF (recommended: 32 bytes)
|
|
108
|
+
* @param info - Context-specific info string for HKDF
|
|
109
|
+
* @returns Promise resolving to AES-256-GCM key
|
|
110
|
+
*/
|
|
111
|
+
deriveEncryptionKey(
|
|
112
|
+
sharedSecret: CryptoKeyLike,
|
|
113
|
+
salt: ArrayBuffer,
|
|
114
|
+
info: ArrayBuffer,
|
|
115
|
+
): Promise<CryptoKeyLike>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Generate cryptographically secure random bytes
|
|
119
|
+
* @param length - Number of bytes to generate
|
|
120
|
+
* @returns ArrayBuffer containing random bytes
|
|
121
|
+
*/
|
|
122
|
+
randomBytes(length: number): ArrayBuffer;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Encrypt data using AES-256-GCM
|
|
126
|
+
* @param key - AES-GCM encryption key
|
|
127
|
+
* @param data - Plaintext data to encrypt
|
|
128
|
+
* @param iv - Initialization vector (12 bytes for GCM)
|
|
129
|
+
* @returns Promise resolving to ciphertext with authentication tag
|
|
130
|
+
*/
|
|
131
|
+
encrypt(
|
|
132
|
+
key: CryptoKeyLike,
|
|
133
|
+
data: ArrayBuffer,
|
|
134
|
+
iv: ArrayBuffer,
|
|
135
|
+
): Promise<ArrayBuffer>;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Decrypt data using AES-256-GCM
|
|
139
|
+
* @param key - AES-GCM decryption key
|
|
140
|
+
* @param data - Ciphertext with authentication tag
|
|
141
|
+
* @param iv - Initialization vector (12 bytes for GCM)
|
|
142
|
+
* @returns Promise resolving to decrypted plaintext
|
|
143
|
+
* @throws Error if authentication tag verification fails
|
|
144
|
+
*/
|
|
145
|
+
decrypt(
|
|
146
|
+
key: CryptoKeyLike,
|
|
147
|
+
data: ArrayBuffer,
|
|
148
|
+
iv: ArrayBuffer,
|
|
149
|
+
): Promise<ArrayBuffer>;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Generate HMAC-SHA256 authentication code
|
|
153
|
+
* @param key - HMAC key (derived from AES key)
|
|
154
|
+
* @param data - Data to authenticate
|
|
155
|
+
* @returns Promise resolving to HMAC (32 bytes)
|
|
156
|
+
*/
|
|
157
|
+
generateHMAC(key: CryptoKeyLike, data: ArrayBuffer): Promise<ArrayBuffer>;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Verify HMAC-SHA256 authentication code
|
|
161
|
+
* @param key - HMAC key (derived from AES key)
|
|
162
|
+
* @param data - Data to verify
|
|
163
|
+
* @param mac - Expected HMAC value
|
|
164
|
+
* @returns Promise resolving to true if MAC is valid, false otherwise
|
|
165
|
+
*/
|
|
166
|
+
verifyHMAC(
|
|
167
|
+
key: CryptoKeyLike,
|
|
168
|
+
data: ArrayBuffer,
|
|
169
|
+
mac: ArrayBuffer,
|
|
170
|
+
): Promise<boolean>;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Factory function type for creating crypto provider instances
|
|
175
|
+
*/
|
|
176
|
+
export type CryptoProviderFactory = () => CryptoProvider;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Supported crypto provider types
|
|
180
|
+
* - webcrypto: Web Crypto API (browsers, modern Node.js)
|
|
181
|
+
* - node: Node.js native crypto (backend services, highest performance)
|
|
182
|
+
* - noble: @noble/curves (pure JavaScript, universal fallback)
|
|
183
|
+
* - quickcrypto: Node.js native crypto or QuickCrypto (performance optimized)
|
|
184
|
+
*/
|
|
185
|
+
export type CryptoProviderType = 'webcrypto' | 'node' | 'noble' | 'quickcrypto';
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Platform detection result
|
|
189
|
+
* Used to determine the best crypto provider for the current environment
|
|
190
|
+
*/
|
|
191
|
+
export interface PlatformDetectionResult {
|
|
192
|
+
/** Running in Node.js */
|
|
193
|
+
isNode: boolean;
|
|
194
|
+
/** Running in browser */
|
|
195
|
+
isBrowser: boolean;
|
|
196
|
+
/** Running in React Native */
|
|
197
|
+
isReactNative: boolean;
|
|
198
|
+
/** Platform identifier (e.g., 'darwin', 'linux', 'win32') */
|
|
199
|
+
platform?: string;
|
|
200
|
+
/** Browser user agent string */
|
|
201
|
+
userAgent?: string;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Granular capability information for a specific cryptographic operation
|
|
206
|
+
*/
|
|
207
|
+
export interface OperationCapability {
|
|
208
|
+
/** Whether this operation is available */
|
|
209
|
+
available: boolean;
|
|
210
|
+
/** Reason for unavailability (if available=false) */
|
|
211
|
+
reason?: string;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Detailed provider capabilities by operation type
|
|
216
|
+
* Used for granular detection and diagnostic reporting
|
|
217
|
+
*/
|
|
218
|
+
export interface CryptoCapabilities {
|
|
219
|
+
/** ECDH P-256 key exchange operations */
|
|
220
|
+
ecdh: OperationCapability;
|
|
221
|
+
/** AES-256-GCM encryption/decryption */
|
|
222
|
+
aesGcm: OperationCapability;
|
|
223
|
+
/** HMAC-SHA256 operations */
|
|
224
|
+
hmac: OperationCapability;
|
|
225
|
+
/** Random byte generation */
|
|
226
|
+
randomBytes: OperationCapability;
|
|
227
|
+
/** Overall provider availability */
|
|
228
|
+
overall: OperationCapability;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Provider availability detection result
|
|
233
|
+
* Reports which crypto providers are available in the current environment
|
|
234
|
+
*/
|
|
235
|
+
export interface ProviderDetectionResult {
|
|
236
|
+
/** Web Crypto API available */
|
|
237
|
+
webCrypto: boolean;
|
|
238
|
+
/** Node.js native crypto available */
|
|
239
|
+
node: boolean;
|
|
240
|
+
/** @noble/curves available */
|
|
241
|
+
noble: boolean;
|
|
242
|
+
/** QuickCrypto/Node crypto available */
|
|
243
|
+
quickCrypto: boolean;
|
|
244
|
+
/** Recommended provider for this environment */
|
|
245
|
+
recommended: CryptoProviderType;
|
|
246
|
+
/** Detected platform information */
|
|
247
|
+
platform: PlatformDetectionResult;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Enhanced provider detection with detailed capabilities
|
|
252
|
+
* Extends base detection with per-operation capability reporting
|
|
253
|
+
*/
|
|
254
|
+
export interface EnhancedProviderDetectionResult extends ProviderDetectionResult {
|
|
255
|
+
/** Detailed capability breakdown per provider */
|
|
256
|
+
capabilities: {
|
|
257
|
+
webcrypto?: CryptoCapabilities;
|
|
258
|
+
node?: CryptoCapabilities;
|
|
259
|
+
noble: CryptoCapabilities;
|
|
260
|
+
quickcrypto?: CryptoCapabilities;
|
|
261
|
+
};
|
|
262
|
+
/** Human-readable recommendations for developers */
|
|
263
|
+
recommendations: string[];
|
|
264
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Crypto payload types for BananaLink message encryption/decryption
|
|
3
|
+
* Based on crypto_payload_spec.md
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Key exchange algorithm specification
|
|
8
|
+
*/
|
|
9
|
+
export interface KeyExchange {
|
|
10
|
+
/** Elliptic Curve Diffie-Hellman */
|
|
11
|
+
algorithm: 'ECDH';
|
|
12
|
+
/** NIST P-256 curve (secp256r1) */
|
|
13
|
+
namedCurve: 'P-256';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Encryption algorithm specification
|
|
18
|
+
*/
|
|
19
|
+
export interface Encryption {
|
|
20
|
+
/** Encryption algorithm */
|
|
21
|
+
algorithm: 'AES-GCM' | 'plaintext';
|
|
22
|
+
/** AES key length in bits (production only) */
|
|
23
|
+
keyLength?: 256;
|
|
24
|
+
/** GCM authentication tag length (production only) */
|
|
25
|
+
tagLength?: 128;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Public key in JWK format for ECDH
|
|
30
|
+
*/
|
|
31
|
+
export interface PublicKeyJWK {
|
|
32
|
+
/** Key type: Elliptic Curve */
|
|
33
|
+
kty: 'EC';
|
|
34
|
+
/** Curve name */
|
|
35
|
+
crv: 'P-256';
|
|
36
|
+
/** X coordinate of public key (base64url) */
|
|
37
|
+
x: string;
|
|
38
|
+
/** Y coordinate of public key (base64url) */
|
|
39
|
+
y: string;
|
|
40
|
+
/** Intended use: encryption */
|
|
41
|
+
use: 'enc';
|
|
42
|
+
/** Permitted key operations */
|
|
43
|
+
key_ops: ['deriveKey'];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Algorithm parameters and metadata
|
|
48
|
+
*/
|
|
49
|
+
export interface CryptoParameters {
|
|
50
|
+
/** Base64-encoded initialization vector (12 bytes for GCM) */
|
|
51
|
+
iv?: string;
|
|
52
|
+
/** ISO 8601 timestamp for replay protection */
|
|
53
|
+
timestamp: string;
|
|
54
|
+
/** Unique session identifier for message correlation */
|
|
55
|
+
sessionId: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Key derivation specification
|
|
60
|
+
*/
|
|
61
|
+
export interface KeyDerivation {
|
|
62
|
+
/** HMAC-based Key Derivation Function */
|
|
63
|
+
algorithm: 'HKDF';
|
|
64
|
+
/** Hash function for HKDF */
|
|
65
|
+
hash: 'SHA-256';
|
|
66
|
+
/** Application-specific context info */
|
|
67
|
+
info: 'message-exchange-v1';
|
|
68
|
+
/** Random salt for key derivation (base64) */
|
|
69
|
+
salt: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Union type for crypto payload
|
|
74
|
+
*/
|
|
75
|
+
export interface CryptoPayload {
|
|
76
|
+
/** Schema version */
|
|
77
|
+
version: '1.0';
|
|
78
|
+
/** Key exchange algorithm specification */
|
|
79
|
+
keyExchange?: KeyExchange;
|
|
80
|
+
/** Encryption algorithm specification */
|
|
81
|
+
encryption: Encryption & { algorithm: 'AES-GCM' | 'plaintext' };
|
|
82
|
+
/** Public key in JWK format */
|
|
83
|
+
publicKey?: PublicKeyJWK;
|
|
84
|
+
/** Algorithm parameters and metadata */
|
|
85
|
+
parameters: CryptoParameters;
|
|
86
|
+
/** Key derivation specification */
|
|
87
|
+
derivation?: KeyDerivation;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discovery types for the BananaLink protocol
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// QR Code payload for discovery
|
|
6
|
+
export interface QRPayload {
|
|
7
|
+
/** Session identifier */
|
|
8
|
+
sessionId: string;
|
|
9
|
+
/** Public key with encryption algorithm (format: "enc_algo:public_key_base64") */
|
|
10
|
+
publicKey: string;
|
|
11
|
+
/** Optional relay URL for direct connection (overrides provider selection) */
|
|
12
|
+
relayUrl?: string;
|
|
13
|
+
/** Optional provider hint for transport selection */
|
|
14
|
+
providerId?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Display information for QR/FrutiLink
|
|
18
|
+
export interface DisplayInfo {
|
|
19
|
+
/** QR code data URL */
|
|
20
|
+
qrCode: string;
|
|
21
|
+
/** FrutiLink emoji sequence */
|
|
22
|
+
frutiLink: string;
|
|
23
|
+
/** Deep link URL */
|
|
24
|
+
deepLink: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Relay server message types
|
|
28
|
+
export type RelayMessageType = 'pub' | 'sub' | 'ack' | 'error';
|
|
29
|
+
|
|
30
|
+
// Encrypted payload for secure communication
|
|
31
|
+
export interface EncryptedPayload {
|
|
32
|
+
/** Base64 encoded initialization vector */
|
|
33
|
+
iv: string;
|
|
34
|
+
/** Base64 encoded ciphertext */
|
|
35
|
+
ciphertext: string;
|
|
36
|
+
/** Base64 encoded HMAC for integrity */
|
|
37
|
+
mac: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Relay server message
|
|
41
|
+
export interface RelayMessage {
|
|
42
|
+
/** Session ID used as topic */
|
|
43
|
+
topic: string;
|
|
44
|
+
/** Message type */
|
|
45
|
+
type: RelayMessageType;
|
|
46
|
+
/** Encrypted payload (optional for control messages) */
|
|
47
|
+
payload?: EncryptedPayload;
|
|
48
|
+
/** Message ID for acknowledgment */
|
|
49
|
+
id: string;
|
|
50
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error types for the BananaLink protocol v2.0
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// Error codes for BananaLink protocol
|
|
6
|
+
// Organized hierarchically for better maintainability
|
|
7
|
+
export type BananaLinkErrorCode =
|
|
8
|
+
// ===== SESSION ERRORS =====
|
|
9
|
+
| 'SESSION_EXPIRED'
|
|
10
|
+
| 'SESSION_NOT_FOUND'
|
|
11
|
+
| 'SESSION_ALREADY_CLAIMED'
|
|
12
|
+
| 'INVALID_SESSION_CLAIM'
|
|
13
|
+
| 'INVALID_CLIENT_CLAIM'
|
|
14
|
+
| 'SESSION_TOKEN_EXPIRED'
|
|
15
|
+
| 'SESSION_CLOSED'
|
|
16
|
+
|
|
17
|
+
// ===== ENCRYPTION ERRORS =====
|
|
18
|
+
| 'ENCRYPTION_FAILED'
|
|
19
|
+
| 'DECRYPTION_FAILED'
|
|
20
|
+
| 'INVALID_SIGNATURE'
|
|
21
|
+
|
|
22
|
+
// ===== NETWORK ERRORS =====
|
|
23
|
+
| 'NETWORK_ERROR'
|
|
24
|
+
| 'RELAY_ERROR'
|
|
25
|
+
| 'MESSAGE_QUEUE_FULL'
|
|
26
|
+
| 'RATE_LIMIT_EXCEEDED'
|
|
27
|
+
| 'ORIGIN_MISMATCH'
|
|
28
|
+
|
|
29
|
+
// ===== REQUEST LIFECYCLE ERRORS (v2.1+) =====
|
|
30
|
+
| 'REQUEST_PENDING' // Another request already pending
|
|
31
|
+
| 'REQUEST_NOT_FOUND' // Request ID not found
|
|
32
|
+
| 'REQUEST_EXPIRED' // Request TTL exceeded
|
|
33
|
+
| 'REQUEST_INVALID' // Malformed request
|
|
34
|
+
| 'REQUEST_TIMEOUT' // Wallet didn't respond in time
|
|
35
|
+
|
|
36
|
+
// ===== OPERATION-SPECIFIC ERRORS (v2.1+) =====
|
|
37
|
+
| 'SIGNING_FAILED' // Generic signing error
|
|
38
|
+
| 'SIGNING_REJECTED' // User rejected signing
|
|
39
|
+
| 'TRANSACTION_INVALID' // Transaction validation failed
|
|
40
|
+
| 'CHAIN_MISMATCH'; // Chain ID doesn't match wallet
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Error response format for protocol v2.0
|
|
44
|
+
* Used for standardized error responses in the BananaLink protocol
|
|
45
|
+
*/
|
|
46
|
+
export interface ProtocolErrorResponse {
|
|
47
|
+
/** Error type discriminator */
|
|
48
|
+
type: 'error';
|
|
49
|
+
/** Error code from BananaLinkErrorCode */
|
|
50
|
+
code: string;
|
|
51
|
+
/** Human-readable error message */
|
|
52
|
+
message: string;
|
|
53
|
+
/** Additional error details */
|
|
54
|
+
details?: unknown;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Wallet-initiated rejection reasons
|
|
59
|
+
* Used in connection_rejected messages
|
|
60
|
+
*/
|
|
61
|
+
export type WalletRejectionReason =
|
|
62
|
+
| 'user_declined' // User explicitly rejected connection
|
|
63
|
+
| 'untrusted_dapp' // dApp not in user's trusted list
|
|
64
|
+
| 'unsupported_chain' // Requested chain not supported by wallet
|
|
65
|
+
| 'insufficient_balance' // Wallet has insufficient gas
|
|
66
|
+
| 'wallet_locked' // Wallet is locked/requires authentication
|
|
67
|
+
| 'timeout' // User didn't respond within timeout
|
|
68
|
+
| 'security_warning' // Security scan flagged dApp
|
|
69
|
+
| 'rate_limited'; // Too many connection requests
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Relay-initiated rejection reasons
|
|
73
|
+
* Used when relay server rejects a connection
|
|
74
|
+
*/
|
|
75
|
+
export type RelayRejectionReason =
|
|
76
|
+
| 'session_already_claimed' // Session claimed by another wallet
|
|
77
|
+
| 'invalid_credentials' // Session token/claim invalid
|
|
78
|
+
| 'session_expired' // Session TTL exceeded before claim
|
|
79
|
+
| 'session_closed' // Session explicitly closed
|
|
80
|
+
| 'dapp_metadata_missing' // Cannot fetch dApp metadata
|
|
81
|
+
| 'encryption_mismatch' // Incompatible encryption algorithms
|
|
82
|
+
| 'relay_overloaded'; // Relay at capacity
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* All possible rejection reasons
|
|
86
|
+
*/
|
|
87
|
+
export type RejectionReason = WalletRejectionReason | RelayRejectionReason;
|