Skip to content

Commit

Permalink
Untested: Support P2SH input consuming on provider api
Browse files Browse the repository at this point in the history
  • Loading branch information
KaffinPX committed Jul 25, 2024
1 parent 467449e commit feca556
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/pages/Wallet/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Input } from "@/components/ui/input"
import { Dialog } from "@/components/ui/dialog"
import useURLParams from "@/hooks/useURLParams"
import useKaspa from "@/hooks/useKaspa"
import { CustomInput } from "@/provider/protocol"

export enum Tabs {
Creation,
Expand All @@ -28,12 +29,14 @@ export default function SendDrawer () {
const { kaspa, request } = useKaspa()
const [ hash, params ] = useURLParams()
const [ outputs, setOutputs ] = useState<[ string, string ][]>(JSON.parse(params.get('outputs') ?? `[[ "", "" ]]`))
const [ fee ] = useState(params.get('fee') ?? "0")
const [ inputs ] = useState<CustomInput[]>(JSON.parse(params.get('outputs') ?? `[]`))
const [ transactions, setTransactions ] = useState<string[]>()
const [ error, setError ] = useState("")
const [ tab, setTab ] = useState(Tabs.Creation)

const initiateSend = useCallback(() => {
request('account:create', [ outputs, '0' ]).then((transactions) => {
request('account:create', [ outputs, fee, inputs ]).then((transactions) => {
setTransactions(transactions)
setTab(Tabs.Sign)
}).catch((err) => {
Expand Down Expand Up @@ -89,8 +92,8 @@ export default function SendDrawer () {
})
}}
onKeyUp={e => {
if (e.key !== 'Enter' || output[0] === "") return;
initiateSend();
if (e.key !== 'Enter' || output[0] === "") return
initiateSend()
}}
/>
<Input
Expand Down Expand Up @@ -155,7 +158,7 @@ export default function SendDrawer () {
setTab(Tabs.Creation)
setTransactions(undefined)
}}>
{tab === Tabs.Sign && <Sign transactions={transactions!} onSigned={(transactions) => {
{tab === Tabs.Sign && <Sign transactions={transactions!} inputs={inputs} onSigned={(transactions) => {
setTransactions(transactions)
setTab(Tabs.Submit)
}} />}
Expand Down
6 changes: 4 additions & 2 deletions src/pages/Wallet/Send/Sign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import {
DialogTitle,
} from "@/components/ui/dialog"
import useKaspa from "@/hooks/useKaspa"
import { CustomInput } from "@/provider/protocol"

export default function Sign ({ transactions, onSigned }: {
export default function Sign ({ transactions, inputs, onSigned }: {
transactions: string[],
inputs: CustomInput[],
onSigned: (transactions: string[]) => void
}) {
const kaspa = useKaspa()
Expand Down Expand Up @@ -48,7 +50,7 @@ export default function Sign ({ transactions, onSigned }: {
const sign = useCallback(() => {
setPassword("")

kaspa.request('account:sign', [ transactions, password ]).then((transactions) => {
kaspa.request('account:sign', [ transactions, password, inputs ]).then((transactions) => {
onSigned(transactions)
}).catch((err) => {
setError(err)
Expand Down
12 changes: 9 additions & 3 deletions src/provider/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ export interface AccountInfo {
addresses: [ string[], string[] ]
}

export interface CustomInput {
address: string
outpoint: string
index: number,
signer: string
script?: string
}

export interface RequestMappings {
'transact': [[ string, string ][]]
'transact': [[ string, string ][], string, CustomInput[]?] // outputs, fee, custom
}

export interface Request<M extends keyof RequestMappings = keyof RequestMappings> {
Expand All @@ -27,8 +35,6 @@ export function isRequest (object: any): object is Request {

switch (object.method) {
case 'transact': {
if (!Array.isArray(object.params)) return false

for (const output of object.params[0]) {
if (typeof output[0] !== 'string' || typeof output[1] !== 'string') return false // TODO: Better checks
}
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/kaspa/account/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export interface CustomInput {
}

export interface CustomSignature {
outpoint: number
index: string,
outpoint: string,
index: number,
signer: string,
script?: string
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/messaging/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface RequestMappings {
'account:balance': []
'account:utxos': []
'account:create': [[ string, string ][], string, CustomInput[]? ]
'account:sign': [ string[], string, CustomSignature[] ]
'account:sign': [ string[], string, CustomSignature[]? ]
'account:submitContextful': [ string[] ]
'account:scan': []
'provider:connect': [ string ]
Expand Down
16 changes: 15 additions & 1 deletion src/wallet/messaging/wallet/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default class Provider extends EventEmitter {

this.windows = new Windows()
this.account = account

this.registerEvents()
}

get connectedURL () {
Expand Down Expand Up @@ -82,7 +84,9 @@ export default class Provider extends EventEmitter {
let transactions: string[]

await this.windows.open('transact', {
'outputs': JSON.stringify(request.params[0], null, 0)
'outputs': JSON.stringify(request.params[0], null, 0),
'fee': request.params[1],
'inputs': JSON.stringify(request.params[2] ?? [], null, 0) // TODO: consider where to do this
}, () => {
if (transactions) {
this.submitEvent(request.id, 'transactions', transactions)
Expand All @@ -94,4 +98,14 @@ export default class Provider extends EventEmitter {
this.account.transactions.once('transactions', (parsedTransactions) => transactions = parsedTransactions)
}
}

private registerEvents () {
// TODO: implement better event processing by a seperate class :p
this.account.on('balance', () => {
this.submitEvent(0, 'account', {
balance: this.account.balance,
addresses: [ this.account.addresses.receiveAddresses, this.account.addresses.changeAddresses ]
})
})
}
}

0 comments on commit feca556

Please sign in to comment.