diff --git a/crud.py b/crud.py index adc0836..7bb799b 100644 --- a/crud.py +++ b/crud.py @@ -55,6 +55,26 @@ async def update_merchant( return await get_merchant(user_id, merchant_id) +async def update_merchant_keys( + user_id: str, merchant_id: str, private_key: str, public_key: str +) -> Optional[Merchant]: + await db.execute( + f""" + UPDATE nostrmarket.merchants + SET private_key = :private_key, public_key = :public_key, + time = {db.timestamp_now} + WHERE id = :id AND user_id = :user_id + """, + { + "private_key": private_key, + "public_key": public_key, + "id": merchant_id, + "user_id": user_id, + }, + ) + return await get_merchant(user_id, merchant_id) + + async def touch_merchant(user_id: str, merchant_id: str) -> Optional[Merchant]: await db.execute( f""" diff --git a/models.py b/models.py index 2c24dee..c766cc2 100644 --- a/models.py +++ b/models.py @@ -43,6 +43,8 @@ class MerchantConfig(MerchantProfile): # TODO: switched to True for AIO demo; determine if we leave this as True active: bool = True restore_in_progress: Optional[bool] = False + # Set at runtime (not persisted) when account keypair != merchant keypair + key_mismatch: Optional[bool] = False class CreateMerchantRequest(BaseModel): diff --git a/static/js/index.js b/static/js/index.js index e180d85..f5d2e62 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -36,6 +36,30 @@ window.app = Vue.createApp({ } }, methods: { + migrateKeys: async function () { + LNbits.utils + .confirmDialog( + 'This will update your merchant to use your current account Nostr keypair ' + + 'and republish all stalls and products under the new identity. ' + + 'Existing orders and messages are preserved. Continue?' + ) + .onOk(async () => { + try { + const {data} = await LNbits.api.request( + 'POST', + `/nostrmarket/api/v1/merchant/${this.merchant.id}/migrate-keys`, + this.g.user.wallets[0].adminkey + ) + this.merchant = data + this.$q.notify({ + type: 'positive', + message: 'Merchant keys migrated and stalls republished' + }) + } catch (error) { + LNbits.utils.notifyApiError(error) + } + }) + }, toggleShowKeys: function () { this.showKeys = !this.showKeys }, diff --git a/templates/nostrmarket/index.html b/templates/nostrmarket/index.html index 2b6ea41..c4f7931 100644 --- a/templates/nostrmarket/index.html +++ b/templates/nostrmarket/index.html @@ -3,6 +3,26 @@