Skip to content

Commit

Permalink
feat(walletd-libs): construct api and v2 api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Feb 13, 2025
1 parent 2af2001 commit eb47a73
Show file tree
Hide file tree
Showing 20 changed files with 330 additions and 76 deletions.
7 changes: 7 additions & 0 deletions .changeset/hungry-monkeys-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@siafoundation/walletd-js': minor
'@siafoundation/walletd-react': minor
'@siafoundation/walletd-types': minor
---

Added construct APIs for both v1 and v2 transactions.
7 changes: 7 additions & 0 deletions .changeset/perfect-turtles-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@siafoundation/walletd-js': minor
'@siafoundation/walletd-react': minor
'@siafoundation/walletd-types': minor
---

Added the basis ChainIndex parameter to txpool broadcast API.
7 changes: 7 additions & 0 deletions .changeset/tiny-deers-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@siafoundation/walletd-js': minor
'@siafoundation/walletd-react': minor
'@siafoundation/walletd-types': minor
---

Added the basis ChainIndex parameter to fund APIs.
7 changes: 7 additions & 0 deletions .changeset/tricky-carrots-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@siafoundation/walletd-js': minor
'@siafoundation/walletd-react': minor
'@siafoundation/walletd-types': minor
---

Added the basis ChainIndex value to txpool transactions response.
4 changes: 2 additions & 2 deletions apps/walletd/contexts/addresses/dataset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export function transformAddressesResponse(
const datum: AddressData = {
id: address,
address,
description: description,
spendPolicy: spendPolicy,
description,
spendPolicy,
metadata: (metadata || {}) as WalletAddressMetadata,
walletId,
onClick: () =>
Expand Down
3 changes: 2 additions & 1 deletion apps/walletd/contexts/addresses/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
WalletAddress,
WalletAddressMetadata,
} from '@siafoundation/walletd-types'
import { SpendPolicy } from '@siafoundation/types'

export type CellContext = {
siascanUrl: string
Expand All @@ -11,7 +12,7 @@ export type AddressData = {
id: string
address: string
description?: string
spendPolicy?: string
spendPolicy?: SpendPolicy
metadata: WalletAddressMetadata
walletId: string
raw?: WalletAddress
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Transaction } from '@siafoundation/types'
import { ChainIndex, Transaction } from '@siafoundation/types'
import { useCallback } from 'react'
import { SendParams } from '../_sharedWalletSend/types'

Expand All @@ -7,6 +7,7 @@ type Props = {
fundedTransaction?: Transaction
toSign?: string[]
error?: string
basis?: ChainIndex
}>
cancel: (transaction: Transaction) => Promise<void>
sign: ({
Expand All @@ -28,6 +29,7 @@ export function useFundAndSign({ fund, cancel, sign }: Props) {
fundedTransaction,
toSign,
error: fundingError,
basis,
} = await fund(params)
if (fundingError) {
return {
Expand All @@ -47,6 +49,7 @@ export function useFundAndSign({ fund, cancel, sign }: Props) {
}
}
return {
basis,
signedTransaction,
}
},
Expand Down
18 changes: 15 additions & 3 deletions apps/walletd/dialogs/WalletSendLedgerDialog/useSendForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@siafoundation/design-system'
import { DeviceConnectForm } from '../DeviceConnectForm'
import { useLedger } from '../../contexts/ledger'
import { Transaction } from '@siafoundation/types'
import { Transaction, ChainIndex } from '@siafoundation/types'
import { LedgerSignTxn } from './LedgerSignTxn'
import { useSign } from './useSign'
import { useBroadcast } from '../_sharedWalletSend/useBroadcast'
Expand Down Expand Up @@ -69,10 +69,12 @@ export function useSendForm({ params, step, onConfirm }: Props) {
const fundAndSign = useFundAndSign({ cancel, fund, sign })
const [waitingForUser, setWaitingForUser] = useState(false)
const [txn, setTxn] = useState<Transaction>()
const [basis, setBasis] = useState<ChainIndex>()

useEffect(() => {
if (step === 'compose') {
setTxn(undefined)
setBasis(undefined)
}
}, [step])

Expand Down Expand Up @@ -109,8 +111,17 @@ export function useSendForm({ params, step, onConfirm }: Props) {
return
}

if (!txn) {
return
}

if (!basis) {
return
}

const { error } = await broadcast({
signedTransaction: txn,
basis,
})

if (error) {
Expand All @@ -124,7 +135,7 @@ export function useSendForm({ params, step, onConfirm }: Props) {
// transactionId,
})
},
[broadcast, txn, onConfirm]
[broadcast, txn, basis, onConfirm]
)

const onInvalid = useOnInvalid(fields)
Expand All @@ -136,11 +147,12 @@ export function useSendForm({ params, step, onConfirm }: Props) {

const runFundAndSign = useCallback(async () => {
setWaitingForUser(true)
const { signedTransaction, error } = await fundAndSign(params)
const { signedTransaction, error, basis } = await fundAndSign(params)
if (error) {
triggerErrorToast({ title: error })
} else {
setTxn(signedTransaction)
setBasis(basis)
form.setValue('isSigned', true)
}
setWaitingForUser(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function useSignAndBroadcast() {
fundedTransaction,
toSign,
error: fundingError,
basis,
} = await fund(params)
if (fundingError) {
return {
Expand Down Expand Up @@ -77,7 +78,7 @@ export function useSignAndBroadcast() {
cacheWalletMnemonic(walletId, mnemonic)

// broadcast
return broadcast({ signedTransaction })
return broadcast({ signedTransaction, basis })
},
[
cancel,
Expand Down
11 changes: 9 additions & 2 deletions apps/walletd/dialogs/_sharedWalletSend/useBroadcast.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Transaction } from '@siafoundation/types'
import { Transaction, ChainIndex } from '@siafoundation/types'
import { useTxPoolBroadcast } from '@siafoundation/walletd-react'
import { useCallback } from 'react'

export function useBroadcast({ cancel }: { cancel: (t: Transaction) => void }) {
const txPoolBroadcast = useTxPoolBroadcast()

const broadcast = useCallback(
async ({ signedTransaction }: { signedTransaction: Transaction }) => {
async ({
signedTransaction,
basis,
}: {
signedTransaction: Transaction
basis: ChainIndex
}) => {
if (!signedTransaction) {
return {
error: 'No signed transaction',
Expand All @@ -15,6 +21,7 @@ export function useBroadcast({ cancel }: { cancel: (t: Transaction) => void }) {
// broadcast
const broadcastResponse = await txPoolBroadcast.post({
payload: {
basis,
transactions: [signedTransaction],
v2transactions: [],
},
Expand Down
2 changes: 2 additions & 0 deletions apps/walletd/dialogs/_sharedWalletSend/useFund.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function useFund() {
}
}
return {
basis: fundResponse.data.basis,
fundedTransaction: fundResponse.data.transaction,
toSign: fundResponse.data.toSign,
}
Expand Down Expand Up @@ -103,6 +104,7 @@ export function useFund() {
}
toSign.push(...fundResponse.data.toSign)
return {
basis: fundResponse.data.basis,
fundedTransaction: fundResponse.data.transaction,
toSign,
}
Expand Down
23 changes: 12 additions & 11 deletions apps/walletd/lib/signLedger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('signLedger', () => {
device,
transaction: mocks.walletFundResponse.transaction,
toSign: mocks.walletFundResponse.toSign,
siacoinOutputs: mocks.walletOutputsSiacoinResponse,
siafundOutputs: mocks.walletOutputsSiafundResponse,
siacoinOutputs: mocks.walletOutputsSiacoinResponse.outputs,
siafundOutputs: mocks.walletOutputsSiafundResponse.outputs,
addresses: getMockAddresses(mocks),
})
).toMatchSnapshot()
Expand All @@ -31,8 +31,8 @@ describe('signLedger', () => {
await signTransactionLedger({
device,
transaction: mocks.walletFundResponse.transaction,
siacoinOutputs: mocks.walletOutputsSiacoinResponse,
siafundOutputs: mocks.walletOutputsSiafundResponse,
siacoinOutputs: mocks.walletOutputsSiacoinResponse.outputs,
siafundOutputs: mocks.walletOutputsSiafundResponse.outputs,
addresses: getMockAddresses(mocks),
toSign: [mocks.walletFundResponse.toSign[0], 'not in siacoinOutputs'],
})
Expand All @@ -49,8 +49,8 @@ describe('signLedger', () => {
device,
transaction: mocks.walletFundResponse.transaction,
toSign: mocks.walletFundResponse.toSign,
siacoinOutputs: mocks.walletOutputsSiacoinResponse,
siafundOutputs: mocks.walletOutputsSiafundResponse,
siacoinOutputs: mocks.walletOutputsSiacoinResponse.outputs,
siafundOutputs: mocks.walletOutputsSiafundResponse.outputs,
addresses: [
{
id: 'id',
Expand All @@ -75,14 +75,15 @@ describe('signLedger', () => {
device,
transaction: mocks.walletFundResponse.transaction,
toSign: mocks.walletFundResponse.toSign,
siacoinOutputs: mocks.walletOutputsSiacoinResponse,
siafundOutputs: mocks.walletOutputsSiafundResponse,
siacoinOutputs: mocks.walletOutputsSiacoinResponse.outputs,
siafundOutputs: mocks.walletOutputsSiafundResponse.outputs,
addresses: [
{
id: 'id',
walletId: 'id',
address:
mocks.walletOutputsSiacoinResponse[1].siacoinOutput.address,
mocks.walletOutputsSiacoinResponse.outputs[1].siacoinOutput
.address,
metadata: {},
},
],
Expand All @@ -102,8 +103,8 @@ describe('signLedger', () => {
device,
transaction: mocks.walletFundResponse.transaction,
toSign: mocks.walletFundResponse.toSign,
siacoinOutputs: mocks.walletOutputsSiacoinResponse,
siafundOutputs: mocks.walletOutputsSiafundResponse,
siacoinOutputs: mocks.walletOutputsSiacoinResponse.outputs,
siafundOutputs: mocks.walletOutputsSiafundResponse.outputs,
addresses,
})
).toEqual({
Expand Down
23 changes: 12 additions & 11 deletions apps/walletd/lib/signSeed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('signSeed', () => {
toSign: mocks.walletFundResponse.toSign,
consensusState: mocks.consensusState,
consensusNetwork: mocks.consensusNetwork,
siacoinOutputs: mocks.walletOutputsSiacoinResponse,
siafundOutputs: mocks.walletOutputsSiafundResponse,
siacoinOutputs: mocks.walletOutputsSiacoinResponse.outputs,
siafundOutputs: mocks.walletOutputsSiafundResponse.outputs,
addresses: getMockAddresses(mocks),
})
).toMatchSnapshot()
Expand All @@ -32,8 +32,8 @@ describe('signSeed', () => {
transaction: mocks.walletFundResponse.transaction,
consensusState: mocks.consensusState,
consensusNetwork: mocks.consensusNetwork,
siacoinOutputs: mocks.walletOutputsSiacoinResponse,
siafundOutputs: mocks.walletOutputsSiafundResponse,
siacoinOutputs: mocks.walletOutputsSiacoinResponse.outputs,
siafundOutputs: mocks.walletOutputsSiafundResponse.outputs,
addresses: getMockAddresses(mocks),
toSign: ['not in siacoinOutputs'],
})
Expand All @@ -51,8 +51,8 @@ describe('signSeed', () => {
toSign: mocks.walletFundResponse.toSign,
consensusState: mocks.consensusState,
consensusNetwork: mocks.consensusNetwork,
siacoinOutputs: mocks.walletOutputsSiacoinResponse,
siafundOutputs: mocks.walletOutputsSiafundResponse,
siacoinOutputs: mocks.walletOutputsSiacoinResponse.outputs,
siafundOutputs: mocks.walletOutputsSiafundResponse.outputs,
addresses: [
{
id: 'id',
Expand All @@ -78,14 +78,15 @@ describe('signSeed', () => {
toSign: mocks.walletFundResponse.toSign,
consensusState: mocks.consensusState,
consensusNetwork: mocks.consensusNetwork,
siacoinOutputs: mocks.walletOutputsSiacoinResponse,
siafundOutputs: mocks.walletOutputsSiafundResponse,
siacoinOutputs: mocks.walletOutputsSiacoinResponse.outputs,
siafundOutputs: mocks.walletOutputsSiafundResponse.outputs,
addresses: [
{
id: 'id',
walletId: 'id',
address:
mocks.walletOutputsSiacoinResponse[1].siacoinOutput.address,
mocks.walletOutputsSiacoinResponse.outputs[1].siacoinOutput
.address,
metadata: {},
},
],
Expand All @@ -106,8 +107,8 @@ describe('signSeed', () => {
toSign: mocks.walletFundResponse.toSign,
consensusState: mocks.consensusState,
consensusNetwork: mocks.consensusNetwork,
siacoinOutputs: mocks.walletOutputsSiacoinResponse,
siafundOutputs: mocks.walletOutputsSiafundResponse,
siacoinOutputs: mocks.walletOutputsSiacoinResponse.outputs,
siafundOutputs: mocks.walletOutputsSiafundResponse.outputs,
addresses,
})
).toEqual({
Expand Down
6 changes: 5 additions & 1 deletion libs/types/src/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import {
SiafundOutput,
Signature,
StateElement,
UnlockConditions,
} from './core'

export type SpendPolicy = string
export type SpendPolicy = {
type: 'uc'
policy: UnlockConditions
}

export type V2SiacoinInput = {
parent: SiacoinElement
Expand Down
18 changes: 18 additions & 0 deletions libs/walletd-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ import {
WalletBalanceParams,
WalletBalancePayload,
WalletBalanceResponse,
WalletConstructV1TransactionParams,
WalletConstructV1TransactionPayload,
WalletConstructV1TransactionResponse,
WalletConstructV2TransactionParams,
WalletConstructV2TransactionPayload,
WalletConstructV2TransactionResponse,
WalletDeleteParams,
WalletDeletePayload,
WalletDeleteResponse,
Expand Down Expand Up @@ -93,6 +99,8 @@ import {
walletsIdAddressesAddrRoute,
walletsIdAddressesRoute,
walletsIdBalanceRoute,
walletsIdConstructTransactionRoute,
walletsIdConstructV2TransactionRoute,
walletsIdEventsRoute,
walletsIdEventsUnconfirmedRoute,
walletsIdFundRoute,
Expand Down Expand Up @@ -245,5 +253,15 @@ export function Walletd({ api, password }: { api: string; password?: string }) {
WalletReleasePayload,
WalletReleaseResponse
>(axios, 'post', walletsIdReleaseRoute),
walletConstructV1Transaction: buildRequestHandler<
WalletConstructV1TransactionParams,
WalletConstructV1TransactionPayload,
WalletConstructV1TransactionResponse
>(axios, 'post', walletsIdConstructTransactionRoute),
walletConstructV2Transaction: buildRequestHandler<
WalletConstructV2TransactionParams,
WalletConstructV2TransactionPayload,
WalletConstructV2TransactionResponse
>(axios, 'post', walletsIdConstructV2TransactionRoute),
}
}
Loading

0 comments on commit eb47a73

Please sign in to comment.