Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update expiration time unlock condition #8011

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
marc2332 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getRecipientAddressFromOutput,
getRecipientReturnAddressFromOutput,
isOutputAsync,
isOutputExpired,
} from '@core/wallet/utils'
import { getTimelockDateFromOutput } from '@core/wallet/utils/generateActivity/helper'
import { NftOutput, UnlockConditionType } from '@iota/sdk/out/types'
Expand All @@ -24,7 +25,7 @@ export function getSpendableStatusFromUnspentNftOutput(
(unlockCondition) => unlockCondition?.type === UnlockConditionType.StorageDepositReturn
)
if (expirationUnixTime) {
const isExpirationTimeExpired = expirationUnixTime < Date.now()
const isExpirationTimeExpired = isOutputExpired(nftOutput)
if (isExpirationTimeExpired) {
if (isReturnRecipient) {
isSpendable = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { get } from 'svelte/store'
import { ActivityAsyncStatus, ActivityDirection } from '../enums'
import { allWalletActivities, updateAsyncDataByActivityId } from '../stores'
import { getExpirationDateFromOutput } from '../utils'
import { getUnixTimestampFromNodeInfoAndSlotIndex, nodeInfoProtocolParameters } from '@core/network'

export async function setOutgoingAsyncActivitiesToClaimed(wallet: IWalletState): Promise<void> {
const walletActivities = get(allWalletActivities)[wallet.id]
Expand All @@ -15,29 +16,30 @@ export async function setOutgoingAsyncActivitiesToClaimed(wallet: IWalletState):
for (const activity of activities) {
try {
const detailedOutput = await wallet.getOutput(activity.outputId)
const isClaimed = detailedOutput && isOutputClaimed(detailedOutput)
if (isClaimed) {
updateAsyncDataByActivityId(wallet.id, activity.id, {
asyncStatus: ActivityAsyncStatus.Claimed,
claimedDate: new Date(), // TODO(2.0) Fix and use: new Date(detailedOutput.metadata.milestoneTimestampSpent * MILLISECONDS_PER_SECOND),
})
const nodeProtocolParameters = get(nodeInfoProtocolParameters)
if (nodeProtocolParameters && detailedOutput.metadata.spent) {
const claimedDate = new Date(
getUnixTimestampFromNodeInfoAndSlotIndex(nodeProtocolParameters, detailedOutput.metadata.spent.slot)
)
const isClaimed = detailedOutput && isOutputClaimed(detailedOutput, claimedDate)
if (isClaimed && claimedDate) {
updateAsyncDataByActivityId(wallet.id, activity.id, {
asyncStatus: ActivityAsyncStatus.Claimed,
claimedDate,
})
}
}
} catch (err) {
console.error(err)
}
}
}

function isOutputClaimed(output: OutputData): boolean {
function isOutputClaimed(output: OutputData, claimedDate: Date): boolean {
const expirationDate = getExpirationDateFromOutput(output?.output as BasicOutput)

if (expirationDate) {
return (
output.isSpent &&
new Date().getTime() /* TODO(2.0) Fix and use: output.metadata.milestoneTimestampSpent * MILLISECONDS_PER_SECOND */ <
expirationDate.getTime()
)
return !!output.metadata.spent && claimedDate.getTime() < expirationDate.getTime()
} else {
return output?.isSpent
return !!output?.metadata.spent
}
}
1 change: 1 addition & 0 deletions packages/shared/lib/core/wallet/utils/outputs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export * from './getTimeDifference'
export * from './hasBlockIssuerFeature'
export * from './isOutputAsync'
export * from './isOutputTimeLocked'
export * from './isOutputExpired'
export * from './preprocessGroupedOutputs'
export * from './preprocessOutgoingTransaction'
export * from './getOutputIdFromTransactionIdAndIndex'
Expand Down
22 changes: 22 additions & 0 deletions packages/shared/lib/core/wallet/utils/outputs/isOutputExpired.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CommonOutput, ExpirationUnlockCondition, UnlockConditionType } from '@iota/sdk/out/types'
import { get } from 'svelte/store'
import { getSlotIndexFromNodeInfo, nodeInfoProtocolParameters } from '@core/network'

export function isOutputExpired(output: CommonOutput): boolean | null {
const expirationUnlockCondition = output.unlockConditions.find(
(unlockCondition) => unlockCondition.type === UnlockConditionType.Expiration
) as ExpirationUnlockCondition
const nodeProtocolParameters = get(nodeInfoProtocolParameters)
const outputSlotIndex = expirationUnlockCondition?.slotIndex
if (!nodeProtocolParameters || !outputSlotIndex) return null

const currentSlotIndex = getSlotIndexFromNodeInfo(nodeProtocolParameters)
if (outputSlotIndex > currentSlotIndex + nodeProtocolParameters.maxCommittableAge) {
return false
} else if (outputSlotIndex <= currentSlotIndex + nodeProtocolParameters.minCommittableAge) {
return true
} else {
// The expiration is in the deadzone where it can't be unlocked
return null
}
}
Loading