11package to.bitkit.data.backup
22
3- import org.lightningdevkit.ldknode.Network
3+ import com.synonym.vssclient.vssDeriveStoreId
44import to.bitkit.data.keychain.Keychain
55import to.bitkit.env.Env
6- import to.bitkit.ext.toHex
7- import to.bitkit.ext.toSha256
86import to.bitkit.utils.Logger
97import to.bitkit.utils.ServiceError
108import javax.inject.Inject
@@ -14,21 +12,31 @@ import javax.inject.Singleton
1412class VssStoreIdProvider @Inject constructor(
1513 private val keychain : Keychain ,
1614) {
15+ @Volatile
16+ private var cachedStoreId: String? = null
17+
1718 fun getVssStoreId (): String {
18- // TODO Temp fix as we don't have VSS auth yet
19- if (Env .network == Network .BITCOIN ) {
20- error(
21- " Do not run this on mainnet until VSS auth is implemented. Below hack is a temporary fix and not safe for mainnet."
22- )
23- }
19+ cachedStoreId?.let { return it }
20+
21+ return synchronized(this ) {
22+ cachedStoreId?.let { return it }
2423
25- val mnemonic = keychain.loadString(Keychain .Key .BIP39_MNEMONIC .name) ? : throw ServiceError .MnemonicNotFound
26- val mnemonicData = mnemonic.encodeToByteArray()
27- val hashedMnemonic = mnemonicData.toSha256()
24+ val mnemonic = keychain.loadString(Keychain .Key .BIP39_MNEMONIC .name) ? : throw ServiceError .MnemonicNotFound
25+ val passphrase = keychain.loadString(Keychain .Key .BIP39_PASSPHRASE .name)
2826
29- val storeIdHack = Env .vssStoreId + hashedMnemonic.toHex()
30- Logger .info(" storeIdHack: $storeIdHack " )
27+ val storeId = vssDeriveStoreId(
28+ prefix = Env .vssStoreIdPrefix,
29+ mnemonic = mnemonic,
30+ passphrase = passphrase,
31+ )
32+
33+ Logger .info(" VSS store id: '$storeId '" , context = TAG )
34+ cachedStoreId = storeId
35+ storeId
36+ }
37+ }
3138
32- return storeIdHack
39+ companion object {
40+ private const val TAG = " VssStoreIdProvider"
3341 }
3442}
0 commit comments