@otoplo/wallet-common 0.6.1 → 0.8.0
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 +88 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +233 -126
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
- package/src/services/asset.ts +3 -3
- package/src/services/market.ts +4 -1
- package/src/services/rostrum.ts +322 -274
- package/src/services/transaction.ts +2 -2
- package/src/services/wallet.ts +7 -3
- package/src/types/rostrum.types.ts +11 -0
- package/src/utils/amount.ts +112 -0
- package/src/utils/common.ts +202 -183
- package/src/utils/index.ts +1 -0
- package/src/utils/vault.ts +6 -5
|
@@ -7,6 +7,17 @@ export interface RostrumParams {
|
|
|
7
7
|
label: string;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Whether the wallet can use a server, from `RostrumService.checkInstance()`:
|
|
12
|
+
* - `online`: reachable and usable, with the round-trip time of a request in milliseconds.
|
|
13
|
+
* - `offline`: couldn't connect, or it didn't answer.
|
|
14
|
+
* - `wrong_network`: it serves another network (e.g. testnet while the wallet is on mainnet).
|
|
15
|
+
* - `unsupported`: it doesn't index scripts (`script_index`), which the wallet needs.
|
|
16
|
+
*/
|
|
17
|
+
export type RostrumInstanceCheck =
|
|
18
|
+
| { status: 'online'; latency: number }
|
|
19
|
+
| { status: 'offline' | 'wrong_network' | 'unsupported' };
|
|
20
|
+
|
|
10
21
|
export interface ServerFeatures {
|
|
11
22
|
genesis_hash: string;
|
|
12
23
|
server_version: string;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { UnitType, UnitUtils } from "libnexa-ts";
|
|
2
|
+
import { type CurrencyCode, getNexaTicker } from "./common";
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Display formatting for amounts. Amounts are bigint base units (satoshis, token units) and stay that way
|
|
6
|
+
* for any math (sums, fees, balance checks) — these helpers only turn them into text.
|
|
7
|
+
*
|
|
8
|
+
* Formatting goes through `Number` and `Intl.NumberFormat`: Hermes' `Intl` throws on a bigint and turns a decimal
|
|
9
|
+
* string into a `Number` anyway. A `Number` shows the exact digits while the amount, counted in its smallest
|
|
10
|
+
* *shown* unit, is below 2^52 (~4.5 quadrillion) — 45 trillion with 2 decimals shown, 45 billion with 5,
|
|
11
|
+
* 45 million with 8. Above that, the last digit or two come out rounded. NEXA (21 trillion max supply,
|
|
12
|
+
* 2 decimals) is always exact.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** Cached `Intl.NumberFormat`s: creating one is slow (on Hermes especially), formatting with one is fast. */
|
|
16
|
+
const numberFormatters = new Map<string, Intl.NumberFormat>();
|
|
17
|
+
|
|
18
|
+
function getNumberFormatter(locale: string, options: Intl.NumberFormatOptions): Intl.NumberFormat {
|
|
19
|
+
const key = `${locale}|${JSON.stringify(options)}`;
|
|
20
|
+
let formatter = numberFormatters.get(key);
|
|
21
|
+
if (!formatter) {
|
|
22
|
+
formatter = new Intl.NumberFormat(locale, options);
|
|
23
|
+
numberFormatters.set(key, formatter);
|
|
24
|
+
}
|
|
25
|
+
return formatter;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface FormatAmountOptions {
|
|
29
|
+
/**
|
|
30
|
+
* Decimals shown at most — the rest are cut off, not rounded. Default 5; pass the token's `decimals` to show all.
|
|
31
|
+
*
|
|
32
|
+
* Each extra decimal shown lowers the exact range tenfold (see above): showing all decimals is exact up to
|
|
33
|
+
* 45 million tokens with 8 decimals, but only up to 0.0045 with 18. Where every digit must be right
|
|
34
|
+
* (e.g. a send confirmation), use `UnitUtils.formatUnits(amount, decimals)` — exact, but not grouped or localized.
|
|
35
|
+
*/
|
|
36
|
+
maxDecimals?: number;
|
|
37
|
+
/** Grouping, separators and digits. Default `en-US`, like `calculateFiatValue`. */
|
|
38
|
+
locale?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* An amount in base units (token units, satoshis) for display, without trailing zeros:
|
|
43
|
+
* `formatTokenAmount(123456789n, 2)` → `1,234,567.89`, `formatTokenAmount(100n, 2)` → `1`.
|
|
44
|
+
*
|
|
45
|
+
* Decimals beyond `maxDecimals` are cut off in bigint, so a balance never shows more than it holds
|
|
46
|
+
* (`0.999999` → `0.99999`, not `1`). A non-zero amount that would show as 0 becomes `<0.00001`.
|
|
47
|
+
*/
|
|
48
|
+
export function formatTokenAmount(
|
|
49
|
+
amount: bigint | string,
|
|
50
|
+
decimals: number,
|
|
51
|
+
{ maxDecimals = 5, locale = 'en-US' }: FormatAmountOptions = {},
|
|
52
|
+
): string {
|
|
53
|
+
const units = BigInt(amount);
|
|
54
|
+
const shown = Math.min(decimals, maxDecimals);
|
|
55
|
+
// Bigint division truncates toward zero: the hidden decimals are dropped, for negatives too.
|
|
56
|
+
const cut = units / 10n ** BigInt(decimals - shown);
|
|
57
|
+
const formatter = getNumberFormatter(locale, { maximumFractionDigits: shown });
|
|
58
|
+
|
|
59
|
+
if (cut === 0n && units !== 0n) {
|
|
60
|
+
return `${units < 0n ? '-' : ''}<${formatter.format(10 ** -shown)}`;
|
|
61
|
+
}
|
|
62
|
+
// Parsing the exact decimal string rounds once; Intl's rounding to `shown` decimals then snaps it back.
|
|
63
|
+
return formatter.format(Number(UnitUtils.formatUnits(cut, shown)));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A token amount with its ticker, e.g. `1,500.25 USDT`.
|
|
68
|
+
* Tokens without a ticker (it's optional in the token's genesis) show the amount alone.
|
|
69
|
+
*/
|
|
70
|
+
export function formatTokenAmountWithTicker(
|
|
71
|
+
amount: bigint | string,
|
|
72
|
+
decimals: number,
|
|
73
|
+
ticker: string,
|
|
74
|
+
options: FormatAmountOptions = {},
|
|
75
|
+
): string {
|
|
76
|
+
const formatted = formatTokenAmount(amount, decimals, options);
|
|
77
|
+
return ticker ? `${formatted} ${ticker}` : formatted;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Satoshis as a NEXA amount: `1,234,567.5`. Negative amounts keep their minus sign. Always exact (see above). */
|
|
81
|
+
export function formatNexaAmount(sats: bigint | string, options: FormatAmountOptions = {}): string {
|
|
82
|
+
return formatTokenAmount(sats, UnitType.NEXA, options);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Satoshis as a NEXA amount with the ticker: `1,234,567.5 NEXA` (`tNEXA` on testnet). */
|
|
86
|
+
export function formatNexaAmountWithTicker(sats: bigint | string, options: FormatAmountOptions = {}): string {
|
|
87
|
+
return formatTokenAmountWithTicker(sats, UnitType.NEXA, getNexaTicker(), options);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Fiat value of an amount in base units (satoshis, token units), e.g. `$1,234.56`.
|
|
92
|
+
* A non-zero value that would round to zero shows as below the currency's smallest unit: `<$0.01`, `<¥1`.
|
|
93
|
+
* Exact to the cent below ~$10 trillion.
|
|
94
|
+
*/
|
|
95
|
+
export function calculateFiatValue(
|
|
96
|
+
amount: bigint | string,
|
|
97
|
+
decimals: number,
|
|
98
|
+
price: number,
|
|
99
|
+
currency: CurrencyCode,
|
|
100
|
+
locale = 'en-US',
|
|
101
|
+
): string {
|
|
102
|
+
const formatter = getNumberFormatter(locale, { style: 'currency', currency });
|
|
103
|
+
const value = Number(UnitUtils.formatUnits(amount, decimals)) * price;
|
|
104
|
+
|
|
105
|
+
// The currency's smallest unit, from its decimals: 0.01 for most, 1 for yen.
|
|
106
|
+
const smallest = 10 ** -(formatter.resolvedOptions().maximumFractionDigits ?? 2);
|
|
107
|
+
// Below half of it, the formatted value would round to zero.
|
|
108
|
+
if (value !== 0 && Math.abs(value) < smallest / 2) {
|
|
109
|
+
return `${value < 0 ? '-' : ''}<${formatter.format(smallest)}`;
|
|
110
|
+
}
|
|
111
|
+
return formatter.format(value);
|
|
112
|
+
}
|
package/src/utils/common.ts
CHANGED
|
@@ -1,183 +1,202 @@
|
|
|
1
|
-
import { Address, AddressType, BufferUtils, CommonUtils, Networks } from "libnexa-ts";
|
|
2
|
-
import type { Balance } from "../types/wallet.types";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
export
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
return
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export function
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
1
|
+
import { Address, AddressType, BufferUtils, CommonUtils, Networks } from "libnexa-ts";
|
|
2
|
+
import type { Balance } from "../types/wallet.types";
|
|
3
|
+
|
|
4
|
+
export const HD_WALLET_ID = -1;
|
|
5
|
+
export const MAX_INT64 = 9223372036854775807n;
|
|
6
|
+
|
|
7
|
+
/** Nexa's target block time, in minutes. */
|
|
8
|
+
export const BLOCK_TIME_MINUTES = 2;
|
|
9
|
+
|
|
10
|
+
export const CURRENCIES = {
|
|
11
|
+
usd: { symbol: '$', name: 'US Dollar' },
|
|
12
|
+
eur: { symbol: '€', name: 'Euro' },
|
|
13
|
+
gbp: { symbol: '£', name: 'British Pound' },
|
|
14
|
+
aud: { symbol: 'A$', name: 'Australian Dollar' },
|
|
15
|
+
cad: { symbol: 'CA$', name: 'Canadian Dollar' },
|
|
16
|
+
chf: { symbol: 'CHF', name: 'Swiss Franc' },
|
|
17
|
+
inr: { symbol: '₹', name: 'Indian Rupee' },
|
|
18
|
+
cny: { symbol: 'CN¥', name: 'Chinese Yuan' },
|
|
19
|
+
jpy: { symbol: '¥', name: 'Japanese Yen' },
|
|
20
|
+
} as const satisfies Record<string, { symbol: string; name: string }>;
|
|
21
|
+
|
|
22
|
+
export type CurrencyCode = keyof typeof CURRENCIES;
|
|
23
|
+
|
|
24
|
+
export const currencyCodes = Object.keys(CURRENCIES) as CurrencyCode[];
|
|
25
|
+
|
|
26
|
+
export function isTestnet(): boolean {
|
|
27
|
+
return Networks.defaultNetwork == Networks.testnet;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** `NEXA` on mainnet, `tNEXA` on testnet. */
|
|
31
|
+
export function getNexaTicker(): string {
|
|
32
|
+
return isTestnet() ? 'tNEXA' : 'NEXA';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function isGenesisHashValid(hash: string): boolean {
|
|
36
|
+
if(isTestnet()) {
|
|
37
|
+
return hash == "508c843a4b98fb25f57cf9ebafb245a5c16468f06519cdd467059a91e7b79d52";
|
|
38
|
+
}
|
|
39
|
+
return hash == "edc7144fe1ba4edd0edf35d7eea90f6cb1dba42314aa85da8207e97c5339c801";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function isValidNexaAddress(address: string, type = AddressType.PayToScriptTemplate): boolean {
|
|
43
|
+
return Address.isValid(address, Networks.defaultNetwork, type);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function getExplorerUrl(): string {
|
|
47
|
+
return `https://${(isTestnet() ? 'testnet-' : '')}explorer.nexa.org`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function currentTimestamp(): number {
|
|
51
|
+
return Math.floor(Date.now() / 1000);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Estimated minutes until `future` block, from the block time. 0 once it's reached. */
|
|
55
|
+
export function estimateMinutesToBlock(current: number, future: number): number {
|
|
56
|
+
return Math.max(future - current, 0) * BLOCK_TIME_MINUTES;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Estimated date of a future block, e.g. `Mar 14, 2027`.
|
|
61
|
+
* `locale` and `options` go to `toLocaleDateString` — defaults to `en-US` like `calculateFiatValue`.
|
|
62
|
+
*/
|
|
63
|
+
export function estimateDateByFutureBlock(
|
|
64
|
+
current: number,
|
|
65
|
+
future: number,
|
|
66
|
+
locale = 'en-US',
|
|
67
|
+
options: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric', year: 'numeric' },
|
|
68
|
+
): string {
|
|
69
|
+
const time = new Date(Date.now() + estimateMinutesToBlock(current, future) * 60_000);
|
|
70
|
+
return time.toLocaleDateString(locale, options);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function isNullOrEmpty(arg?: string | any[] | null): arg is undefined | [] | null | '' {
|
|
74
|
+
return !arg || arg.length === 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function truncateStringMiddle(str?: string, maxLength = 0): string {
|
|
78
|
+
if (!str || str.length <= maxLength) {
|
|
79
|
+
return str || '';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const ellipsis = '...';
|
|
83
|
+
const halfLength = Math.floor((maxLength - ellipsis.length) / 2);
|
|
84
|
+
const firstHalf = str.slice(0, halfLength);
|
|
85
|
+
const secondHalf = str.slice(str.length - halfLength);
|
|
86
|
+
|
|
87
|
+
return firstHalf + ellipsis + secondHalf;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export function capitalizeFirstLetter(str: string): string {
|
|
91
|
+
if (str.length === 0) {
|
|
92
|
+
return "";
|
|
93
|
+
}
|
|
94
|
+
return str.charAt(0).toUpperCase() + str.substring(1);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function getAddressBuffer(address: string): Uint8Array {
|
|
98
|
+
if (CommonUtils.isHexa(address)) {
|
|
99
|
+
return BufferUtils.hexToBuffer(address) ;
|
|
100
|
+
}
|
|
101
|
+
return Address.fromString(address).data;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function tokenIdToHex(token: string): string {
|
|
105
|
+
if (CommonUtils.isHexa(token)) {
|
|
106
|
+
return token;
|
|
107
|
+
}
|
|
108
|
+
return BufferUtils.bufferToHex(getAddressBuffer(token));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function tokenHexToAddr(token: string): string {
|
|
112
|
+
if (!CommonUtils.isHexa(token)) {
|
|
113
|
+
return token;
|
|
114
|
+
}
|
|
115
|
+
return new Address(getAddressBuffer(token), Networks.defaultNetwork, AddressType.GroupIdAddress).toString();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function tokenAmountToAssetAmount(amount?: string | number | bigint): string {
|
|
119
|
+
if (amount == null || amount === "") {
|
|
120
|
+
return "";
|
|
121
|
+
}
|
|
122
|
+
if (BigInt(amount) <= 0n) {
|
|
123
|
+
return "0";
|
|
124
|
+
}
|
|
125
|
+
return amount.toString();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Confirmed plus unconfirmed, in base units. The unconfirmed part can be negative (spent, not yet confirmed). */
|
|
129
|
+
export function balanceTotal(balance: Balance): bigint {
|
|
130
|
+
return BigInt(balance.confirmed) + BigInt(balance.unconfirmed);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function sumBalance(balances: Balance[]): Balance {
|
|
134
|
+
let confirmed = BigInt(0), unconfirmed = BigInt(0);
|
|
135
|
+
balances.forEach(b => {
|
|
136
|
+
confirmed += BigInt(b.confirmed);
|
|
137
|
+
unconfirmed += BigInt(b.unconfirmed);
|
|
138
|
+
});
|
|
139
|
+
return {confirmed: confirmed.toString(), unconfirmed: unconfirmed.toString()};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function sumTokensBalance(balances: Record<string, Balance>[]): Record<string, Balance> {
|
|
143
|
+
const tokensBalance: Record<string, Balance> = {};
|
|
144
|
+
balances.forEach(b => {
|
|
145
|
+
for (const key in b) {
|
|
146
|
+
if (tokensBalance[key]) {
|
|
147
|
+
tokensBalance[key].confirmed = (BigInt(tokensBalance[key].confirmed) + BigInt(b[key].confirmed)).toString();
|
|
148
|
+
tokensBalance[key].unconfirmed = (BigInt(tokensBalance[key].unconfirmed) + BigInt(b[key].unconfirmed)).toString();
|
|
149
|
+
} else {
|
|
150
|
+
tokensBalance[key] = { confirmed: BigInt(b[key].confirmed).toString(), unconfirmed: BigInt(b[key].unconfirmed).toString() };
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
return tokensBalance;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const FILE_EXTENSION_TO_TYPE = {
|
|
159
|
+
// Images
|
|
160
|
+
//'.svg': { media: 'image', mime: 'image/svg+xml' },
|
|
161
|
+
'.gif': { media: 'image', mime: 'image/gif' },
|
|
162
|
+
'.png': { media: 'image', mime: 'image/png' },
|
|
163
|
+
'.apng': { media: 'image', mime: 'image/apng' },
|
|
164
|
+
'.jpg': { media: 'image', mime: 'image/jpeg' },
|
|
165
|
+
'.jpeg': { media: 'image', mime: 'image/jpeg' },
|
|
166
|
+
'.avif': { media: 'image', mime: 'image/avif' },
|
|
167
|
+
'.webp': { media: 'image', mime: 'image/webp' },
|
|
168
|
+
'.bmp': { media: 'image', mime: 'image/bmp' },
|
|
169
|
+
|
|
170
|
+
// Videos
|
|
171
|
+
'.mp4': { media: 'video', mime: 'video/mp4' },
|
|
172
|
+
'.mpeg': { media: 'video', mime: 'video/mpeg' },
|
|
173
|
+
'.mpg': { media: 'video', mime: 'video/mpeg' },
|
|
174
|
+
'.webm': { media: 'video', mime: 'video/webm' },
|
|
175
|
+
'.mov': { media: 'video', mime: 'video/quicktime' },
|
|
176
|
+
|
|
177
|
+
// Audio
|
|
178
|
+
'.mp3': { media: 'audio', mime: 'audio/mpeg' },
|
|
179
|
+
'.ogg': { media: 'audio', mime: 'audio/ogg' },
|
|
180
|
+
'.wav': { media: 'audio', mime: 'audio/wav' },
|
|
181
|
+
'.m4a': { media: 'audio', mime: 'audio/mp4' },
|
|
182
|
+
} as const;
|
|
183
|
+
|
|
184
|
+
export function getFileMediaType(filename: string): 'video' | 'audio' | 'image' | 'unknown' {
|
|
185
|
+
const lastDotIndex = filename.lastIndexOf('.');
|
|
186
|
+
if (lastDotIndex === -1) return 'unknown';
|
|
187
|
+
|
|
188
|
+
const extension = filename.slice(lastDotIndex).toLowerCase();
|
|
189
|
+
return FILE_EXTENSION_TO_TYPE[extension as keyof typeof FILE_EXTENSION_TO_TYPE]?.media ?? 'unknown';
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function getFileMimeType(filename: string): string {
|
|
193
|
+
const lastDotIndex = filename.lastIndexOf('.');
|
|
194
|
+
if (lastDotIndex === -1) return 'application/octet-stream';
|
|
195
|
+
|
|
196
|
+
const extension = filename.slice(lastDotIndex).toLowerCase();
|
|
197
|
+
return FILE_EXTENSION_TO_TYPE[extension as keyof typeof FILE_EXTENSION_TO_TYPE]?.mime ?? 'application/octet-stream';
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function sleep(ms: number): Promise<void> {
|
|
201
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
202
|
+
}
|
package/src/utils/index.ts
CHANGED
package/src/utils/vault.ts
CHANGED
|
@@ -40,9 +40,10 @@ export function generateVaultAddress(pubKey: PublicKey, args: number[]): string
|
|
|
40
40
|
return address.toString();
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
/**
|
|
44
|
+
* A vault is spendable once the chain reached its lock block: a transaction with that lock time
|
|
45
|
+
* can go into the next block (`OP_CHECKLOCKTIMEVERIFY` in the template). `false` while the height is unknown (0).
|
|
46
|
+
*/
|
|
47
|
+
export function isVaultUnlocked(lockBlock: number, height: number): boolean {
|
|
48
|
+
return height > 0 && height >= lockBlock;
|
|
48
49
|
}
|