@temple-digital-group/temple-canton-js 1.0.37 → 1.0.38-beta

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.
Files changed (29) hide show
  1. package/README.md +361 -361
  2. package/index.js +12 -12
  3. package/package.json +47 -47
  4. package/src/api/config.d.ts +20 -20
  5. package/src/api/index.ts +355 -355
  6. package/src/api/tokenStore.ts +87 -87
  7. package/src/api/types.ts +149 -149
  8. package/src/auth0/index.js +50 -50
  9. package/src/canton/index.js +3825 -3825
  10. package/src/canton/instrumentCatalog.js +174 -174
  11. package/src/canton/request_schemas/cancel_orders_amulet.json +77 -77
  12. package/src/canton/request_schemas/cancel_orders_utility.json +68 -68
  13. package/src/canton/request_schemas/create_order_proposal_amulet.json +94 -94
  14. package/src/canton/request_schemas/create_order_proposal_utility.json +121 -121
  15. package/src/canton/request_schemas/create_utility_credential.json +31 -31
  16. package/src/canton/request_schemas/execute_transfer_factory.json +43 -43
  17. package/src/canton/request_schemas/get_allocation_factory.json +21 -21
  18. package/src/canton/request_schemas/get_amulet_holdings.json +21 -21
  19. package/src/canton/request_schemas/get_instrument_configurations.json +21 -21
  20. package/src/canton/request_schemas/get_locked_amulet_holdings.json +21 -21
  21. package/src/canton/request_schemas/get_order_proposals.json +21 -21
  22. package/src/canton/request_schemas/get_orders.json +21 -21
  23. package/src/canton/request_schemas/get_sender_credentials.json +22 -22
  24. package/src/canton/request_schemas/get_transfer_factory.json +28 -28
  25. package/src/canton/request_schemas/get_utility_holdings.json +21 -21
  26. package/src/canton/request_schemas/unlock_amulet.json +38 -38
  27. package/src/canton/walletAdapter.js +89 -89
  28. package/src/config/index.d.ts +57 -57
  29. package/src/config/index.js +154 -154
@@ -1,87 +1,87 @@
1
- import type { TokenData, AuthHeaders } from "./types.js";
2
-
3
- let accessToken: string | null = null;
4
- let refreshToken: string | null = null;
5
- let tokenExpiryTime: number | null = null;
6
- let apiKey: string | null = null;
7
- let userId: string | null = null;
8
-
9
- /**
10
- * Store tokens received from login or refresh.
11
- */
12
- export function setTokens(data: TokenData): void {
13
- accessToken = data.access_token;
14
- refreshToken = data.refresh_token;
15
- tokenExpiryTime = Date.now() + data.expires_in * 1000;
16
- if (data.user?.user_id != null) {
17
- userId = String(data.user.user_id);
18
- }
19
- }
20
-
21
- /**
22
- * Get the current access token, or null if not logged in.
23
- */
24
- export function getAccessToken(): string | null {
25
- return accessToken;
26
- }
27
-
28
- /**
29
- * Get the stored refresh token, or null if not logged in.
30
- */
31
- export function getRefreshToken(): string | null {
32
- return refreshToken;
33
- }
34
-
35
- /**
36
- * Check if the current access token is expired (with 60-second buffer).
37
- */
38
- export function isTokenExpired(): boolean {
39
- if (!tokenExpiryTime) return true;
40
- return Date.now() >= tokenExpiryTime - 60_000;
41
- }
42
-
43
- /**
44
- * Get the stored user ID from login, or null if not logged in.
45
- */
46
- export function getUserId(): string | null {
47
- return userId;
48
- }
49
-
50
-
51
- /**
52
- * Clear all stored tokens (logout).
53
- */
54
- export function clearTokens(): void {
55
- accessToken = null;
56
- refreshToken = null;
57
- tokenExpiryTime = null;
58
- userId = null;
59
- }
60
-
61
- /**
62
- * Set an API key for authentication (future: replaces email/password login).
63
- */
64
- export function setApiKey(key: string): void {
65
- apiKey = key;
66
- }
67
-
68
- /**
69
- * Get the stored API key, or null if not set.
70
- */
71
- export function getApiKey(): string | null {
72
- return apiKey;
73
- }
74
-
75
- /**
76
- * Get the authorization header. Prefers API key if set, otherwise uses Bearer token.
77
- * Returns null if neither is available.
78
- */
79
- export function getAuthHeader(): AuthHeaders | null {
80
- if (apiKey) {
81
- return { Authorization: `Bearer ${apiKey}` };
82
- }
83
- if (accessToken) {
84
- return { Authorization: `Bearer ${accessToken}` };
85
- }
86
- return null;
87
- }
1
+ import type { TokenData, AuthHeaders } from "./types.js";
2
+
3
+ let accessToken: string | null = null;
4
+ let refreshToken: string | null = null;
5
+ let tokenExpiryTime: number | null = null;
6
+ let apiKey: string | null = null;
7
+ let userId: string | null = null;
8
+
9
+ /**
10
+ * Store tokens received from login or refresh.
11
+ */
12
+ export function setTokens(data: TokenData): void {
13
+ accessToken = data.access_token;
14
+ refreshToken = data.refresh_token;
15
+ tokenExpiryTime = Date.now() + data.expires_in * 1000;
16
+ if (data.user?.user_id != null) {
17
+ userId = String(data.user.user_id);
18
+ }
19
+ }
20
+
21
+ /**
22
+ * Get the current access token, or null if not logged in.
23
+ */
24
+ export function getAccessToken(): string | null {
25
+ return accessToken;
26
+ }
27
+
28
+ /**
29
+ * Get the stored refresh token, or null if not logged in.
30
+ */
31
+ export function getRefreshToken(): string | null {
32
+ return refreshToken;
33
+ }
34
+
35
+ /**
36
+ * Check if the current access token is expired (with 60-second buffer).
37
+ */
38
+ export function isTokenExpired(): boolean {
39
+ if (!tokenExpiryTime) return true;
40
+ return Date.now() >= tokenExpiryTime - 60_000;
41
+ }
42
+
43
+ /**
44
+ * Get the stored user ID from login, or null if not logged in.
45
+ */
46
+ export function getUserId(): string | null {
47
+ return userId;
48
+ }
49
+
50
+
51
+ /**
52
+ * Clear all stored tokens (logout).
53
+ */
54
+ export function clearTokens(): void {
55
+ accessToken = null;
56
+ refreshToken = null;
57
+ tokenExpiryTime = null;
58
+ userId = null;
59
+ }
60
+
61
+ /**
62
+ * Set an API key for authentication (future: replaces email/password login).
63
+ */
64
+ export function setApiKey(key: string): void {
65
+ apiKey = key;
66
+ }
67
+
68
+ /**
69
+ * Get the stored API key, or null if not set.
70
+ */
71
+ export function getApiKey(): string | null {
72
+ return apiKey;
73
+ }
74
+
75
+ /**
76
+ * Get the authorization header. Prefers API key if set, otherwise uses Bearer token.
77
+ * Returns null if neither is available.
78
+ */
79
+ export function getAuthHeader(): AuthHeaders | null {
80
+ if (apiKey) {
81
+ return { Authorization: `Bearer ${apiKey}` };
82
+ }
83
+ if (accessToken) {
84
+ return { Authorization: `Bearer ${accessToken}` };
85
+ }
86
+ return null;
87
+ }
package/src/api/types.ts CHANGED
@@ -1,149 +1,149 @@
1
- // ── Auth ──
2
-
3
- export interface LoginUser {
4
- user_id: number;
5
- max_limit_orders: number;
6
- max_market_orders: number;
7
- }
8
-
9
- export interface LoginResponse {
10
- access_token: string;
11
- refresh_token: string;
12
- expires_in: number;
13
- token_type: string;
14
- user: LoginUser;
15
- }
16
-
17
- export interface RefreshResponse {
18
- access_token: string;
19
- refresh_token: string;
20
- expires_in: number;
21
- }
22
-
23
- // ── Market Data ──
24
-
25
- export interface Ticker {
26
- symbol: string;
27
- last_price: string;
28
- bid_price: string;
29
- ask_price: string;
30
- high_24h: string;
31
- low_24h: string;
32
- volume_24h: string;
33
- change_24h: string;
34
- change_pct_24h: string;
35
- timestamp: string;
36
- }
37
-
38
- export interface OrderBookLevel {
39
- price: string;
40
- quantity: string;
41
- }
42
-
43
- export interface OrderBook {
44
- symbol: string;
45
- bids: OrderBookLevel[];
46
- asks: OrderBookLevel[];
47
- best_bid: string;
48
- best_ask: string;
49
- spread: string;
50
- timestamp: string;
51
- }
52
-
53
- export interface OrderBookOptions {
54
- levels?: number;
55
- precision?: 2 | 4;
56
- }
57
-
58
- export interface SymbolConfig {
59
- symbol: string;
60
- paused: boolean;
61
- max_decimals: number;
62
- minimum_quantity: string;
63
- }
64
-
65
- export interface OpenInterest {
66
- symbol: string;
67
- bid_interest: string;
68
- ask_interest: string;
69
- bid_order_count: number;
70
- ask_order_count: number;
71
- timestamp: string;
72
- }
73
-
74
- export interface Trade {
75
- trade_id: string;
76
- symbol: string;
77
- side: string;
78
- quantity: string;
79
- price: string;
80
- timestamp: string;
81
- }
82
-
83
- export interface RecentTradesOptions {
84
- limit?: number;
85
- }
86
-
87
- // ── Orders ──
88
-
89
- export interface ActiveOrder {
90
- order_id: string;
91
- symbol: string;
92
- side: string;
93
- quantity: string;
94
- price: string;
95
- status: string;
96
- created_at: string;
97
- updated_at: string;
98
- }
99
-
100
- export interface ActiveOrdersOptions {
101
- symbol?: string;
102
- limit?: number;
103
- }
104
-
105
- export interface CancelOrderResponse {
106
- success: boolean;
107
- order_id: string;
108
- already_queued: boolean;
109
- message: string;
110
- }
111
-
112
- export interface CancelAllOrdersOptions {
113
- symbol?: string;
114
- }
115
-
116
- export interface CancelAllOrdersResponse {
117
- success: boolean;
118
- canceled_count: number;
119
- already_queued_count: number;
120
- order_ids: string[];
121
- }
122
-
123
- // ── Disclosures ──
124
-
125
- export interface DisclosuresResponse {
126
- disclosures: Record<string, unknown>;
127
- }
128
-
129
- // ── Errors ──
130
-
131
- export interface ApiError {
132
- error: true;
133
- status: number | null;
134
- code: string | null;
135
- message: string;
136
- }
137
-
138
- // ── Token Store ──
139
-
140
- export interface TokenData {
141
- access_token: string;
142
- refresh_token: string;
143
- expires_in: number;
144
- user?: LoginUser;
145
- }
146
-
147
- export interface AuthHeaders {
148
- Authorization: string;
149
- }
1
+ // ── Auth ──
2
+
3
+ export interface LoginUser {
4
+ user_id: number;
5
+ max_limit_orders: number;
6
+ max_market_orders: number;
7
+ }
8
+
9
+ export interface LoginResponse {
10
+ access_token: string;
11
+ refresh_token: string;
12
+ expires_in: number;
13
+ token_type: string;
14
+ user: LoginUser;
15
+ }
16
+
17
+ export interface RefreshResponse {
18
+ access_token: string;
19
+ refresh_token: string;
20
+ expires_in: number;
21
+ }
22
+
23
+ // ── Market Data ──
24
+
25
+ export interface Ticker {
26
+ symbol: string;
27
+ last_price: string;
28
+ bid_price: string;
29
+ ask_price: string;
30
+ high_24h: string;
31
+ low_24h: string;
32
+ volume_24h: string;
33
+ change_24h: string;
34
+ change_pct_24h: string;
35
+ timestamp: string;
36
+ }
37
+
38
+ export interface OrderBookLevel {
39
+ price: string;
40
+ quantity: string;
41
+ }
42
+
43
+ export interface OrderBook {
44
+ symbol: string;
45
+ bids: OrderBookLevel[];
46
+ asks: OrderBookLevel[];
47
+ best_bid: string;
48
+ best_ask: string;
49
+ spread: string;
50
+ timestamp: string;
51
+ }
52
+
53
+ export interface OrderBookOptions {
54
+ levels?: number;
55
+ precision?: 2 | 4;
56
+ }
57
+
58
+ export interface SymbolConfig {
59
+ symbol: string;
60
+ paused: boolean;
61
+ max_decimals: number;
62
+ minimum_quantity: string;
63
+ }
64
+
65
+ export interface OpenInterest {
66
+ symbol: string;
67
+ bid_interest: string;
68
+ ask_interest: string;
69
+ bid_order_count: number;
70
+ ask_order_count: number;
71
+ timestamp: string;
72
+ }
73
+
74
+ export interface Trade {
75
+ trade_id: string;
76
+ symbol: string;
77
+ side: string;
78
+ quantity: string;
79
+ price: string;
80
+ timestamp: string;
81
+ }
82
+
83
+ export interface RecentTradesOptions {
84
+ limit?: number;
85
+ }
86
+
87
+ // ── Orders ──
88
+
89
+ export interface ActiveOrder {
90
+ order_id: string;
91
+ symbol: string;
92
+ side: string;
93
+ quantity: string;
94
+ price: string;
95
+ status: string;
96
+ created_at: string;
97
+ updated_at: string;
98
+ }
99
+
100
+ export interface ActiveOrdersOptions {
101
+ symbol?: string;
102
+ limit?: number;
103
+ }
104
+
105
+ export interface CancelOrderResponse {
106
+ success: boolean;
107
+ order_id: string;
108
+ already_queued: boolean;
109
+ message: string;
110
+ }
111
+
112
+ export interface CancelAllOrdersOptions {
113
+ symbol?: string;
114
+ }
115
+
116
+ export interface CancelAllOrdersResponse {
117
+ success: boolean;
118
+ canceled_count: number;
119
+ already_queued_count: number;
120
+ order_ids: string[];
121
+ }
122
+
123
+ // ── Disclosures ──
124
+
125
+ export interface DisclosuresResponse {
126
+ disclosures: Record<string, unknown>;
127
+ }
128
+
129
+ // ── Errors ──
130
+
131
+ export interface ApiError {
132
+ error: true;
133
+ status: number | null;
134
+ code: string | null;
135
+ message: string;
136
+ }
137
+
138
+ // ── Token Store ──
139
+
140
+ export interface TokenData {
141
+ access_token: string;
142
+ refresh_token: string;
143
+ expires_in: number;
144
+ user?: LoginUser;
145
+ }
146
+
147
+ export interface AuthHeaders {
148
+ Authorization: string;
149
+ }
@@ -1,50 +1,50 @@
1
- import config from "../config/index.js";
2
- import axios from "axios";
3
-
4
- let jwtToken = null;
5
- let tokenExpiryTime = null;
6
-
7
- /**
8
- * Get an Auth0 JWT access token with automatic caching and refresh.
9
- * Tokens are cached and reused until they expire (with a 60-second buffer).
10
- * Requires AUTH0_TOKEN_URL, AUTH0_CLIENT_ID, AUTH0_CLIENT_SECRET, and AUTH0_AUDIENCE to be configured.
11
- * @returns {Promise<string|null>} The JWT access token, or null on failure
12
- */
13
- export async function getJWTToken() {
14
- // Return cached token if not expired
15
- if (jwtToken != null && tokenExpiryTime != null) {
16
- // Check if expired (with 60 second buffer to avoid edge cases)
17
- const isExpired = Date.now() >= tokenExpiryTime - 60000;
18
- if (!isExpired) {
19
- return jwtToken.access_token;
20
- }
21
- }
22
- const encodedParams = new URLSearchParams();
23
- encodedParams.set("grant_type", config.AUTH0_GRANT_TYPE);
24
- encodedParams.set("client_id", config.AUTH0_CLIENT_ID);
25
- encodedParams.set("client_secret", config.AUTH0_CLIENT_SECRET);
26
- encodedParams.set("audience", config.AUTH0_AUDIENCE);
27
- encodedParams.set("scope", config.AUTH0_SCOPE);
28
-
29
- const options = {
30
- method: "POST",
31
- url: config.AUTH0_TOKEN_URL,
32
- headers: {
33
- "content-type": "application/x-www-form-urlencoded",
34
- },
35
- data: encodedParams,
36
- };
37
-
38
- try {
39
- const { data } = await axios.request(options);
40
- jwtToken = data;
41
- // Calculate expiry time: current time + expires_in (in seconds) * 1000 (to convert to milliseconds)
42
- tokenExpiryTime = Date.now() + data.expires_in * 1000;
43
- return jwtToken.access_token;
44
- } catch (error) {
45
- console.error(error);
46
- tokenExpiryTime = null;
47
- jwtToken = null;
48
- return null;
49
- }
50
- }
1
+ import config from "../config/index.js";
2
+ import axios from "axios";
3
+
4
+ let jwtToken = null;
5
+ let tokenExpiryTime = null;
6
+
7
+ /**
8
+ * Get an Auth0 JWT access token with automatic caching and refresh.
9
+ * Tokens are cached and reused until they expire (with a 60-second buffer).
10
+ * Requires AUTH0_TOKEN_URL, AUTH0_CLIENT_ID, AUTH0_CLIENT_SECRET, and AUTH0_AUDIENCE to be configured.
11
+ * @returns {Promise<string|null>} The JWT access token, or null on failure
12
+ */
13
+ export async function getJWTToken() {
14
+ // Return cached token if not expired
15
+ if (jwtToken != null && tokenExpiryTime != null) {
16
+ // Check if expired (with 60 second buffer to avoid edge cases)
17
+ const isExpired = Date.now() >= tokenExpiryTime - 60000;
18
+ if (!isExpired) {
19
+ return jwtToken.access_token;
20
+ }
21
+ }
22
+ const encodedParams = new URLSearchParams();
23
+ encodedParams.set("grant_type", config.AUTH0_GRANT_TYPE);
24
+ encodedParams.set("client_id", config.AUTH0_CLIENT_ID);
25
+ encodedParams.set("client_secret", config.AUTH0_CLIENT_SECRET);
26
+ encodedParams.set("audience", config.AUTH0_AUDIENCE);
27
+ encodedParams.set("scope", config.AUTH0_SCOPE);
28
+
29
+ const options = {
30
+ method: "POST",
31
+ url: config.AUTH0_TOKEN_URL,
32
+ headers: {
33
+ "content-type": "application/x-www-form-urlencoded",
34
+ },
35
+ data: encodedParams,
36
+ };
37
+
38
+ try {
39
+ const { data } = await axios.request(options);
40
+ jwtToken = data;
41
+ // Calculate expiry time: current time + expires_in (in seconds) * 1000 (to convert to milliseconds)
42
+ tokenExpiryTime = Date.now() + data.expires_in * 1000;
43
+ return jwtToken.access_token;
44
+ } catch (error) {
45
+ console.error(error);
46
+ tokenExpiryTime = null;
47
+ jwtToken = null;
48
+ return null;
49
+ }
50
+ }