Skip to content

Commit

Permalink
fix: Apply fix for alias reference unlock not pre expanding (issues #…
Browse files Browse the repository at this point in the history
…1121) to NOVA
  • Loading branch information
msarcev committed Apr 22, 2024
1 parent f14daa4 commit c76f8a8
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions client/src/helpers/nova/preExpandedConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {
AccountOutput,
AccountUnlock,
AddressType,
AddressUnlockCondition,
CommonOutput,
ExpirationUnlockCondition,
Expand All @@ -7,12 +10,11 @@ import {
StateControllerAddressUnlockCondition,
Unlock,
UnlockConditionType,
Utils,
UnlockType,
} from "@iota/sdk-wasm-nova/web";
import { AddressHelper } from "~/helpers/nova/addressHelper";
import { IInput } from "~models/api/nova/IInput";
import { IPreExpandedConfig } from "~models/components";
import { resolveTransitiveUnlock } from "./resolveTransiviteUnlock";

const OUTPUT_EXPAND_CONDITIONS: UnlockConditionType[] = [
UnlockConditionType.Address,
Expand Down Expand Up @@ -40,22 +42,30 @@ export function getInputsPreExpandedConfig(inputs: IInput[], unlocks: Unlock[],
preExpandedConfig = {
isPreExpanded: !!matchExpandCondition,
};
if (input?.output?.output && "unlockConditions" in input.output.output) {
const commmonOutput = input.output.output as unknown as CommonOutput;

const signatureUnlock = resolveTransitiveUnlock(unlocks, idx);
const unlockSignatureAddress = Utils.addressToBech32(Utils.publicKeyHash(signatureUnlock.signature.publicKey), bech32Hrp);
if (commonOutput.unlockConditions.length > 0) {
// unlockSignatureAddress is allready calculated in the input
const unlockSignatureAddress = input.address.bech32;
// special case for account unlock where the signature is the state controller address but the unlock condition is the account address
const { referencedStateControllerAddress, referencedAccountAddress } = getReferencedAddresses(
inputs,
unlocks[idx],
bech32Hrp,
);

preExpandedConfig = {
...preExpandedConfig,
unlockConditions: commmonOutput.unlockConditions?.map((unlockCondition) => {
unlockConditions: commonOutput.unlockConditions.map((unlockCondition) => {
switch (unlockCondition.type) {
case UnlockConditionType.Address: {
const unlockAddress = AddressHelper.buildAddress(
bech32Hrp,
(unlockCondition as AddressUnlockCondition).address,
)?.bech32;
return unlockAddress === unlockSignatureAddress;
return (
unlockAddress === unlockSignatureAddress ||
(unlockAddress === referencedAccountAddress &&
referencedStateControllerAddress === unlockSignatureAddress)
);
}
case UnlockConditionType.Expiration: {
const unlockAddress = AddressHelper.buildAddress(
Expand Down Expand Up @@ -90,6 +100,27 @@ export function getInputsPreExpandedConfig(inputs: IInput[], unlocks: Unlock[],
return inputsPreExpandedConfig;
}

function getReferencedAddresses(
inputs: IInput[],
unlock: Unlock,
bech32Hrp: string,
): { referencedStateControllerAddress: string; referencedAccountAddress: string } {
let referencedStateControllerAddress = "";
let referencedAccountAddress = "";
if (unlock.type === UnlockType.Account) {
const referencedAccountInput = inputs[(unlock as AccountUnlock).reference];
const referencedAccountOutput = referencedAccountInput?.output?.output as unknown as AccountOutput;
referencedAccountAddress =
AddressHelper.buildAddress(bech32Hrp, referencedAccountOutput.accountId, AddressType.Account)?.bech32 || "";

const referencedStateControllerAddressUC = referencedAccountOutput.unlockConditions.find(
(uc) => uc.type === UnlockConditionType.StateControllerAddress,
) as StateControllerAddressUnlockCondition;
referencedStateControllerAddress = AddressHelper.buildAddress(bech32Hrp, referencedStateControllerAddressUC.address)?.bech32 || "";
}
return { referencedStateControllerAddress, referencedAccountAddress };
}

/**
* Get the preExpandedConfig for the outputs.
* Expand the output and its relevant receiver address related unlock conditions.
Expand Down

0 comments on commit c76f8a8

Please sign in to comment.