Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Aug 9, 2023
1 parent ccc4b7f commit db20a66
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ import type {
FoundryOutputBuilderParams,
NftOutputBuilderParams,
INode,
AccountAddress,
} from '@iota/wallet/out/types'

import { ParticipationEventType } from '@iota/wallet/out/types'

export interface IAccount {
addresses(): Promise<Address[]>
addresses(): Promise<AccountAddress[]>
addressesWithUnspentOutputs(): Promise<AddressWithUnspentOutputs[]>
buildAliasOutput(data: AliasOutputBuilderParams): Promise<AliasOutput>
buildBasicOutput(data: BasicOutputBuilderParams): Promise<BasicOutput>
Expand Down Expand Up @@ -124,4 +125,5 @@ export interface IAccount {
vote(eventId?: string, answers?: number[]): Promise<Transaction>
verifyEd25519Signature(signature: Ed25519Signature, message: HexEncodedString): Promise<boolean>
verifySecp256k1EcdsaSignature(signature: Secp256k1EcdsaSignature, message: HexEncodedString): Promise<boolean>
minimumRequiredStorageDeposit(output: Output): Promise<number>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { OUTPUT_TYPE_NFT } from '../../wallet/constants'
import { INft } from '../interfaces'
import { buildNftFromNftOutput } from '../utils'
import { setAccountNftsInAllAccountNfts } from './setAccountNftsInAllAccountNfts'
import { NftOutput } from '@iota/wallet/out/types'

export async function loadNftsForActiveProfile(): Promise<void> {
const allAccounts = get(activeAccounts)
Expand All @@ -31,7 +32,8 @@ async function loadNftsForAccount(account: IAccountState): Promise<void> {
.sort((a, b) => b.metadata.milestoneTimestampBooked - a.metadata.milestoneTimestampBooked)
for (const outputData of sortedNftOutputs) {
if (outputData.output.type === OUTPUT_TYPE_NFT) {
const nftId = getNftId(outputData.output.nftId, outputData.outputId)
const nftOutput = outputData.output as NftOutput
const nftId = getNftId(nftOutput.nftId, outputData.outputId)
if (!accountNfts.some((nft) => nft.id === nftId)) {
const nft = buildNftFromNftOutput(outputData as IWrappedOutput, account.depositAddress, false)
accountNfts.push(nft)
Expand Down
6 changes: 3 additions & 3 deletions packages/shared/lib/core/nfts/utils/buildNftFromNftOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { parseNftMetadata } from './parseNftMetadata'
import { composeUrlFromNftUri } from './composeUrlFromNftUri'
import { getSpendableStatusFromUnspentNftOutput } from './getSpendableStatusFromUnspentNftOutput'
import { ADDRESS_TYPE_NFT } from '@core/wallet/constants'
import { NftOutput } from '@iota/wallet/out/types'
import { Address, NftOutput } from '@iota/wallet/out/types'

export function buildNftFromNftOutput(
wrappedOutput: IWrappedOutput,
Expand All @@ -32,11 +32,11 @@ export function buildNftFromNftOutput(
}

const id = getNftId(nftOutput.getNftId(), wrappedOutput.outputId)
const address = getBech32AddressFromAddressTypes({ type: ADDRESS_TYPE_NFT, nftId: id })
const address = getBech32AddressFromAddressTypes({ type: ADDRESS_TYPE_NFT, nftId: id } as unknown as Address)
const issuer = getIssuerFromNftOutput(nftOutput)
const metadata = getMetadataFromNftOutput(nftOutput)
const parsedMetadata = parseNftMetadata(metadata)
const composedUrl = composeUrlFromNftUri(parsedMetadata?.uri)
const composedUrl = composeUrlFromNftUri(parsedMetadata.uri)
const filePath = `${get(activeProfileId)}/nfts/${id}`
const storageDeposit = Number(nftOutput.amount)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event, NewOutputWalletEvent } from '@iota/wallet/out/types'
import { AliasOutput, Event, NewOutputWalletEvent } from '@iota/wallet/out/types'
import { WalletEventType } from '@iota/wallet/out/types'

import { syncBalance } from '@core/account/actions/syncBalance'
Expand Down Expand Up @@ -32,34 +32,39 @@ export function handleNewOutputEvent(error: Error, rawEvent: Event): void {

export async function handleNewOutputEventInternal(accountIndex: number, payload: NewOutputWalletEvent): Promise<void> {
const account = get(activeAccounts)?.find((account) => account.index === accountIndex)
const output = payload.output
const outputData = payload.output

if (!account || !output) return
if (!account || !outputData) return

const address = getBech32AddressFromAddressTypes(output?.address)
const output = outputData.output as AliasOutput

const address = getBech32AddressFromAddressTypes(outputData?.address)
const isNewAliasOutput =
output.output.type === OUTPUT_TYPE_ALIAS &&
output.output.stateIndex === 0 &&
!get(allAccountActivities)[accountIndex].find((_activity) => _activity.id === output.outputId)
const isNftOutput = output.output.type === OUTPUT_TYPE_NFT
output.type === OUTPUT_TYPE_ALIAS &&
output.stateIndex === 0 &&
!get(allAccountActivities)[accountIndex].find((_activity) => _activity.id === outputData.outputId)
const isNftOutput = outputData.output.type === OUTPUT_TYPE_NFT

if ((account?.depositAddress === address && !output?.remainder) || isNewAliasOutput) {
if ((account?.depositAddress === address && !outputData?.remainder) || isNewAliasOutput) {
await syncBalance(account.index)

const processedOutput = preprocessGroupedOutputs([output], payload?.transactionInputs ?? [], account)
const processedOutput = preprocessGroupedOutputs([outputData], payload?.transactionInputs ?? [], account)

const activities = await generateActivities(processedOutput, account)
for (const activity of activities) {
if (activity.type === ActivityType.Basic || activity.type === ActivityType.Foundry) {
const asset = await getOrRequestAssetFromPersistedAssets(activity.assetId)
addPersistedAsset(asset)
if (asset) {
addPersistedAsset(asset)
}
}
}
addActivitiesToAccountActivitiesInAllAccountActivities(account.index, activities)
}

if (isNftOutput) {
const nft = buildNftFromNftOutput(output as IWrappedOutput, account.depositAddress)
const wrappedOutput = outputData.output as unknown as IWrappedOutput
const nft = buildNftFromNftOutput(wrappedOutput, account.depositAddress)
addOrUpdateNftInAllAccountNfts(account.index, nft)
void addNftsToDownloadQueue(accountIndex, [nft])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function generateSingleBasicActivity(

const baseTokenAmount = getAmountFromOutput(output) - storageDeposit - gasBudget

const nativeToken = getNativeTokenFromOutput(output)
const nativeToken = await getNativeTokenFromOutput(output)
const assetId = fallbackAssetId ?? nativeToken?.id ?? getCoinType()

let surplus: number | undefined = undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function generateSingleFoundryActivity(
const containsValue = true

const id = outputId || transactionId
const nativeToken = getNativeTokenFromOutput(output)
const nativeToken = await getNativeTokenFromOutput(output)
const assetId = nativeToken?.id ?? getCoinType()

const storageDeposit = getAmountFromOutput(output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function getLayer2ActivityInformation(
try {
parsedLayer2Metadata = parseLayer2Metadata(metadata)
destinationNetwork = getDestinationNetworkFromAddress(
sendingInfo.subject.type === 'address' ? sendingInfo.subject.address : undefined
sendingInfo.subject?.type === 'address' ? sendingInfo.subject.address : undefined
)
} catch (_err) {
parsedLayer2Metadata = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { getSubjectFromAddress } from '../../getSubjectFromAddress'
import { isSubjectInternal } from '../../isSubjectInternal'
import { getRecipientFromOutput } from '../../outputs'
import { getSenderAddressFromInputs, getSenderFromTransaction } from '../../transactions'
import { Output } from '@iota/wallet/out/types'
import { CommonOutput } from '@iota/wallet/out/types'

export function getSendingInformation(
processedTransaction: IProcessedTransaction,
output: Output,
output: CommonOutput,
account: IAccountState
): SenderInfo {
const { direction, wrappedInputs } = processedTransaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { buildFoundryId } from './getFoundryId'
import type { CommonOutput, FoundryOutput, INativeToken, SimpleTokenScheme } from '@iota/wallet/out/types'
import { OutputType } from '@iota/wallet/out/types'

export function getNativeTokenFromOutput(output: CommonOutput): INativeToken | undefined {
export async function getNativeTokenFromOutput(output: CommonOutput): Promise<INativeToken | undefined> {
if (output?.type === OutputType.Foundry) {
const foundryOutput = output as FoundryOutput
return {
id: buildFoundryId(output as FoundryOutput),
id: await buildFoundryId(output as FoundryOutput),
amount: (foundryOutput.tokenScheme as SimpleTokenScheme).mintedTokens,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { AddressUnlockCondition, CommonOutput } from '@iota/wallet/out/types'
import { UNLOCK_CONDITION_ADDRESS } from '../../constants'
import { Output } from '../../types'
import { getBech32AddressFromAddressTypes } from '../getBech32AddressFromAddressTypes'

export function getRecipientAddressFromOutput(output: Output): string {
export function getRecipientAddressFromOutput(output: CommonOutput): string | undefined {
for (const unlockCondition of output.unlockConditions) {
if (unlockCondition.type === UNLOCK_CONDITION_ADDRESS) {
return getBech32AddressFromAddressTypes(unlockCondition.address)
const addressUnlockCondition = unlockCondition as AddressUnlockCondition
return getBech32AddressFromAddressTypes(addressUnlockCondition.address)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { getRecipientAddressFromOutput } from './getRecipientAddressFromOutput'
import { Output, Subject } from '../../types'
import { Subject } from '../../types'
import { getSubjectFromAddress } from '../getSubjectFromAddress'
import { CommonOutput } from '@iota/wallet'

export function getRecipientFromOutput(output: Output): Subject {
export function getRecipientFromOutput(output: CommonOutput): Subject | undefined {
const recipientAddress = getRecipientAddressFromOutput(output)
return getSubjectFromAddress(recipientAddress)
if (recipientAddress) {
return getSubjectFromAddress(recipientAddress)
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { Output, Subject } from '../../types'
import { Subject } from '../../types'
import { getBech32AddressFromAddressTypes } from '../getBech32AddressFromAddressTypes'
import { UNLOCK_CONDITION_EXPIRATION, UNLOCK_CONDITION_STORAGE_DEPOSIT_RETURN } from '@core/wallet/constants'
import { getSubjectFromAddress } from '../getSubjectFromAddress'
import { CommonOutput, ExpirationUnlockCondition, StorageDepositReturnUnlockCondition } from '@iota/wallet'

export function getSenderFromOutput(output: Output): Subject | undefined {
export function getSenderFromOutput(output: CommonOutput): Subject | undefined {
for (const unlockCondition of output.unlockConditions) {
if (
unlockCondition?.type === UNLOCK_CONDITION_STORAGE_DEPOSIT_RETURN ||
unlockCondition?.type === UNLOCK_CONDITION_EXPIRATION
) {
return getSubjectFromAddress(getBech32AddressFromAddressTypes(unlockCondition?.returnAddress))
const storageOrExpirationUnlockCondition = unlockCondition as
| StorageDepositReturnUnlockCondition
| ExpirationUnlockCondition
const address = getBech32AddressFromAddressTypes(storageOrExpirationUnlockCondition?.returnAddress)
if (address) {
return getSubjectFromAddress(address)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ function convertTransactionOutputResponseToWrappedOutput(
}

function getUtxoInputsFromWrappedInputs(wrappedInputs: IWrappedOutput[]): UTXOInput[] {
// TODO-sdk This won't work probably
return (
wrappedInputs?.map((input) => ({
type: 0,
transactionId: input.metadata?.transactionId,
transactionInputIndex: input.metadata?.outputIndex,
})) ?? []
wrappedInputs?.map((input) => new UTXOInput(input.metadata?.transactionId, input.metadata?.outputIndex)) ?? []
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { IAccountState } from '@core/account/interfaces'
import { CommonOutput, ExpirationUnlockCondition, OutputType, UnlockConditionType } from '@iota/wallet/out/types'

export async function validateSendConfirmation(account: IAccountState, output: CommonOutput): Promise<void> {
const parseNumber: (value: string) => number = (value: string) => parseInt(value, 10) ?? 0
const parseNumber = (value: string) => parseInt(value, 10) ?? 0
const amount = parseNumber(output?.amount)
const balance = parseNumber(getSelectedAccount()?.balances?.baseCoin.available ?? '0')
const balance = parseNumber(getSelectedAccount()?.balances?.baseCoin.available.toString() ?? '0')
const { storageDeposit, giftedStorageDeposit } = await getStorageDepositFromOutput(account, output)

const expirationUnlockCondition = output.unlockConditions.find(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function activityOutputContainsValue(
const output = wrappedOutput.output as BasicOutput

const isAsync = isOutputAsync(output)
const nativeToken = getNativeTokenFromOutput(output)
const nativeToken = await getNativeTokenFromOutput(output)

const { storageDeposit } = await getStorageDepositFromOutput(account, output)
const rawAmount = getAmountFromOutput(output) - storageDeposit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Output, Subject } from '@core/wallet/types'
import { Subject } from '@core/wallet/types'
import { CommonOutput } from '@iota/wallet'
import { getSenderFromOutput } from '../outputs/getSenderFromOutput'

export function getSenderFromTransaction(
isIncoming: boolean,
accountAddress: string,
output: Output
output: CommonOutput
): Subject | undefined {
if (isIncoming) {
return getSenderFromOutput(output)
Expand Down

0 comments on commit db20a66

Please sign in to comment.