@pear-protocol/utils 0.5.1 → 0.5.2
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/dist/index.d.ts +1 -0
- package/dist/index.js +533 -185
- package/dist/xauth/index.d.ts +78 -0
- package/package.json +3 -2
- package/dist/asset/compute.test.d.ts +0 -1
- package/dist/basket/derive-basket-key.test.d.ts +0 -1
- package/dist/fills/compute.test.d.ts +0 -1
- package/dist/math/decimal.test.d.ts +0 -1
- package/dist/position/compute.test.d.ts +0 -1
- package/dist/proxy.test.d.ts +0 -1
- package/dist/sync/applicables/basket.test.d.ts +0 -1
- package/dist/sync/applicables/exposure.test.d.ts +0 -1
- package/dist/sync/applicables/fills.test.d.ts +0 -1
- package/dist/sync/applicables/index.test.d.ts +0 -1
- package/dist/sync/applicables/matching.test.d.ts +0 -1
- package/dist/sync/applicables/position.test.d.ts +0 -1
- package/dist/sync/applicables/synthetics.test.d.ts +0 -1
- package/dist/sync/compute.test.d.ts +0 -1
- package/dist/sync/cutoff.test.d.ts +0 -1
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The browser's half of an X sign-in.
|
|
3
|
+
*
|
|
4
|
+
* X returns the user by redirect, and a redirect cannot carry a credential up or a token
|
|
5
|
+
* pair back down. So the callback settles who the user is and leaves a single-use ticket on
|
|
6
|
+
* the return URL, which the frontend redeems over an XHR of its own.
|
|
7
|
+
*
|
|
8
|
+
* A ticket travelling in a URL is only safe if holding it is not enough. That is what these
|
|
9
|
+
* two functions are for: `startXHandoff` keeps a secret on this origin before the browser
|
|
10
|
+
* leaves, and `finishXHandoff` produces it again on the way back. A ticket lifted from
|
|
11
|
+
* someone else's URL matches no secret the lifting browser holds.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Where the secret waits while the browser is away at X.
|
|
15
|
+
*
|
|
16
|
+
* The subset of `Storage` this needs, so a caller can pass `localStorage`, a test double,
|
|
17
|
+
* or something else entirely.
|
|
18
|
+
*
|
|
19
|
+
* `localStorage` is the sane choice over `sessionStorage`: the flow may return in a tab
|
|
20
|
+
* opened for it, and browsers disagree about whether a new tab inherits session storage.
|
|
21
|
+
* Sharing across tabs costs nothing, because a ticket is bound to a secret, not to a tab.
|
|
22
|
+
*/
|
|
23
|
+
export type XHandoffStorage = {
|
|
24
|
+
getItem(key: string): string | null;
|
|
25
|
+
setItem(key: string, value: string): void;
|
|
26
|
+
removeItem(key: string): void;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* What an X flow left on the page it returned the browser to.
|
|
30
|
+
*
|
|
31
|
+
* A link is already done by the time the browser lands and only needs reporting. A sign-in
|
|
32
|
+
* is not: `login-settled` carries both halves needed to redeem it, so the caller never
|
|
33
|
+
* reaches into storage itself.
|
|
34
|
+
*/
|
|
35
|
+
export type XReturn = {
|
|
36
|
+
kind: 'link-succeeded';
|
|
37
|
+
status: 'connected' | 'reconnected';
|
|
38
|
+
} | {
|
|
39
|
+
kind: 'link-failed';
|
|
40
|
+
reason: string | undefined;
|
|
41
|
+
} | {
|
|
42
|
+
kind: 'login-settled';
|
|
43
|
+
ticket: string;
|
|
44
|
+
verifier: string;
|
|
45
|
+
} | {
|
|
46
|
+
kind: 'login-failed';
|
|
47
|
+
reason: string | undefined;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Begin a sign-in: keep a secret here, and return what the server needs.
|
|
51
|
+
*
|
|
52
|
+
* Send both fields to `startXLogin`. The server holds the challenge against the flow and
|
|
53
|
+
* echoes the id back on the return URL, which is how a browser that started more than one
|
|
54
|
+
* sign-in knows which secret this outcome belongs to.
|
|
55
|
+
*/
|
|
56
|
+
export declare function startXHandoff(storage: XHandoffStorage): {
|
|
57
|
+
handoffId: string;
|
|
58
|
+
challenge: string;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Read the outcome this page is the end of, and retire the secret behind it.
|
|
62
|
+
*
|
|
63
|
+
* Returns undefined when the page is not the end of an X flow at all. Every sign-in outcome
|
|
64
|
+
* clears its stored secret, success or failure, so nothing spent is left behind.
|
|
65
|
+
*
|
|
66
|
+
* The ticket comes out of the fragment rather than the query, which is where the callback
|
|
67
|
+
* puts it: no browser sends a fragment to a server, so it stays out of the frontend's access
|
|
68
|
+
* logs and out of the `Referer` of anything the page loads.
|
|
69
|
+
*/
|
|
70
|
+
export declare function finishXHandoff(href: string, storage: XHandoffStorage): XReturn | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* The same page with every X parameter taken back off it.
|
|
73
|
+
*
|
|
74
|
+
* What the user reloads or shares should be where they were, never a spent ticket. The
|
|
75
|
+
* fragment goes entirely: the flow put the ticket there, and any fragment the page had was
|
|
76
|
+
* dropped when the return URL was recorded at start time.
|
|
77
|
+
*/
|
|
78
|
+
export declare function stripXReturn(href: string): string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pear-protocol/utils",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Pear Protocol Utility functions",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@noble/hashes": "^1.3.2",
|
|
30
|
-
"@pear-protocol/types": "^1.
|
|
30
|
+
"@pear-protocol/types": "^1.24.1",
|
|
31
|
+
"@scure/base": "^2.3.0",
|
|
31
32
|
"bignumber.js": "9.3.1"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/proxy.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|