@otoplo/wallet-common 0.4.2 → 0.4.4
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.js +246 -224
- package/dist/index.js.map +1 -1
- package/dist/types/services/asset.d.ts.map +1 -1
- package/dist/types/services/discovery.d.ts +6 -2
- package/dist/types/services/discovery.d.ts.map +1 -1
- package/dist/types/services/market.d.ts +1 -1
- package/dist/types/services/market.d.ts.map +1 -1
- package/dist/types/services/rostrum.d.ts +7 -2
- package/dist/types/services/rostrum.d.ts.map +1 -1
- package/dist/types/services/transaction.d.ts.map +1 -1
- package/dist/types/services/wallet.d.ts +2 -1
- package/dist/types/services/wallet.d.ts.map +1 -1
- package/dist/types/types/db.types.d.ts +1 -0
- package/dist/types/types/db.types.d.ts.map +1 -1
- package/dist/types/types/rostrum.types.d.ts +4 -17
- package/dist/types/types/rostrum.types.d.ts.map +1 -1
- package/dist/types/types/wallet.types.d.ts +1 -0
- package/dist/types/types/wallet.types.d.ts.map +1 -1
- package/dist/types/utils/common.d.ts +42 -4
- package/dist/types/utils/common.d.ts.map +1 -1
- package/dist/types/utils/index.d.ts +0 -1
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/vault.d.ts +3 -0
- package/dist/types/utils/vault.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/services/asset.ts +2 -1
- package/src/services/discovery.ts +30 -21
- package/src/services/market.ts +1 -1
- package/src/services/rostrum.ts +43 -11
- package/src/services/transaction.ts +2 -1
- package/src/services/wallet.ts +16 -11
- package/src/types/db.types.ts +1 -0
- package/src/types/rostrum.types.ts +4 -18
- package/src/types/wallet.types.ts +1 -0
- package/src/utils/common.ts +26 -15
- package/src/utils/index.ts +0 -1
- package/src/utils/vault.ts +12 -2
- package/dist/types/utils/market.d.ts +0 -45
- package/dist/types/utils/market.d.ts.map +0 -1
- package/src/utils/market.ts +0 -43
package/src/services/wallet.ts
CHANGED
|
@@ -150,18 +150,21 @@ export class WalletManager {
|
|
|
150
150
|
|
|
151
151
|
const rIndexPromise = this.discoveryService.discoverWalletIndex(AccountType.MAIN, KeySpace.RECEIVE);
|
|
152
152
|
const cIndexPromise = this.discoveryService.discoverWalletIndex(AccountType.MAIN, KeySpace.CHANGE);
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
153
|
+
const dappIndexPromise = this.discoveryService.discoverWalletIndex(AccountType.DAPP, KeySpace.RECEIVE);
|
|
154
|
+
const vaultsPromise = this.discoveryService.discoverVaults();
|
|
155
|
+
|
|
156
|
+
const [rIndex, cIndex, dappIndex, vaults] = await Promise.all([rIndexPromise, cIndexPromise, dappIndexPromise, vaultsPromise]);
|
|
156
157
|
|
|
157
158
|
this.receiveAddresses = this.deriveAddresses(KeySpace.RECEIVE, 0, rIndex + KeyManager.GAP_LIMIT + 1);
|
|
158
159
|
this.changeAddresses = this.deriveAddresses(KeySpace.CHANGE, 0, cIndex + KeyManager.GAP_LIMIT + 1);
|
|
159
160
|
this.accounts = this.deriveAccounts(0, dappIndex < 0 ? 1 : dappIndex + 1);
|
|
160
161
|
this.accounts.forEach(entry => this.accountsAddressToId.set(entry.address, entry.id));
|
|
162
|
+
this.vaults = vaults;
|
|
161
163
|
|
|
162
164
|
await this.saveAddresses(this.receiveAddresses);
|
|
163
165
|
await this.saveAddresses(this.changeAddresses);
|
|
164
166
|
await this.saveAccounts(this.accounts);
|
|
167
|
+
await this.saveVaults(this.vaults);
|
|
165
168
|
|
|
166
169
|
this.initState();
|
|
167
170
|
|
|
@@ -296,6 +299,12 @@ export class WalletManager {
|
|
|
296
299
|
}
|
|
297
300
|
}
|
|
298
301
|
|
|
302
|
+
private async saveVaults(vaults: Map<string, VaultDTO>): Promise<void> {
|
|
303
|
+
for (const vault of vaults.values()) {
|
|
304
|
+
await this.walletDb.saveVault(vault);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
299
308
|
private async subscribeAddresses(addresses: WalletAddress[], isInit = false): Promise<void> {
|
|
300
309
|
const subscriptionPromises = addresses.map(async addr => {
|
|
301
310
|
const result = await this.rostrumService.subscribeAddress(addr.address, this.onSubscribeEvent);
|
|
@@ -394,7 +403,7 @@ export class WalletManager {
|
|
|
394
403
|
await Promise.all(vaultsHistoryPromises);
|
|
395
404
|
const walletTxs = await Promise.all(walletHistoryPromises);
|
|
396
405
|
|
|
397
|
-
const postPromises: Promise<void>[] = [this.postProcessWalletUpdate(walletTxs
|
|
406
|
+
const postPromises: Promise<void>[] = [this.postProcessWalletUpdate(walletTxs), this.postProcessAccountsUpdate(accountsTxs)];
|
|
398
407
|
if (initResolved) {
|
|
399
408
|
postPromises.push(this.recoverTokens());
|
|
400
409
|
}
|
|
@@ -411,7 +420,7 @@ export class WalletManager {
|
|
|
411
420
|
}
|
|
412
421
|
}
|
|
413
422
|
|
|
414
|
-
private async postProcessWalletUpdate(history: AddressHistory[]
|
|
423
|
+
private async postProcessWalletUpdate(history: AddressHistory[]): Promise<void> {
|
|
415
424
|
if (history.length === 0) {
|
|
416
425
|
return;
|
|
417
426
|
}
|
|
@@ -424,10 +433,6 @@ export class WalletManager {
|
|
|
424
433
|
}
|
|
425
434
|
const txHistory = Array.from(txHistoryMap.values());
|
|
426
435
|
|
|
427
|
-
if (processVaults) {
|
|
428
|
-
this.recoverVaults(txHistory);
|
|
429
|
-
}
|
|
430
|
-
|
|
431
436
|
await this.checkGapLimit(KeySpace.RECEIVE);
|
|
432
437
|
await this.checkGapLimit(KeySpace.CHANGE);
|
|
433
438
|
|
|
@@ -642,9 +647,9 @@ export class WalletManager {
|
|
|
642
647
|
await this.subscribeAddresses([vault]);
|
|
643
648
|
}
|
|
644
649
|
|
|
645
|
-
public async recoverVaults(
|
|
650
|
+
public async recoverVaults(): Promise<boolean> {
|
|
646
651
|
let found = false;
|
|
647
|
-
const vaults = await this.discoveryService.discoverVaults(
|
|
652
|
+
const vaults = await this.discoveryService.discoverVaults();
|
|
648
653
|
const promises: Promise<void>[] = [];
|
|
649
654
|
for (const [address, vault] of vaults) {
|
|
650
655
|
if (!this.vaults.has(address)) {
|
package/src/types/db.types.ts
CHANGED
|
@@ -10,6 +10,7 @@ export interface RostrumParams {
|
|
|
10
10
|
export interface ServerFeatures {
|
|
11
11
|
genesis_hash: string;
|
|
12
12
|
server_version: string;
|
|
13
|
+
script_index?: boolean;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
export interface BlockTip {
|
|
@@ -24,30 +25,15 @@ export interface IFirstUse {
|
|
|
24
25
|
tx_hash: string;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
|
-
export interface ITokenGenesis {
|
|
28
|
-
decimal_places?: number,
|
|
29
|
-
document_hash?: string;
|
|
30
|
-
document_url?: string;
|
|
31
|
-
group: string;
|
|
32
|
-
height: number;
|
|
33
|
-
name?: string;
|
|
34
|
-
op_return?: string;
|
|
35
|
-
ticker?: string;
|
|
36
|
-
token_id_hex: string;
|
|
37
|
-
txid: string;
|
|
38
|
-
txidem: string;
|
|
39
|
-
op_return_id: number;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
28
|
export interface ITokensBalance {
|
|
43
29
|
cursor?: any;
|
|
44
30
|
confirmed: Record<string, string | number>;
|
|
45
31
|
unconfirmed: Record<string, string | number>;
|
|
46
32
|
}
|
|
47
33
|
|
|
48
|
-
export interface
|
|
49
|
-
|
|
50
|
-
|
|
34
|
+
export interface ITemplateScript {
|
|
35
|
+
scriptpubkey: string;
|
|
36
|
+
scripthash: string;
|
|
51
37
|
}
|
|
52
38
|
|
|
53
39
|
export interface ITokenUtxo {
|
package/src/utils/common.ts
CHANGED
|
@@ -4,8 +4,22 @@ import bigDecimal from 'js-big-decimal';
|
|
|
4
4
|
|
|
5
5
|
export const HD_WALLET_ID = -1;
|
|
6
6
|
export const MAX_INT64 = 9223372036854775807n;
|
|
7
|
-
|
|
8
|
-
export const
|
|
7
|
+
|
|
8
|
+
export const CURRENCIES = {
|
|
9
|
+
usd: { symbol: '$', name: 'US Dollar' },
|
|
10
|
+
eur: { symbol: '€', name: 'Euro' },
|
|
11
|
+
gbp: { symbol: '£', name: 'British Pound' },
|
|
12
|
+
aud: { symbol: 'A$', name: 'Australian Dollar' },
|
|
13
|
+
cad: { symbol: 'CA$', name: 'Canadian Dollar' },
|
|
14
|
+
chf: { symbol: 'CHF', name: 'Swiss Franc' },
|
|
15
|
+
inr: { symbol: '₹', name: 'Indian Rupee' },
|
|
16
|
+
cny: { symbol: 'CN¥', name: 'Chinese Yuan' },
|
|
17
|
+
jpy: { symbol: '¥', name: 'Japanese Yen' },
|
|
18
|
+
} as const satisfies Record<string, { symbol: string; name: string }>;
|
|
19
|
+
|
|
20
|
+
export type CurrencyCode = keyof typeof CURRENCIES;
|
|
21
|
+
|
|
22
|
+
export const currencyCodes = Object.keys(CURRENCIES) as CurrencyCode[];
|
|
9
23
|
|
|
10
24
|
export function isTestnet(): boolean {
|
|
11
25
|
return Networks.defaultNetwork == Networks.testnet;
|
|
@@ -30,13 +44,6 @@ export function currentTimestamp(): number {
|
|
|
30
44
|
return Math.floor(Date.now() / 1000);
|
|
31
45
|
}
|
|
32
46
|
|
|
33
|
-
export function estimateDateByFutureBlock(current: number, future: number): string {
|
|
34
|
-
const estimateMins = (future - current) * 2;
|
|
35
|
-
const time = new Date();
|
|
36
|
-
time.setMinutes(time.getMinutes() + estimateMins);
|
|
37
|
-
return time.toLocaleDateString();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
47
|
export function isNullOrEmpty(arg?: string | any[] | null): arg is undefined | [] | null | '' {
|
|
41
48
|
return !arg || arg.length === 0;
|
|
42
49
|
}
|
|
@@ -163,10 +170,14 @@ export function sleep(ms: number): Promise<void> {
|
|
|
163
170
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
164
171
|
}
|
|
165
172
|
|
|
166
|
-
export function prettifyAmount(amount: string | number): string {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
173
|
+
export function prettifyAmount(amount: string | number | bigint): string {
|
|
174
|
+
return new bigDecimal(amount).stripTrailingZero().getPrettyValue();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function calculateFiatValue(amount: string | number, price: number, currency: CurrencyCode, locale = 'en-US'): string {
|
|
178
|
+
const value = bigDecimal.multiply(amount, price);
|
|
179
|
+
return new Intl.NumberFormat(locale, {
|
|
180
|
+
style: 'currency',
|
|
181
|
+
currency,
|
|
182
|
+
}).format(Number(value));
|
|
172
183
|
}
|
package/src/utils/index.ts
CHANGED
package/src/utils/vault.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import type { PublicKey, ScriptElement} from "libnexa-ts";
|
|
1
|
+
import type { PublicKey, ScriptElement} from "libnexa-ts";
|
|
2
2
|
import { Address, BNExtended, Hash, Opcode, Script, ScriptOpcode } from "libnexa-ts";
|
|
3
3
|
|
|
4
|
+
export const VAULT_FIRST_BLOCK = 274710;
|
|
5
|
+
export const VAULT_SCRIPT_PREFIX = "0014461ad25081cb0119d034385ff154c8d3ad6bdd76";
|
|
6
|
+
|
|
4
7
|
export function getVaultTemplate(): Script {
|
|
5
8
|
return Script.empty()
|
|
6
9
|
.add(Opcode.OP_FROMALTSTACK).add(Opcode.OP_DROP)
|
|
@@ -36,4 +39,11 @@ export function generateVaultAddress(pubKey: PublicKey, args: number[]): string
|
|
|
36
39
|
const visibleArgs = generateVaultVisibleArgs(args);
|
|
37
40
|
const address = Address.fromScriptTemplate(templateHash, constraintHash, visibleArgs);
|
|
38
41
|
return address.toString();
|
|
39
|
-
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function estimateDateByFutureBlock(current: number, future: number): string {
|
|
45
|
+
const estimateMins = (future - current) * 2;
|
|
46
|
+
const time = new Date();
|
|
47
|
+
time.setMinutes(time.getMinutes() + estimateMins);
|
|
48
|
+
return time.toLocaleDateString();
|
|
49
|
+
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
export declare const CURRENCIES: readonly [{
|
|
2
|
-
readonly code: "usd";
|
|
3
|
-
readonly symbol: "$";
|
|
4
|
-
readonly name: "US Dollar";
|
|
5
|
-
}, {
|
|
6
|
-
readonly code: "eur";
|
|
7
|
-
readonly symbol: "€";
|
|
8
|
-
readonly name: "Euro";
|
|
9
|
-
}, {
|
|
10
|
-
readonly code: "gbp";
|
|
11
|
-
readonly symbol: "£";
|
|
12
|
-
readonly name: "British Pound";
|
|
13
|
-
}, {
|
|
14
|
-
readonly code: "aud";
|
|
15
|
-
readonly symbol: "A$";
|
|
16
|
-
readonly name: "Australian Dollar";
|
|
17
|
-
}, {
|
|
18
|
-
readonly code: "cad";
|
|
19
|
-
readonly symbol: "C$";
|
|
20
|
-
readonly name: "Canadian Dollar";
|
|
21
|
-
}, {
|
|
22
|
-
readonly code: "chf";
|
|
23
|
-
readonly symbol: "Fr";
|
|
24
|
-
readonly name: "Swiss Franc";
|
|
25
|
-
}, {
|
|
26
|
-
readonly code: "inr";
|
|
27
|
-
readonly symbol: "₹";
|
|
28
|
-
readonly name: "Indian Rupee";
|
|
29
|
-
}, {
|
|
30
|
-
readonly code: "cny";
|
|
31
|
-
readonly symbol: "¥";
|
|
32
|
-
readonly name: "Chinese Yuan";
|
|
33
|
-
}, {
|
|
34
|
-
readonly code: "jpy";
|
|
35
|
-
readonly symbol: "¥";
|
|
36
|
-
readonly name: "Japanese Yen";
|
|
37
|
-
}];
|
|
38
|
-
export type CurrencyCode = typeof CURRENCIES[number]['code'];
|
|
39
|
-
export type CurrencySymbol = typeof CURRENCIES[number]['symbol'];
|
|
40
|
-
export declare const currencySymbols: Record<CurrencyCode, CurrencySymbol>;
|
|
41
|
-
export declare const currencyCodes: ("usd" | "eur" | "gbp" | "aud" | "cad" | "chf" | "inr" | "cny" | "jpy")[];
|
|
42
|
-
export declare function getCurrencySymbol(currency: CurrencyCode): string;
|
|
43
|
-
export declare function calculateFiatValue(amount: string | number, price: number, currency?: CurrencyCode): string;
|
|
44
|
-
export declare function formatMoney(value: string | number, currency: CurrencyCode, locale?: string): string;
|
|
45
|
-
//# sourceMappingURL=market.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"market.d.ts","sourceRoot":"","sources":["../../../src/utils/market.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAUb,CAAC;AAEX,MAAM,MAAM,YAAY,GAAG,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AAE7D,MAAM,MAAM,cAAc,GAAG,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC;AAEjE,eAAO,MAAM,eAAe,EAEvB,MAAM,CAAC,YAAY,EAAE,cAAc,CAAC,CAAC;AAE1C,eAAO,MAAM,aAAa,2EAA8B,CAAC;AAEzD,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,YAAY,GAAG,MAAM,CAEhE;AAGD,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,YAAY,GAAG,MAAM,CAI1G;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAU,GAAG,MAAM,CAOpG"}
|
package/src/utils/market.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import bigDecimal from "js-big-decimal";
|
|
2
|
-
|
|
3
|
-
export const CURRENCIES = [
|
|
4
|
-
{ code: 'usd', symbol: '$', name: 'US Dollar' },
|
|
5
|
-
{ code: 'eur', symbol: '€', name: 'Euro' },
|
|
6
|
-
{ code: 'gbp', symbol: '£', name: 'British Pound' },
|
|
7
|
-
{ code: 'aud', symbol: 'A$', name: 'Australian Dollar' },
|
|
8
|
-
{ code: 'cad', symbol: 'C$', name: 'Canadian Dollar' },
|
|
9
|
-
{ code: 'chf', symbol: 'Fr', name: 'Swiss Franc' },
|
|
10
|
-
{ code: 'inr', symbol: '₹', name: 'Indian Rupee' },
|
|
11
|
-
{ code: 'cny', symbol: '¥', name: 'Chinese Yuan' },
|
|
12
|
-
{ code: 'jpy', symbol: '¥', name: 'Japanese Yen' },
|
|
13
|
-
] as const;
|
|
14
|
-
|
|
15
|
-
export type CurrencyCode = typeof CURRENCIES[number]['code'];
|
|
16
|
-
|
|
17
|
-
export type CurrencySymbol = typeof CURRENCIES[number]['symbol'];
|
|
18
|
-
|
|
19
|
-
export const currencySymbols = Object.fromEntries(
|
|
20
|
-
CURRENCIES.map(c => [c.code, c.symbol])
|
|
21
|
-
) as Record<CurrencyCode, CurrencySymbol>;
|
|
22
|
-
|
|
23
|
-
export const currencyCodes = CURRENCIES.map(c => c.code);
|
|
24
|
-
|
|
25
|
-
export function getCurrencySymbol(currency: CurrencyCode): string {
|
|
26
|
-
return currencySymbols[currency] || currency;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
export function calculateFiatValue(amount: string | number, price: number, currency?: CurrencyCode): string {
|
|
31
|
-
const value = bigDecimal.multiply(amount, price);
|
|
32
|
-
const rounded = bigDecimal.round(value, 2, bigDecimal.RoundingModes.HALF_DOWN);
|
|
33
|
-
return currency ? formatMoney(rounded, currency) : rounded;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function formatMoney(value: string | number, currency: CurrencyCode, locale = 'en-US'): string {
|
|
37
|
-
return new Intl.NumberFormat(locale, {
|
|
38
|
-
style: 'currency',
|
|
39
|
-
currency,
|
|
40
|
-
maximumFractionDigits: 2,
|
|
41
|
-
minimumFractionDigits: 2
|
|
42
|
-
}).format(Number(value));
|
|
43
|
-
}
|