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

refactor: handle multiple unlock types in getInputsPreExpandedConfig #1113

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 5 additions & 12 deletions client/src/helpers/stardust/preExpandedConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down
19 changes: 19 additions & 0 deletions client/src/helpers/stardust/resolveTransiviteUnlock.ts
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 7 additions & 23 deletions client/src/helpers/stardust/transactionsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ import {
Output,
OutputType,
PayloadType,
ReferenceUnlock,
RegularTransactionEssence,
SignatureUnlock,
StateControllerAddressUnlockCondition,
TagFeature,
TransactionPayload,
TreasuryOutput,
Unlock,
UnlockCondition,
UnlockConditionType,
UnlockType,
Utils,
UTXOInput,
} from "@iota/sdk-wasm/web";
Expand All @@ -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[];
Expand Down Expand Up @@ -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;
Expand Down
Loading