diff --git a/client/src/helpers/stardust/preExpandedConfig.ts b/client/src/helpers/stardust/preExpandedConfig.ts index a1badc321..c52a0d669 100644 --- a/client/src/helpers/stardust/preExpandedConfig.ts +++ b/client/src/helpers/stardust/preExpandedConfig.ts @@ -3,18 +3,16 @@ import { CommonOutput, ExpirationUnlockCondition, GovernorAddressUnlockCondition, - ReferenceUnlock, - SignatureUnlock, StateControllerAddressUnlockCondition, Unlock, UnlockConditionType, - UnlockType, Utils, } from "@iota/sdk-wasm/web"; import { Bech32AddressHelper } from "~/helpers/stardust/bech32AddressHelper"; import { IInput } from "~models/api/stardust/IInput"; import { IOutput } from "~models/api/stardust/IOutput"; import { IPreExpandedConfig } from "~models/components"; +import { resolveTransitiveUnlock } from "./resolveTransiviteUnlock"; const OUTPUT_EXPAND_CONDITIONS: UnlockConditionType[] = [ UnlockConditionType.Address, @@ -44,15 +42,10 @@ export function getInputsPreExpandedConfig(inputs: IInput[], unlocks: Unlock[], }; if (input?.output?.output && "unlockConditions" in input.output.output) { const commmonOutput = input.output.output as unknown as CommonOutput; - let unlock = unlocks[idx]; - if (unlock.type === UnlockType.Reference) { - const referenceUnlock = unlock as ReferenceUnlock; - unlock = unlocks[referenceUnlock.reference]; - } - const unlockSignatureAddress = Utils.hexPublicKeyToBech32Address( - (unlock as SignatureUnlock).signature.publicKey, - bech32Hrp, - ); + + const signatureUnlock = resolveTransitiveUnlock(unlocks, idx); + const unlockSignatureAddress = Utils.hexPublicKeyToBech32Address(signatureUnlock.signature.publicKey, bech32Hrp); + preExpandedConfig = { ...preExpandedConfig, unlockConditions: commmonOutput.unlockConditions?.map((unlockCondition) => { diff --git a/client/src/helpers/stardust/resolveTransiviteUnlock.ts b/client/src/helpers/stardust/resolveTransiviteUnlock.ts new file mode 100644 index 000000000..2e7f8d206 --- /dev/null +++ b/client/src/helpers/stardust/resolveTransiviteUnlock.ts @@ -0,0 +1,19 @@ +import { ReferenceUnlock, SignatureUnlock, Unlock, UnlockType } from "@iota/sdk-wasm/web"; + +export function resolveTransitiveUnlock(unlocks: Unlock[], unlockIndex: number): SignatureUnlock { + const unlock = unlocks[unlockIndex]; + let signatureUnlock: SignatureUnlock; + if (unlock.type === UnlockType.Signature) { + signatureUnlock = unlock as SignatureUnlock; + } else { + let refUnlockIdx = unlockIndex; + // unlock references can be transitive, + // so we need to follow the path until we find the signature + do { + const referenceUnlock = unlocks[refUnlockIdx] as ReferenceUnlock; + signatureUnlock = unlocks[referenceUnlock.reference] as SignatureUnlock; + refUnlockIdx = referenceUnlock.reference; + } while (!signatureUnlock.signature); + } + return signatureUnlock; +} diff --git a/client/src/helpers/stardust/transactionsHelper.ts b/client/src/helpers/stardust/transactionsHelper.ts index 1a04615db..c3dc20a19 100644 --- a/client/src/helpers/stardust/transactionsHelper.ts +++ b/client/src/helpers/stardust/transactionsHelper.ts @@ -13,9 +13,7 @@ import { Output, OutputType, PayloadType, - ReferenceUnlock, RegularTransactionEssence, - SignatureUnlock, StateControllerAddressUnlockCondition, TagFeature, TransactionPayload, @@ -23,7 +21,6 @@ import { Unlock, UnlockCondition, UnlockConditionType, - UnlockType, Utils, UTXOInput, } from "@iota/sdk-wasm/web"; @@ -36,6 +33,7 @@ import { IOutput } from "~models/api/stardust/IOutput"; import { MAINNET } from "~models/config/networkType"; import { StardustApiClient } from "~services/stardust/stardustApiClient"; import { Bech32AddressHelper } from "../stardust/bech32AddressHelper"; +import { resolveTransitiveUnlock } from "./resolveTransiviteUnlock"; interface TransactionInputsAndOutputsResponse { inputs: IInput[]; @@ -82,28 +80,14 @@ export class TransactionsHelper { // unlock Addresses computed from public keys in unlocks for (let i = 0; i < unlocks.length; i++) { - const unlock = payload.unlocks[i]; - let signatureUnlock: SignatureUnlock; + const signatureUnlock = resolveTransitiveUnlock(unlocks, i); - if (unlock.type === UnlockType.Signature) { - signatureUnlock = unlock as SignatureUnlock; - } else { - let refUnlockIdx = i; - // unlock references can be transitive, - // so we need to follow the path until we find the signature - do { - const referenceUnlock = payload.unlocks[refUnlockIdx] as ReferenceUnlock; - signatureUnlock = payload.unlocks[referenceUnlock.reference] as SignatureUnlock; - refUnlockIdx = referenceUnlock.reference; - } while (!signatureUnlock.signature); - } - - unlockAddresses.push( - Bech32AddressHelper.buildAddress( - _bechHrp, - Utils.hexPublicKeyToBech32Address(signatureUnlock.signature.publicKey, _bechHrp), - ), + const address = Bech32AddressHelper.buildAddress( + _bechHrp, + Utils.hexPublicKeyToBech32Address(signatureUnlock.signature.publicKey, _bechHrp), ); + + unlockAddresses.push(address); } const payloadEssence = payload.essence as RegularTransactionEssence;