Skip to content

Commit

Permalink
Merge pull request #289 from omnisat/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
hathbanger authored Dec 19, 2024
2 parents 2482d18 + 443ead1 commit 4ce8320
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 81 deletions.
2 changes: 1 addition & 1 deletion packages/lasereyes-core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/lasereyes-core/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
152 changes: 76 additions & 76 deletions packages/lasereyes-core/src/client/providers/xverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
RpcErrorCode,
getAddress,
request,
signTransaction,
SignTransactionOptions,
MessageSigningProtocols,
} from 'sats-connect'
import { WalletProvider } from '.'
Expand Down Expand Up @@ -232,6 +230,7 @@ export default class XVerseProvider extends WalletProvider {
}
}
}

async signPsbt(
_: string,
__: string,
Expand All @@ -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<string, number[]> = {}
const ordinalAddressData: Record<string, number[]> = {
[address]: [] as number[],
}
const paymentsAddressData: Record<string, number[]> = {
[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
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/lasereyes-react/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/lasereyes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -56,4 +56,4 @@
"publishConfig": {
"access": "public"
}
}
}

0 comments on commit 4ce8320

Please sign in to comment.