1
1
import {
2
- Coin ,
2
+ Coin ,
3
3
getCoinId ,
4
4
masterPublicKeyToFirstPuzzleHash ,
5
5
masterPublicKeyToWalletSyntheticKey ,
@@ -13,15 +13,8 @@ import {
13
13
import { mnemonicToSeedSync } from 'bip39' ;
14
14
import { PrivateKey } from 'chia-bls' ;
15
15
import { FileCacheService } from '../services/FileCacheService' ;
16
- import { CACHE_DURATION } from '../services/WalletService' ;
17
- import path from 'path' ;
18
- import os from 'os' ;
19
16
20
- export const MIN_HEIGHT = 5777842 ;
21
- export const MIN_HEIGHT_HEADER_HASH =
22
- "b29a4daac2434fd17a36e15ba1aac5d65012d4a66f99bed0bf2b5342e92e562c" ;
23
-
24
- export const USER_DIR_PATH = path . join ( os . homedir ( ) , '.dig' ) ;
17
+ const COIN_CACHE_DURATION = 600000 ;
25
18
26
19
export class Wallet {
27
20
private mnemonic : string ;
@@ -75,21 +68,18 @@ export class Wallet {
75
68
peer : Peer ,
76
69
coinAmount : bigint ,
77
70
feeBigInt : bigint ,
78
- omitCoins : Coin [ ] = [ ]
71
+ omitCoins : Coin [ ] = [ ] ,
72
+ lastHeight : number ,
73
+ lastHeaderHash : string ,
79
74
) : Promise < Coin [ ] > {
80
- const cache = new FileCacheService < { coinId : string ; expiry : number } > (
81
- "reserved_coins" ,
82
- USER_DIR_PATH
83
- ) ;
75
+ const cache = new FileCacheService < { coinId : string ; expiry : number } > ( 'reserved_coins' ) ;
84
76
85
77
const ownerPuzzleHash = await this . getOwnerPuzzleHash ( ) ;
86
78
87
79
// Define a function to attempt selecting unspent coins
88
80
const trySelectCoins = async ( ) : Promise < Coin [ ] > => {
89
81
const now = Date . now ( ) ;
90
- const omitCoinIds = omitCoins . map ( ( coin ) =>
91
- getCoinId ( coin ) . toString ( "hex" )
92
- ) ;
82
+ const omitCoinIds = omitCoins . map ( ( coin ) => getCoinId ( coin ) . toString ( 'hex' ) ) ;
93
83
94
84
// Update omitCoinIds with currently valid reserved coins
95
85
const cachedReservedCoins = cache . getCachedKeys ( ) ;
@@ -107,12 +97,12 @@ export class Wallet {
107
97
108
98
const coinsResp = await peer . getAllUnspentCoins (
109
99
ownerPuzzleHash ,
110
- MIN_HEIGHT ,
111
- Buffer . from ( MIN_HEIGHT_HEADER_HASH , " hex" )
100
+ lastHeight ,
101
+ Buffer . from ( lastHeaderHash , ' hex' ) ,
112
102
) ;
113
103
114
104
const unspentCoins = coinsResp . coins . filter (
115
- ( coin ) => ! omitCoinIds . includes ( getCoinId ( coin ) . toString ( " hex" ) )
105
+ ( coin ) => ! omitCoinIds . includes ( getCoinId ( coin ) . toString ( ' hex' ) ) ,
116
106
) ;
117
107
118
108
const selectedCoins = selectCoins ( unspentCoins , feeBigInt + coinAmount ) ;
@@ -142,15 +132,15 @@ export class Wallet {
142
132
await new Promise ( ( resolve ) => setTimeout ( resolve , 10000 ) ) ;
143
133
} else {
144
134
// No unspent coins and no reserved coins
145
- throw new Error ( " No unspent coins available." ) ;
135
+ throw new Error ( ' No unspent coins available.' ) ;
146
136
}
147
137
}
148
138
}
149
139
150
140
// Reserve the selected coins
151
141
selectedCoins . forEach ( ( coin ) => {
152
- const coinId = getCoinId ( coin ) . toString ( " hex" ) ;
153
- cache . set ( coinId , { coinId, expiry : Date . now ( ) + CACHE_DURATION } ) ;
142
+ const coinId = getCoinId ( coin ) . toString ( ' hex' ) ;
143
+ cache . set ( coinId , { coinId, expiry : Date . now ( ) + COIN_CACHE_DURATION } ) ;
154
144
} ) ;
155
145
156
146
return selectedCoins ;
0 commit comments