@mezo-org/passport 0.4.0-dev.1 → 0.4.0-dev.11
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/src/api/auth.d.ts +57 -52
- package/dist/src/api/auth.d.ts.map +1 -1
- package/dist/src/api/auth.js +22 -53
- package/dist/src/api/auth.js.map +1 -1
- package/dist/src/api/client.d.ts +24 -0
- package/dist/src/api/client.d.ts.map +1 -0
- package/dist/src/api/client.js +54 -0
- package/dist/src/api/client.js.map +1 -0
- package/dist/src/api/fetch-error.d.ts +5 -0
- package/dist/src/api/fetch-error.d.ts.map +1 -0
- package/dist/src/api/fetch-error.js +8 -0
- package/dist/src/api/fetch-error.js.map +1 -0
- package/dist/src/api/index.d.ts +3 -0
- package/dist/src/api/index.d.ts.map +1 -0
- package/dist/src/api/index.js +3 -0
- package/dist/src/api/index.js.map +1 -0
- package/dist/src/api/portal.d.ts +32 -0
- package/dist/src/api/portal.d.ts.map +1 -0
- package/dist/src/api/portal.js +23 -0
- package/dist/src/api/portal.js.map +1 -0
- package/dist/src/assets/DefaultAvatar.d.ts +5 -0
- package/dist/src/assets/DefaultAvatar.d.ts.map +1 -0
- package/dist/src/assets/DefaultAvatar.js +21 -0
- package/dist/src/assets/DefaultAvatar.js.map +1 -0
- package/dist/src/components/Dropdown/AccountAddress.d.ts +8 -0
- package/dist/src/components/Dropdown/AccountAddress.d.ts.map +1 -0
- package/dist/src/components/Dropdown/AccountAddress.js +66 -0
- package/dist/src/components/Dropdown/AccountAddress.js.map +1 -0
- package/dist/src/components/Dropdown/AccountAssets.d.ts +14 -0
- package/dist/src/components/Dropdown/AccountAssets.d.ts.map +1 -0
- package/dist/src/components/Dropdown/AccountAssets.js +44 -0
- package/dist/src/components/Dropdown/AccountAssets.js.map +1 -0
- package/dist/src/components/Dropdown/AccountBalance.d.ts +7 -0
- package/dist/src/components/Dropdown/AccountBalance.d.ts.map +1 -0
- package/dist/src/components/Dropdown/AccountBalance.js +18 -0
- package/dist/src/components/Dropdown/AccountBalance.js.map +1 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts +7 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.js +30 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -0
- package/dist/src/components/Dropdown/Content.d.ts +9 -0
- package/dist/src/components/Dropdown/Content.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Content.js +69 -0
- package/dist/src/components/Dropdown/Content.js.map +1 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts +7 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts.map +1 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.js +13 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.js.map +1 -0
- package/dist/src/components/Dropdown/Dropdown.d.ts +20 -0
- package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Dropdown.js +64 -0
- package/dist/src/components/Dropdown/Dropdown.js.map +1 -0
- package/dist/src/components/Dropdown/WelcomeBlock.d.ts +8 -0
- package/dist/src/components/Dropdown/WelcomeBlock.d.ts.map +1 -0
- package/dist/src/components/Dropdown/WelcomeBlock.js +44 -0
- package/dist/src/components/Dropdown/WelcomeBlock.js.map +1 -0
- package/dist/src/components/Dropdown/index.d.ts +3 -0
- package/dist/src/components/Dropdown/index.d.ts.map +1 -0
- package/dist/src/components/Dropdown/index.js +2 -0
- package/dist/src/components/Dropdown/index.js.map +1 -0
- package/dist/src/components/index.d.ts +2 -0
- package/dist/src/components/index.d.ts.map +1 -0
- package/dist/src/components/index.js +2 -0
- package/dist/src/components/index.js.map +1 -0
- package/dist/src/hooks/constants.d.ts +3 -3
- package/dist/src/hooks/constants.js +4 -4
- package/dist/src/hooks/constants.js.map +1 -1
- package/dist/src/hooks/index.d.ts +1 -4
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +1 -4
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/useAssetsUSDConversion.d.ts +8 -0
- package/dist/src/hooks/useAssetsUSDConversion.d.ts.map +1 -0
- package/dist/src/hooks/useAssetsUSDConversion.js +21 -0
- package/dist/src/hooks/useAssetsUSDConversion.js.map +1 -0
- package/dist/src/hooks/useAuthApiClient.d.ts +1 -1
- package/dist/src/hooks/useAuthApiClient.d.ts.map +1 -1
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts +73 -0
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -0
- package/dist/src/hooks/useAuthenticateWithWallet.js +69 -0
- package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -0
- package/dist/src/hooks/useCreateAccount.d.ts +41 -137
- package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
- package/dist/src/hooks/useCreateAccount.js +8 -10
- package/dist/src/hooks/useCreateAccount.js.map +1 -1
- package/dist/src/hooks/useCreateSession.d.ts +12 -182
- package/dist/src/hooks/useCreateSession.d.ts.map +1 -1
- package/dist/src/hooks/useCreateSession.js +12 -18
- package/dist/src/hooks/useCreateSession.js.map +1 -1
- package/dist/src/hooks/useDropdownData.d.ts +45 -0
- package/dist/src/hooks/useDropdownData.d.ts.map +1 -0
- package/dist/src/hooks/useDropdownData.js +74 -0
- package/dist/src/hooks/useDropdownData.js.map +1 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts +5 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts.map +1 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js +34 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js.map +1 -0
- package/dist/src/hooks/useGetAccountByAddress.d.ts +3 -5
- package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -1
- package/dist/src/hooks/useGetAccountByAddress.js +3 -4
- package/dist/src/hooks/useGetAccountByAddress.js.map +1 -1
- package/dist/src/hooks/useGetAccountByMezoId.d.ts +3 -5
- package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useGetAccountByMezoId.js +3 -4
- package/dist/src/hooks/useGetAccountByMezoId.js.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.d.ts +14 -8
- package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.js +28 -6
- package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
- package/dist/src/hooks/useGetSession.d.ts +3 -20
- package/dist/src/hooks/useGetSession.d.ts.map +1 -1
- package/dist/src/hooks/useGetSession.js +2 -2
- package/dist/src/hooks/useGetSession.js.map +1 -1
- package/dist/src/hooks/useLinkAccount.d.ts +15 -121
- package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
- package/dist/src/hooks/useLinkAccount.js +8 -21
- package/dist/src/hooks/useLinkAccount.js.map +1 -1
- package/dist/src/hooks/usePortalApiClient.d.ts +2 -0
- package/dist/src/hooks/usePortalApiClient.d.ts.map +1 -0
- package/dist/src/hooks/usePortalApiClient.js +6 -0
- package/dist/src/hooks/usePortalApiClient.js.map +1 -0
- package/dist/src/hooks/useSignInWithDiscord.d.ts +12 -181
- package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -1
- package/dist/src/hooks/useSignInWithDiscord.js +11 -22
- package/dist/src/hooks/useSignInWithDiscord.js.map +1 -1
- package/dist/src/hooks/useSignInWithWallet.d.ts +12 -181
- package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useSignInWithWallet.js +6 -43
- package/dist/src/hooks/useSignInWithWallet.js.map +1 -1
- package/dist/src/hooks/useSignOut.d.ts +12 -28
- package/dist/src/hooks/useSignOut.d.ts.map +1 -1
- package/dist/src/hooks/useSignOut.js +11 -6
- package/dist/src/hooks/useSignOut.js.map +1 -1
- package/dist/src/hooks/useSignUpWithWallet.d.ts +73 -0
- package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -0
- package/dist/src/hooks/useSignUpWithWallet.js +11 -0
- package/dist/src/hooks/useSignUpWithWallet.js.map +1 -0
- package/dist/src/hooks/useUpdateMezoId.d.ts +30 -82
- package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useUpdateMezoId.js +8 -12
- package/dist/src/hooks/useUpdateMezoId.js.map +1 -1
- package/dist/src/hooks/useWalletAccount.d.ts +12 -0
- package/dist/src/hooks/useWalletAccount.d.ts.map +1 -0
- package/dist/src/hooks/useWalletAccount.js +28 -0
- package/dist/src/hooks/useWalletAccount.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/provider.d.ts +10 -13
- package/dist/src/provider.d.ts.map +1 -1
- package/dist/src/provider.js +11 -20
- package/dist/src/provider.js.map +1 -1
- package/dist/src/utils/address.d.ts +15 -0
- package/dist/src/utils/address.d.ts.map +1 -0
- package/dist/src/utils/address.js +35 -0
- package/dist/src/utils/address.js.map +1 -0
- package/dist/src/utils/address.test.d.ts +2 -0
- package/dist/src/utils/address.test.d.ts.map +1 -0
- package/dist/src/utils/address.test.js +32 -0
- package/dist/src/utils/address.test.js.map +1 -0
- package/dist/src/utils/cryptoAssets.d.ts +28 -0
- package/dist/src/utils/cryptoAssets.d.ts.map +1 -0
- package/dist/src/utils/cryptoAssets.js +73 -0
- package/dist/src/utils/cryptoAssets.js.map +1 -0
- package/dist/src/utils/cryptoAssets.test.d.ts +2 -0
- package/dist/src/utils/cryptoAssets.test.d.ts.map +1 -0
- package/dist/src/utils/cryptoAssets.test.js +50 -0
- package/dist/src/utils/cryptoAssets.test.js.map +1 -0
- package/dist/src/utils/currency.d.ts +14 -0
- package/dist/src/utils/currency.d.ts.map +1 -0
- package/dist/src/utils/currency.js +27 -0
- package/dist/src/utils/currency.js.map +1 -0
- package/dist/src/utils/currency.test.d.ts +2 -0
- package/dist/src/utils/currency.test.d.ts.map +1 -0
- package/dist/src/utils/currency.test.js +34 -0
- package/dist/src/utils/currency.test.js.map +1 -0
- package/dist/src/utils/numbers.d.ts +26 -0
- package/dist/src/utils/numbers.d.ts.map +1 -0
- package/dist/src/utils/numbers.js +40 -0
- package/dist/src/utils/numbers.js.map +1 -0
- package/dist/src/utils/numbers.test.d.ts +2 -0
- package/dist/src/utils/numbers.test.d.ts.map +1 -0
- package/dist/src/utils/numbers.test.js +54 -0
- package/dist/src/utils/numbers.test.js.map +1 -0
- package/package.json +10 -4
- package/src/api/auth.ts +104 -129
- package/src/api/client.ts +78 -0
- package/src/api/fetch-error.ts +8 -0
- package/src/api/index.ts +2 -0
- package/src/api/portal.ts +56 -0
- package/src/assets/DefaultAvatar.tsx +74 -0
- package/src/components/Dropdown/AccountAddress.tsx +130 -0
- package/src/components/Dropdown/AccountAssets.tsx +110 -0
- package/src/components/Dropdown/AccountBalance.tsx +38 -0
- package/src/components/Dropdown/ConnectedTrigger.tsx +56 -0
- package/src/components/Dropdown/Content.tsx +148 -0
- package/src/components/Dropdown/DisconnectedTrigger.tsx +36 -0
- package/src/components/Dropdown/Dropdown.tsx +111 -0
- package/src/components/Dropdown/README.md +51 -0
- package/src/components/Dropdown/WelcomeBlock.tsx +92 -0
- package/src/components/Dropdown/index.ts +2 -0
- package/src/components/index.ts +1 -0
- package/src/hooks/constants.ts +4 -4
- package/src/hooks/index.ts +1 -4
- package/src/hooks/useAssetsUSDConversion.ts +31 -0
- package/src/hooks/useAuthenticateWithWallet.ts +98 -0
- package/src/hooks/useCreateAccount.ts +20 -11
- package/src/hooks/useCreateSession.ts +24 -18
- package/src/hooks/useDropdownData.ts +131 -0
- package/src/hooks/useEnsureNoSessionAndFetchNonce.ts +46 -0
- package/src/hooks/useGetAccountByAddress.ts +11 -5
- package/src/hooks/useGetAccountByMezoId.ts +11 -5
- package/src/hooks/useGetCurrentAccount.ts +53 -7
- package/src/hooks/useGetSession.ts +10 -3
- package/src/hooks/useLinkAccount.ts +19 -31
- package/src/hooks/usePortalApiClient.ts +6 -0
- package/src/hooks/useSignInWithDiscord.ts +19 -28
- package/src/hooks/useSignInWithWallet.ts +14 -54
- package/src/hooks/useSignOut.ts +22 -6
- package/src/hooks/useSignUpWithWallet.ts +21 -0
- package/src/hooks/useUpdateMezoId.ts +20 -12
- package/src/hooks/useWalletAccount.ts +53 -0
- package/src/index.ts +1 -0
- package/src/provider.ts +30 -37
- package/src/utils/address.test.ts +38 -0
- package/src/utils/address.ts +43 -0
- package/src/utils/cryptoAssets.test.ts +61 -0
- package/src/utils/cryptoAssets.ts +93 -0
- package/src/utils/currency.test.ts +38 -0
- package/src/utils/currency.ts +32 -0
- package/src/utils/numbers.test.ts +73 -0
- package/src/utils/numbers.ts +55 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a number or bigint into a human-readable string with K, M, B, or T suffix.
|
|
3
|
+
* @param value The number or bigint to format.
|
|
4
|
+
* @param decimals Number of decimal places to include (ignored for bigint to avoid floating point issues).
|
|
5
|
+
* @returns The formatted number as a string.
|
|
6
|
+
*/
|
|
7
|
+
export function formatHumanReadableNumber(value, decimals = 4) {
|
|
8
|
+
return value.toLocaleString("en-US", {
|
|
9
|
+
notation: "compact",
|
|
10
|
+
compactDisplay: "short",
|
|
11
|
+
maximumFractionDigits: decimals,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Returns a number rounded up to the desired number of decimals.
|
|
16
|
+
* @param amount The number to round up.
|
|
17
|
+
* @param desiredDecimals The number of decimals to round up to.
|
|
18
|
+
* @returns The rounded up number.
|
|
19
|
+
* @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L1-L3
|
|
20
|
+
*/
|
|
21
|
+
export function roundUpNumber(amount, desiredDecimals = 2) {
|
|
22
|
+
return Math.ceil(amount * 10 ** desiredDecimals) / 10 ** desiredDecimals;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Convert a fixed point bigint with precision `fixedPointDecimals` to a
|
|
26
|
+
* floating point number truncated to `desiredDecimals`.
|
|
27
|
+
* @param fixedPoint The fixed point bigint to convert.
|
|
28
|
+
* @param fixedPointDecimals The number of decimals in the fixed point bigint.
|
|
29
|
+
* @param desiredDecimals The number of decimals in the output number.
|
|
30
|
+
* @returns The floating point number truncated to `desiredDecimals`.
|
|
31
|
+
* @see https://github.com/tahowallet/extension/blob/f6b26b7cb4a0a0278b1fccc6319626638f1a6dfa/background/lib/fixed-point.ts#L216-L239
|
|
32
|
+
*/
|
|
33
|
+
export function fromFixedPoint(fixedPoint, fixedPointDecimals, desiredDecimals = 2) {
|
|
34
|
+
const fixedPointDesiredDecimalsAmount = fixedPoint /
|
|
35
|
+
10n ** BigInt(Math.max(1, fixedPointDecimals - desiredDecimals));
|
|
36
|
+
const formattedAmount = Number(fixedPointDesiredDecimalsAmount) /
|
|
37
|
+
10 ** Math.min(desiredDecimals, fixedPointDecimals);
|
|
38
|
+
return formattedAmount;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=numbers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numbers.js","sourceRoot":"","sources":["../../../src/utils/numbers.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CACvC,KAAsB,EACtB,QAAQ,GAAG,CAAC;IAEZ,OAAO,KAAK,CAAC,cAAc,CAAC,OAAO,EAAE;QACnC,QAAQ,EAAE,SAAS;QACnB,cAAc,EAAE,OAAO;QACvB,qBAAqB,EAAE,QAA2B;KACnD,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,eAAe,GAAG,CAAC;IAC/D,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,EAAE,IAAI,eAAe,CAAC,GAAG,EAAE,IAAI,eAAe,CAAA;AAC1E,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAC5B,UAAkB,EAClB,kBAA0B,EAC1B,eAAe,GAAG,CAAC;IAEnB,MAAM,+BAA+B,GACnC,UAAU;QACV,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,kBAAkB,GAAG,eAAe,CAAC,CAAC,CAAA;IAElE,MAAM,eAAe,GACnB,MAAM,CAAC,+BAA+B,CAAC;QACvC,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,eAAe,EAAE,kBAAkB,CAAC,CAAA;IAErD,OAAO,eAAe,CAAA;AACxB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numbers.test.d.ts","sourceRoot":"","sources":["../../../src/utils/numbers.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { formatHumanReadableNumber, fromFixedPoint, roundUpNumber, } from "./numbers";
|
|
2
|
+
describe("formatHumanReadableNumber", () => {
|
|
3
|
+
it("formats number less than 1000 with decimals", () => {
|
|
4
|
+
expect(formatHumanReadableNumber(123.456789, 2)).toBe("123.46");
|
|
5
|
+
});
|
|
6
|
+
it("formats thousands with K", () => {
|
|
7
|
+
expect(formatHumanReadableNumber(12_345, 2)).toBe("12.35K");
|
|
8
|
+
});
|
|
9
|
+
it("formats millions with M", () => {
|
|
10
|
+
expect(formatHumanReadableNumber(12_345_678, 2)).toBe("12.35M");
|
|
11
|
+
});
|
|
12
|
+
it("formats billions with B", () => {
|
|
13
|
+
expect(formatHumanReadableNumber(12_345_678_901, 2)).toBe("12.35B");
|
|
14
|
+
});
|
|
15
|
+
it("formats trillions with T", () => {
|
|
16
|
+
expect(formatHumanReadableNumber(12_345_678_901_234, 2)).toBe("12.35T");
|
|
17
|
+
});
|
|
18
|
+
it("formats bigint as readable number", () => {
|
|
19
|
+
expect(formatHumanReadableNumber(1234567890123456789n, 2)).toBe("1,234,567.89T");
|
|
20
|
+
});
|
|
21
|
+
it("caps at > 1000T for huge numbers", () => {
|
|
22
|
+
const hugeNumber = 10 ** 18; // 1e18
|
|
23
|
+
expect(formatHumanReadableNumber(hugeNumber, 2)).toBe("1,000,000T");
|
|
24
|
+
});
|
|
25
|
+
it("formats numbers with trailing zeros correctly", () => {
|
|
26
|
+
expect(formatHumanReadableNumber(1_000, 2)).toBe("1K");
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
describe("roundUpNumber", () => {
|
|
30
|
+
it("rounds up to 2 decimals", () => {
|
|
31
|
+
expect(roundUpNumber(1.234)).toBe(1.24);
|
|
32
|
+
});
|
|
33
|
+
it("rounds up to 3 decimals", () => {
|
|
34
|
+
expect(roundUpNumber(9.87654, 3)).toBe(9.877);
|
|
35
|
+
});
|
|
36
|
+
it("rounds up exact decimal", () => {
|
|
37
|
+
expect(roundUpNumber(2.5, 1)).toBe(2.5);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
describe("fromFixedPoint", () => {
|
|
41
|
+
it("converts fixed point with 18 decimals to float", () => {
|
|
42
|
+
const raw = 1234567890000000000n; // 1.23456789 ETH in 18 decimals
|
|
43
|
+
expect(fromFixedPoint(raw, 18, 4)).toBe(1.2345);
|
|
44
|
+
});
|
|
45
|
+
it("converts with truncation to 2 decimals", () => {
|
|
46
|
+
const raw = 987654321000000000n; // ~0.987654321 ETH
|
|
47
|
+
expect(fromFixedPoint(raw, 18, 2)).toBe(0.98);
|
|
48
|
+
});
|
|
49
|
+
it("handles large fixedPoint decimals", () => {
|
|
50
|
+
const raw = 1000000000000000000000000n; // 1M with 6 decimals
|
|
51
|
+
expect(fromFixedPoint(raw, 6, 2)).toBe(1000000000000000000);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
//# sourceMappingURL=numbers.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numbers.test.js","sourceRoot":"","sources":["../../../src/utils/numbers.test.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mezo-org/passport",
|
|
3
|
-
"version": "0.4.0-dev.
|
|
3
|
+
"version": "0.4.0-dev.11",
|
|
4
4
|
"main": "dist/src/index.js",
|
|
5
5
|
"license": "GPL-3.0-only",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "tsc",
|
|
8
|
+
"dev": "tsc --watch",
|
|
8
9
|
"format": "npm run lint:js && npm run lint:config",
|
|
9
10
|
"format:fix": "npm run lint:js:fix && npm run lint:config:fix",
|
|
10
11
|
"lint:js": "eslint .",
|
|
@@ -23,24 +24,29 @@
|
|
|
23
24
|
"directory": "typescript/passport"
|
|
24
25
|
},
|
|
25
26
|
"devDependencies": {
|
|
26
|
-
"@babel/preset-env": "^7.23.7",
|
|
27
27
|
"@thesis-co/eslint-config": "github:thesis/eslint-config#7b9bc8c",
|
|
28
28
|
"@types/jest": "^29.5.12",
|
|
29
29
|
"@types/node": "^20.11.30",
|
|
30
30
|
"@types/react": "^18.2.64",
|
|
31
31
|
"eslint": "^8.57.1",
|
|
32
32
|
"jest": "^29.7.0",
|
|
33
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
33
34
|
"prettier": "^3.2.5",
|
|
34
35
|
"ts-jest": "^29.1.2",
|
|
35
36
|
"ts-node": "^10.9.2",
|
|
36
37
|
"typescript": "^5.4.3"
|
|
37
38
|
},
|
|
38
39
|
"dependencies": {
|
|
39
|
-
"@mezo-org/
|
|
40
|
+
"@mezo-org/mezo-clay": "0.1.0-dev.18",
|
|
41
|
+
"@mezo-org/orangekit": "1.0.0-beta.40-dev.1",
|
|
42
|
+
"@mezo-org/sign-in-with-wallet": "1.0.0-beta.8"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
40
45
|
"@rainbow-me/rainbowkit": "2.0.2",
|
|
41
46
|
"@tanstack/react-query": "^5.28.4",
|
|
42
|
-
"@mezo-org/sign-in-with-wallet": "1.0.0-beta.8",
|
|
43
47
|
"react": "^18.2.0",
|
|
48
|
+
"styletron-engine-monolithic": "^1.0.0",
|
|
49
|
+
"usehooks-ts": "^3.1.1",
|
|
44
50
|
"viem": "2.22.8",
|
|
45
51
|
"wagmi": "2.5.12"
|
|
46
52
|
}
|
package/src/api/auth.ts
CHANGED
|
@@ -1,152 +1,110 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
queryParams?: Record<string, string | undefined>
|
|
4
|
-
}
|
|
5
|
-
type PassportIdentityProvider = "wallet" | "discord" | "email"
|
|
1
|
+
import { ApiClient } from "./client"
|
|
2
|
+
import { FetchError } from "./fetch-error"
|
|
6
3
|
|
|
7
|
-
type
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
type AuthProvider = "discord"
|
|
5
|
+
|
|
6
|
+
export type LinkedWalletAccount = {
|
|
7
|
+
type: "wallet"
|
|
8
|
+
btcAddress?: string
|
|
10
9
|
evmAddress: string
|
|
11
10
|
}
|
|
12
11
|
|
|
12
|
+
export type LinkedProviderAccount = {
|
|
13
|
+
type: AuthProvider
|
|
14
|
+
name: string
|
|
15
|
+
id: string
|
|
16
|
+
avatarUrl: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type LinkedAccount = LinkedWalletAccount | LinkedProviderAccount
|
|
20
|
+
|
|
13
21
|
type Account = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
user_metadata: AccountPublicData
|
|
22
|
+
mezoId: string
|
|
23
|
+
hasModifiedMezoId: boolean
|
|
24
|
+
linkedAccounts: LinkedAccount[]
|
|
19
25
|
}
|
|
20
26
|
|
|
21
|
-
type Session = {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
export type Session = {
|
|
28
|
+
expiresIn: number
|
|
29
|
+
expiresAt: number
|
|
30
|
+
sessionLinks: {
|
|
31
|
+
_links: {
|
|
32
|
+
account: string
|
|
33
|
+
}
|
|
34
|
+
}
|
|
28
35
|
}
|
|
29
36
|
|
|
30
|
-
type AuthenticationProviderRedirectResponse = {
|
|
37
|
+
export type AuthenticationProviderRedirectResponse = {
|
|
31
38
|
redirectTo: string
|
|
32
39
|
}
|
|
33
40
|
|
|
34
|
-
type
|
|
35
|
-
type: "wallet"
|
|
41
|
+
type SignatureData = {
|
|
36
42
|
message: string
|
|
37
43
|
signature: string
|
|
38
44
|
}
|
|
39
45
|
|
|
46
|
+
// Request and Response types
|
|
47
|
+
export type GetSessionResponse = Session | { nonce: string }
|
|
48
|
+
|
|
40
49
|
export type CreateSessionRequest =
|
|
41
|
-
|
|
|
50
|
+
| ({ type: "wallet" } & SignatureData)
|
|
42
51
|
| { type: "discord" }
|
|
52
|
+
export type CreateSessionResponse =
|
|
53
|
+
| Session
|
|
54
|
+
| AuthenticationProviderRedirectResponse
|
|
43
55
|
|
|
44
|
-
export type
|
|
56
|
+
export type DeleteSessionResponse = { message: string }
|
|
45
57
|
|
|
46
|
-
export type
|
|
58
|
+
export type GetCurrentAccountResponse = Partial<Account> | null
|
|
47
59
|
|
|
48
|
-
export
|
|
49
|
-
|
|
50
|
-
|
|
60
|
+
export type GetAccountByMezoIdOrAddressResponse = {
|
|
61
|
+
mezoId?: string
|
|
62
|
+
linkedAccounts?: Extract<LinkedAccount, { type: "wallet" }>[]
|
|
51
63
|
}
|
|
52
64
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
constructor(apiUrl?: string) {
|
|
59
|
-
this.apiUrl = apiUrl ?? DEFAULT_API_ENDPOINT
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* The request handler for the Passport API. It accepts an API endpoint and
|
|
64
|
-
* request options.
|
|
65
|
-
* @dev Generic type `D` is a type of returned object from the function
|
|
66
|
-
* @param endpoint - The API endpoint to request. Should start with
|
|
67
|
-
* forward slash ("/")
|
|
68
|
-
* @param {RequestHandlerOptions<B>} options - The request options, the
|
|
69
|
-
* `RequestInit` type with additional type-safe properties
|
|
70
|
-
* @returns The promise of the API response
|
|
71
|
-
*/
|
|
72
|
-
protected async handleRequest<D>(
|
|
73
|
-
endpoint: `/${string}`,
|
|
74
|
-
options: RequestHandlerOptions,
|
|
75
|
-
): Promise<D> {
|
|
76
|
-
const {
|
|
77
|
-
method,
|
|
78
|
-
headers,
|
|
79
|
-
body,
|
|
80
|
-
queryParams = {},
|
|
81
|
-
...restOptions
|
|
82
|
-
} = options || {}
|
|
83
|
-
|
|
84
|
-
// If last character in apiUrl is forward slash we are removing it
|
|
85
|
-
const authApiUrlRoute = this.apiUrl.replace(/\/$/, "")
|
|
86
|
-
const url = new URL(`${authApiUrlRoute}${endpoint}`)
|
|
87
|
-
|
|
88
|
-
// Removes falsy values (e.g. null, undefined etc.) from query params and
|
|
89
|
-
// appends remaining ones to the URL
|
|
90
|
-
Object.entries(queryParams)
|
|
91
|
-
.filter(([, value]) => value) // Remove falsy values
|
|
92
|
-
.forEach(([key, value]) => url.searchParams.append(key, value!))
|
|
93
|
-
|
|
94
|
-
try {
|
|
95
|
-
const response = await fetch(url, {
|
|
96
|
-
method,
|
|
97
|
-
credentials: "include",
|
|
98
|
-
headers: {
|
|
99
|
-
"Content-Type": "application/json",
|
|
100
|
-
...headers,
|
|
101
|
-
},
|
|
102
|
-
body: body ? JSON.stringify(body) : undefined,
|
|
103
|
-
...restOptions,
|
|
104
|
-
})
|
|
65
|
+
export type LinkAccountRequest = CreateSessionRequest
|
|
66
|
+
export type LinkAccountResponse =
|
|
67
|
+
| Account
|
|
68
|
+
| AuthenticationProviderRedirectResponse
|
|
105
69
|
|
|
106
|
-
|
|
70
|
+
export type CreateAccountRequest = { type: "wallet" } & SignatureData
|
|
71
|
+
export type CreateAccountResponse = Account
|
|
107
72
|
|
|
108
|
-
|
|
109
|
-
const error = data?.error || "An error occurred"
|
|
110
|
-
throw new Error(error)
|
|
111
|
-
}
|
|
73
|
+
export type UpdateMezoIdResponse = Account
|
|
112
74
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
75
|
+
export const AUTH_API_ENDPOINTS_BY_ENV = {
|
|
76
|
+
mainnet: "https://api.mezo.org",
|
|
77
|
+
testnet: "https://api.test.mezo.org",
|
|
78
|
+
}
|
|
117
79
|
|
|
118
|
-
|
|
119
|
-
|
|
80
|
+
export class AuthApiClient extends ApiClient {
|
|
81
|
+
constructor(
|
|
82
|
+
environment: keyof typeof AUTH_API_ENDPOINTS_BY_ENV = "mainnet",
|
|
83
|
+
apiUrl?: string,
|
|
84
|
+
) {
|
|
85
|
+
super(apiUrl ?? AUTH_API_ENDPOINTS_BY_ENV[environment])
|
|
120
86
|
}
|
|
121
87
|
|
|
122
88
|
async getSession(code?: string) {
|
|
123
|
-
const data = await this.handleRequest<
|
|
124
|
-
"
|
|
125
|
-
{
|
|
126
|
-
|
|
127
|
-
queryParams: { code },
|
|
128
|
-
},
|
|
129
|
-
)
|
|
89
|
+
const data = await this.handleRequest<GetSessionResponse>("/session", {
|
|
90
|
+
method: "GET",
|
|
91
|
+
queryParams: { code },
|
|
92
|
+
})
|
|
130
93
|
|
|
131
94
|
return data
|
|
132
95
|
}
|
|
133
96
|
|
|
134
97
|
async createSession(createSessionRequest: CreateSessionRequest) {
|
|
135
|
-
const data = await this.handleRequest<
|
|
136
|
-
"
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
body: createSessionRequest,
|
|
140
|
-
},
|
|
141
|
-
)
|
|
98
|
+
const data = await this.handleRequest<CreateSessionResponse>("/session", {
|
|
99
|
+
method: "POST",
|
|
100
|
+
body: createSessionRequest,
|
|
101
|
+
})
|
|
142
102
|
|
|
143
103
|
return data
|
|
144
104
|
}
|
|
145
105
|
|
|
146
106
|
async deleteSession() {
|
|
147
|
-
const data = await this.handleRequest<{
|
|
148
|
-
message: string
|
|
149
|
-
}>("/session", {
|
|
107
|
+
const data = await this.handleRequest<DeleteSessionResponse>("/session", {
|
|
150
108
|
method: "DELETE",
|
|
151
109
|
})
|
|
152
110
|
|
|
@@ -154,37 +112,51 @@ export class AuthApiClient {
|
|
|
154
112
|
}
|
|
155
113
|
|
|
156
114
|
async getCurrentAccount() {
|
|
157
|
-
const data = await this.handleRequest<
|
|
158
|
-
|
|
159
|
-
|
|
115
|
+
const data = await this.handleRequest<GetCurrentAccountResponse>(
|
|
116
|
+
"/session/account",
|
|
117
|
+
{
|
|
118
|
+
method: "GET",
|
|
119
|
+
},
|
|
120
|
+
)
|
|
160
121
|
|
|
161
122
|
return data
|
|
162
123
|
}
|
|
163
124
|
|
|
164
125
|
async getAccountByMezoIdOrAddress(mezoIdOrAddress: string) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
126
|
+
let data
|
|
127
|
+
try {
|
|
128
|
+
data = await this.handleRequest<GetAccountByMezoIdOrAddressResponse>(
|
|
129
|
+
`/accounts/${mezoIdOrAddress}`,
|
|
130
|
+
{
|
|
131
|
+
method: "GET",
|
|
132
|
+
},
|
|
133
|
+
)
|
|
134
|
+
} catch (error) {
|
|
135
|
+
// Return null if account is not found instead of throwing an error
|
|
136
|
+
if (error instanceof FetchError && error.status === 404) {
|
|
137
|
+
data = null
|
|
138
|
+
} else {
|
|
139
|
+
throw error
|
|
140
|
+
}
|
|
141
|
+
}
|
|
171
142
|
|
|
172
143
|
return data
|
|
173
144
|
}
|
|
174
145
|
|
|
175
146
|
async linkAccount(linkAccountRequest: LinkAccountRequest) {
|
|
176
|
-
const data = await this.handleRequest<
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
147
|
+
const data = await this.handleRequest<LinkAccountResponse>(
|
|
148
|
+
"/session/account/linked-accounts",
|
|
149
|
+
{
|
|
150
|
+
method: "POST",
|
|
151
|
+
body: linkAccountRequest,
|
|
152
|
+
},
|
|
153
|
+
)
|
|
182
154
|
|
|
183
155
|
return data
|
|
184
156
|
}
|
|
185
157
|
|
|
186
158
|
async createAccount(createAccountRequest: CreateAccountRequest) {
|
|
187
|
-
const data = await this.handleRequest<
|
|
159
|
+
const data = await this.handleRequest<CreateAccountResponse>("/accounts", {
|
|
188
160
|
method: "POST",
|
|
189
161
|
body: createAccountRequest,
|
|
190
162
|
})
|
|
@@ -193,10 +165,13 @@ export class AuthApiClient {
|
|
|
193
165
|
}
|
|
194
166
|
|
|
195
167
|
async updateMezoId(newMezoId: string) {
|
|
196
|
-
const data = await this.handleRequest<
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
168
|
+
const data = await this.handleRequest<UpdateMezoIdResponse>(
|
|
169
|
+
"/session/account",
|
|
170
|
+
{
|
|
171
|
+
method: "PATCH",
|
|
172
|
+
body: { mezoId: newMezoId },
|
|
173
|
+
},
|
|
174
|
+
)
|
|
200
175
|
|
|
201
176
|
return data
|
|
202
177
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { FetchError } from "./fetch-error"
|
|
2
|
+
|
|
3
|
+
type RequestHandlerOptions = Omit<RequestInit, "credentials" | "body"> & {
|
|
4
|
+
body?: Record<string, unknown>
|
|
5
|
+
queryParams?: Record<string, string | undefined>
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export abstract class ApiClient {
|
|
9
|
+
private apiUrl: string
|
|
10
|
+
|
|
11
|
+
constructor(apiUrl: string) {
|
|
12
|
+
this.apiUrl = apiUrl
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The request handler for the Passport API. It accepts an API endpoint and
|
|
17
|
+
* request options.
|
|
18
|
+
* @dev Generic type `D` is a type of returned object from the function
|
|
19
|
+
* @dev WARNING: This function hardcodes `credentials: "include"`, meaning
|
|
20
|
+
* cookies and authentication headers will be sent with every request. Ensure
|
|
21
|
+
* this is safe if you plan to use it in other contexts than auth api worker.
|
|
22
|
+
* @param endpoint - The API endpoint to request. Should start with
|
|
23
|
+
* forward slash ("/")
|
|
24
|
+
* @param options - The request options, the `RequestInit` type with
|
|
25
|
+
* additional type-safe properties
|
|
26
|
+
* @returns The promise of the API response
|
|
27
|
+
*/
|
|
28
|
+
protected async handleRequest<D>(
|
|
29
|
+
endpoint: `/${string}`,
|
|
30
|
+
options: RequestHandlerOptions = {},
|
|
31
|
+
): Promise<D> {
|
|
32
|
+
try {
|
|
33
|
+
const { method, body, queryParams = {}, ...restOptions } = options
|
|
34
|
+
|
|
35
|
+
// If last character in apiUrl is forward slash we are removing it
|
|
36
|
+
const authApiUrlRoute = this.apiUrl.replace(/\/$/, "")
|
|
37
|
+
const url = new URL(`${authApiUrlRoute}${endpoint}`)
|
|
38
|
+
|
|
39
|
+
// Removes falsy values (e.g. null, undefined etc.) from query params and
|
|
40
|
+
// appends remaining ones to the URL
|
|
41
|
+
Object.entries(queryParams)
|
|
42
|
+
.filter(([, value]) => value) // Remove falsy values
|
|
43
|
+
.forEach(([key, value]) => url.searchParams.append(key, value!))
|
|
44
|
+
|
|
45
|
+
const response = await fetch(url, {
|
|
46
|
+
method,
|
|
47
|
+
credentials: "include",
|
|
48
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
49
|
+
...restOptions,
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const data = await response.json()
|
|
53
|
+
|
|
54
|
+
if (!response.ok || data?.error) {
|
|
55
|
+
const error =
|
|
56
|
+
data?.error ||
|
|
57
|
+
`An error occurred when handling API request: ${JSON.stringify(data)}`
|
|
58
|
+
throw new FetchError(
|
|
59
|
+
response.status,
|
|
60
|
+
`Error [${response.status}]: ${error}`,
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return data as D
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (error instanceof FetchError) {
|
|
67
|
+
throw error
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const errorMessage =
|
|
71
|
+
error instanceof Error ? error.message : JSON.stringify(error)
|
|
72
|
+
throw new FetchError(
|
|
73
|
+
500,
|
|
74
|
+
`Error [500]: An error occurred when handling API request: ${errorMessage}`,
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
package/src/api/index.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { ApiClient } from "./client"
|
|
2
|
+
|
|
3
|
+
type PortalStatistics = {
|
|
4
|
+
hodlers: number
|
|
5
|
+
depositedBitcoin: string
|
|
6
|
+
tvl: number
|
|
7
|
+
currentUsdPerBtc: string
|
|
8
|
+
currentUsdPerEth: string
|
|
9
|
+
usdPerBtc24hChange: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type PortalMats = {
|
|
13
|
+
address: string
|
|
14
|
+
totalMats: number
|
|
15
|
+
matsFromDeposits: number
|
|
16
|
+
matsFromReferrals: number
|
|
17
|
+
matsFromSecondaryReferrals: number
|
|
18
|
+
netMatsTransferred: number
|
|
19
|
+
dailyMats: number
|
|
20
|
+
dailyMatsFromDeposits: number
|
|
21
|
+
dailyMatsFromReferrals: number
|
|
22
|
+
dailyMatsFromSecondaryReferrals: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const PORTAL_API_ENDPOINTS_BY_ENV = {
|
|
26
|
+
mainnet: "https://api.mezo.org/api/v1",
|
|
27
|
+
testnet: "https://api.test.mezo.org/api/v1",
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class PortalApiClient extends ApiClient {
|
|
31
|
+
constructor(
|
|
32
|
+
environment: keyof typeof PORTAL_API_ENDPOINTS_BY_ENV = "mainnet",
|
|
33
|
+
apiUrl?: string,
|
|
34
|
+
) {
|
|
35
|
+
super(apiUrl ?? PORTAL_API_ENDPOINTS_BY_ENV[environment])
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async getPortalStatistics() {
|
|
39
|
+
const data = await this.handleRequest<PortalStatistics>("/statistics", {
|
|
40
|
+
method: "GET",
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
return data
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async getPortalMats(address: string) {
|
|
47
|
+
const data = await this.handleRequest<PortalMats>(
|
|
48
|
+
`/accounts/${address}/mats`,
|
|
49
|
+
{
|
|
50
|
+
method: "GET",
|
|
51
|
+
},
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
return data
|
|
55
|
+
}
|
|
56
|
+
}
|