diff --git a/packages/lasereyes-core/package.json b/packages/lasereyes-core/package.json index 6b4897fd..3e402428 100644 --- a/packages/lasereyes-core/package.json +++ b/packages/lasereyes-core/package.json @@ -1,7 +1,7 @@ { "name": "@omnisat/lasereyes-core", "private": false, - "version": "0.0.52", + "version": "0.0.53-rc.2", "type": "module", "main": "./dist/index.umd.cjs", "module": "./dist/index.js", diff --git a/packages/lasereyes-core/src/client/index.ts b/packages/lasereyes-core/src/client/index.ts index 40910cfa..c0d1ab50 100644 --- a/packages/lasereyes-core/src/client/index.ts +++ b/packages/lasereyes-core/src/client/index.ts @@ -63,7 +63,7 @@ export class LaserEyesClient { [XVERSE]: new XVerseProvider(stores, this, config), [WIZZ]: new WizzProvider(stores, this, config), } - this.$network.subscribe(this.watchNetworkChange.bind(this)) + this.$network.listen(this.watchNetworkChange.bind(this)) subscribeKeys(this.$store, ['isInitializing'], (v) => this.handleIsInitializingChanged(v.isInitializing) diff --git a/packages/lasereyes-core/src/client/providers/xverse.ts b/packages/lasereyes-core/src/client/providers/xverse.ts index 3dc1b35b..7778a757 100644 --- a/packages/lasereyes-core/src/client/providers/xverse.ts +++ b/packages/lasereyes-core/src/client/providers/xverse.ts @@ -4,8 +4,6 @@ import { RpcErrorCode, getAddress, request, - signTransaction, - SignTransactionOptions, MessageSigningProtocols, } from 'sats-connect' import { WalletProvider } from '.' @@ -232,6 +230,7 @@ export default class XVerseProvider extends WalletProvider { } } } + async signPsbt( _: string, __: string, @@ -246,92 +245,93 @@ export default class XVerseProvider extends WalletProvider { } | undefined > { - const { address, paymentAddress } = this.$store.get() - const toSignPsbt = bitcoin.Psbt.fromBase64(String(psbtBase64), { - network: getBitcoinNetwork(this.network), - }) + try { + const toSignPsbt = bitcoin.Psbt.fromBase64(String(psbtBase64), { + network: getBitcoinNetwork(this.network), + }) - const inputs = toSignPsbt.data.inputs - const inputsToSign = [] - const ordinalAddressData = { - address: address, - signingIndexes: [] as number[], - } - const paymentsAddressData = { - address: paymentAddress, - signingIndexes: [] as number[], - } + const address = this.$store.get().address + const paymentAddress = this.$store.get().paymentAddress - let counter = 0 - for await (let input of inputs) { - if (input.witnessUtxo === undefined) { - paymentsAddressData.signingIndexes.push(Number(counter)) - } else { - const { script } = input.witnessUtxo! - const addressFromScript = bitcoin.address.fromOutputScript( - script, - getBitcoinNetwork(this.network) - ) - if (addressFromScript === paymentAddress) { - paymentsAddressData.signingIndexes.push(Number(counter)) - } else if (addressFromScript === address) { - ordinalAddressData.signingIndexes.push(Number(counter)) - } + const inputs = toSignPsbt.data.inputs + let inputsToSign: Record = {} + const ordinalAddressData: Record = { + [address]: [] as number[], + } + const paymentsAddressData: Record = { + [paymentAddress]: [] as number[], } - counter++ - } + let counter = 0 + for await (let input of inputs) { + if (input.witnessUtxo === undefined) { + paymentsAddressData[paymentAddress].push(Number(counter)) + } else { + const { script } = input.witnessUtxo! + const addressFromScript = bitcoin.address.fromOutputScript( + script, + getBitcoinNetwork(this.network) + ) + if (addressFromScript === paymentAddress) { + paymentsAddressData[paymentAddress].push(Number(counter)) + } else if (addressFromScript === address) { + ordinalAddressData[address].push(Number(counter)) + } + } + counter++ + } - if (ordinalAddressData.signingIndexes.length > 0) { - inputsToSign.push(ordinalAddressData) - } + if (ordinalAddressData[address].length > 0) { + inputsToSign = { ...inputsToSign, ...ordinalAddressData } + } - if (paymentsAddressData.signingIndexes.length > 0) { - inputsToSign.push(paymentsAddressData) - } + if (paymentsAddressData[paymentAddress].length > 0) { + inputsToSign = { ...inputsToSign, ...paymentsAddressData } + } - let txId, signedPsbtHex, signedPsbtBase64 - let signedPsbt: bitcoin.Psbt | undefined + let txId, signedPsbtHex, signedPsbtBase64 + let signedPsbt: bitcoin.Psbt | undefined - const xverseNetwork = getSatsConnectNetwork(this.network) + const response = await request("signPsbt", + { + psbt: psbtBase64, + broadcast: !!broadcast, + signInputs: inputsToSign, + } + ); - const signPsbtOptions = { - payload: { - network: { - type: xverseNetwork, - }, - message: 'Sign Transaction', - psbtBase64: toSignPsbt.toBase64(), - broadcast: broadcast, - inputsToSign: inputsToSign, - }, - onFinish: (response: { psbtBase64: string; txId: string }) => { - if (response.txId) { - txId = response.txId - } else if (response.psbtBase64) { - signedPsbt = bitcoin.Psbt.fromBase64(String(response.psbtBase64), { - network: getBitcoinNetwork(this.network), - }) + if (response.status === 'success') { + signedPsbt = bitcoin.Psbt.fromBase64(response.result.psbt, { network: getBitcoinNetwork(this.network) }); + txId = response.result.txid; + } else { + if (response.error.code === RpcErrorCode.USER_REJECTION) { + throw new Error('User canceled the request') + } else { + throw new Error('Error signing psbt') + } + } - if (_finalize) { - signedPsbt!.finalizeAllInputs() - } + if (!signedPsbt) { + throw new Error('Error signing psbt') + } - signedPsbtHex = signedPsbt.toHex() - signedPsbtBase64 = signedPsbt.toBase64() - } - }, - onCancel: () => { - console.error('Request canceled') - throw new Error('User canceled the request') - }, - } as SignTransactionOptions - await signTransaction(signPsbtOptions) + if (_finalize && !txId) { + signedPsbt!.finalizeAllInputs() + signedPsbtHex = signedPsbt.toHex() + signedPsbtBase64 = signedPsbt.toBase64() + } else { + signedPsbtHex = signedPsbt.toHex() + signedPsbtBase64 = signedPsbt.toBase64() + } - return { - signedPsbtHex, - signedPsbtBase64, - txId, + return { + signedPsbtHex, + signedPsbtBase64, + txId, + } + } catch (e) { + console.error(e) + throw e } } diff --git a/packages/lasereyes-react/package.json b/packages/lasereyes-react/package.json index efc07ca8..c4847f45 100644 --- a/packages/lasereyes-react/package.json +++ b/packages/lasereyes-react/package.json @@ -1,7 +1,7 @@ { "name": "@omnisat/lasereyes-react", "private": false, - "version": "0.0.47", + "version": "0.0.48-rc.0", "type": "module", "main": "./dist/index.umd.cjs", "module": "./dist/index.js", diff --git a/packages/lasereyes/package.json b/packages/lasereyes/package.json index f3d40583..04de2586 100644 --- a/packages/lasereyes/package.json +++ b/packages/lasereyes/package.json @@ -20,7 +20,7 @@ "url": "https://github.com/omnisat/lasereyes-mono.git" }, "private": false, - "version": "0.0.130", + "version": "0.0.131-rc.2", "type": "module", "main": "./dist/index.umd.cjs", "module": "./dist/index.js", @@ -56,4 +56,4 @@ "publishConfig": { "access": "public" } -} \ No newline at end of file +}