Skip to content

Commit

Permalink
set stricter tsconfig, fix what broke
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Jan 4, 2024
1 parent b607da9 commit 5cb2732
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 92 deletions.
34 changes: 12 additions & 22 deletions connect/src/protocols/cctpTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ export class CircleTransfer<N extends Network = Network>

for (const idx in attestations) {
const ca = attestations[idx]!;
if (ca.attestation.attestation) continue; // already got it
if (ca.attestation?.attestation) continue; // already got it

const attestation = await this.wh.getCircleAttestation(ca.id.hash, timeout);
if (attestation === null) throw new Error("No attestation available after timeout exhausted");

attestations[idx].attestation.attestation = attestation;
attestations[idx]!.attestation!.attestation = attestation;
}

this.attestations = attestations;
Expand Down Expand Up @@ -355,24 +355,20 @@ export class CircleTransfer<N extends Network = Network>

if (!this.attestations) throw new Error("No Circle Attestations found");

const circleAttestations = this.attestations.filter((a) =>
isCircleMessageId(a.id),
) as AttestationReceipt<"CircleBridge">[];

const circleAttestations = this.attestations.filter((a) => isCircleMessageId(a.id));
if (circleAttestations.length > 1)
throw new Error(`Expected a single circle attestation, found ${circleAttestations.length}`);

const toChain = this.wh.getChain(this.transfer.to.chain);

const {
id,
attestation: { message, attestation },
} = circleAttestations[0]!;

const { id, attestation } = circleAttestations[0]! as AttestationReceipt<"CircleBridge">;
if (!attestation) throw new Error(`No Circle Attestation for ${id.hash}`);

const { message, attestation: signatures } = attestation;
if (!signatures) throw new Error(`No Circle Attestation for ${id.hash}`);

const toChain = this.wh.getChain(this.transfer.to.chain);
const tb = await toChain.getCircleBridge();
const xfer = tb.redeem(this.transfer.to.address, message, attestation);

const xfer = tb.redeem(this.transfer.to.address, message, signatures!);

const txids = await signSendWait<N, typeof toChain.chain>(toChain, xfer, signer);
this.txids?.push(...txids);
Expand Down Expand Up @@ -490,14 +486,8 @@ export class CircleTransfer<N extends Network = Network>
receipt = { ...receipt, state: TransferState.SourceInitiated, originTxs };
}

const att = xfer.attestations.filter((a) =>
isWormholeMessageId(a.id),
) as AttestationReceipt<"AutomaticCircleBridge">[];

const ctt = xfer.attestations.filter((a) =>
isCircleMessageId(a.id),
) as AttestationReceipt<"CircleBridge">[];

const att = xfer.attestations?.filter((a) => isWormholeMessageId(a.id)) ?? [];
const ctt = xfer.attestations?.filter((a) => isCircleMessageId(a.id)) ?? [];
const attestation = att.length > 0 ? att[0]! : ctt.length > 0 ? ctt[0]! : undefined;
if (attestation && attestation.attestation) {
receipt = { ...receipt, state: TransferState.Attested, attestation: attestation };
Expand Down
2 changes: 1 addition & 1 deletion core/definitions/src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function registerNative<P extends Platform>(platform: P, ctr: NativeAddre
}

export function nativeIsRegistered<C extends Chain>(chain: C): boolean {
const platform: Platform = chainToPlatform.get(chain);
const platform: Platform = chainToPlatform.get(chain)!;
return nativeFactory.has(platform);
}

Expand Down
54 changes: 25 additions & 29 deletions platforms/algorand/protocols/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ export class AlgorandWormholeCore<N extends Network, C extends AlgorandChains>

const storage = StorageLogicSig.forEmitter(this.coreAppId, _sender.toUint8Array());

const {
accounts: [storageAddress],
txs,
} = await AlgorandWormholeCore.maybeCreateStorageTx(
const { accounts, txs } = await AlgorandWormholeCore.maybeCreateStorageTx(
this.connection,
address,
this.coreAppId,
Expand All @@ -143,7 +140,7 @@ export class AlgorandWormholeCore<N extends Network, C extends AlgorandChains>
from: address,
appIndex: safeBigIntToNumber(this.coreAppId),
appArgs: [AlgorandWormholeCore.publishMessage, message, encoding.bignum.toBytes(0n, 8)],
accounts: [storageAddress],
accounts: accounts,
onComplete: OnApplicationComplete.NoOpOC,
suggestedParams,
});
Expand All @@ -162,7 +159,7 @@ export class AlgorandWormholeCore<N extends Network, C extends AlgorandChains>
.getApplicationByID(safeBigIntToNumber(this.coreAppId))
.do();
const appInfo = modelsv2.Application.from_obj_for_encoding(applInfoResp);
const val = appInfo.params.globalState.find((kv) => kv.key === AlgorandWormholeCore.feeKey);
const val = appInfo.params.globalState?.find((kv) => kv.key === AlgorandWormholeCore.feeKey);
return val ? BigInt(val.value.uint) : 0n;
}

Expand All @@ -180,20 +177,20 @@ export class AlgorandWormholeCore<N extends Network, C extends AlgorandChains>
}

// Expect target is core app
if (BigInt(ptr.txn.txn.apid) !== this.coreAppId) return msgs;
if (BigInt(ptr.txn.txn.apid ?? 0) !== this.coreAppId) return msgs;

// Expect logs
if (!ptr.logs || ptr.logs.length === 0) return msgs;

// Expect publish messeage as first arg
const args = ptr.txn.txn.apaa;
const args = ptr.txn.txn.apaa ?? [];
if (
args.length !== 3 ||
!encoding.bytes.equals(new Uint8Array(args[0]), AlgorandWormholeCore.publishMessage)
!encoding.bytes.equals(new Uint8Array(args[0]!), AlgorandWormholeCore.publishMessage)
)
return msgs;

const sequence = encoding.bignum.decode(ptr.logs[0]);
const sequence = encoding.bignum.decode(ptr.logs[0]!);
const emitter = new AlgorandAddress(ptr.txn.txn.snd).toUniversalAddress();
msgs.push({ chain: this.chain, emitter, sequence });

Expand Down Expand Up @@ -233,7 +230,7 @@ export class AlgorandWormholeCore<N extends Network, C extends AlgorandChains>
suggestedParams,
});
seedTxn.fee = seedTxn.fee * 2;
txs.push({ tx: seedTxn, signer: null });
txs.push({ tx: seedTxn });

// Opt in to the app and rekey to the app address that is using
// this as storage
Expand Down Expand Up @@ -285,10 +282,7 @@ export class AlgorandWormholeCore<N extends Network, C extends AlgorandChains>
sequence: vaa.sequence,
emitter: vaa.emitterAddress,
});
const {
accounts: [seqAddr],
txs: seqOptInTxs,
} = await AlgorandWormholeCore.maybeCreateStorageTx(
const { accounts: seqAddr, txs: seqOptInTxs } = await AlgorandWormholeCore.maybeCreateStorageTx(
client,
senderAddr,
appid,
Expand All @@ -299,22 +293,24 @@ export class AlgorandWormholeCore<N extends Network, C extends AlgorandChains>

// Get storage account for Guardian set
const gsStorage = StorageLogicSig.forGuardianSet(coreId, vaa.guardianSet);
const {
accounts: [guardianAddr],
txs: guardianOptInTxs,
} = await AlgorandWormholeCore.maybeCreateStorageTx(
client,
senderAddr,
coreId,
gsStorage,
suggestedParams,
);
const { accounts: guardianAddr, txs: guardianOptInTxs } =
await AlgorandWormholeCore.maybeCreateStorageTx(
client,
senderAddr,
coreId,
gsStorage,
suggestedParams,
);
txs.push(...guardianOptInTxs);

let accts: string[] = [seqAddr, guardianAddr];
let accts: string[] = [...seqAddr, ...guardianAddr];

// Get the Guardian keys
const keys: Uint8Array = await StorageLogicSig.decodeLocalState(client, coreId, guardianAddr);
const keys: Uint8Array = await StorageLogicSig.decodeLocalState(
client,
coreId,
guardianAddr[0]!,
);

// We don't pass the entire payload in but instead just pass it pre-digested. This gets around size
// limitations with lsigs AND reduces the cost of the entire operation on a congested network by reducing the
Expand Down Expand Up @@ -343,7 +339,7 @@ export class AlgorandWormholeCore<N extends Network, C extends AlgorandChains>
for (let i = 0; i < sigs.length; i++) {
// The first byte of the sig is the relative index of that signature in the signatures array
// Use that index to get the appropriate Guardian key
const sig = sigs[i * SIG_LEN];
const sig = sigs[i * SIG_LEN]!;
const key = keys.slice(
sig.guardianIndex * GuardianKeyLen + 1,
(sig.guardianIndex + 1) * GuardianKeyLen + 1,
Expand Down Expand Up @@ -386,7 +382,7 @@ export class AlgorandWormholeCore<N extends Network, C extends AlgorandChains>
suggestedParams,
});
appTxn.fee = appTxn.fee * (2 + numTxns); // Was 1
txs.push({ tx: appTxn, signer: null });
txs.push({ tx: appTxn });

return { accounts: accts, txs };
}
Expand Down
10 changes: 5 additions & 5 deletions platforms/algorand/protocols/core/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ export const StorageLogicSig = {
* @returns Promise with Uint8Array of data squirreled away
*/
decodeLocalState: async (client: Algodv2, appId: bigint, address: string) => {
let appState: modelsv2.ApplicationLocalState | undefined;
let appState: modelsv2.ApplicationLocalState;
try {
const ai = await client
.accountApplicationInformation(address, safeBigIntToNumber(appId))
.do();
const acctAppInfo = modelsv2.AccountApplicationResponse.from_obj_for_encoding(ai);
appState = acctAppInfo.appLocalState;
appState = acctAppInfo.appLocalState!;
} catch (e) {
return new Uint8Array();
}
Expand All @@ -173,19 +173,19 @@ export const StorageLogicSig = {
// so first put them in a map by numeric key
// then iterate over keys to concat them in the right order
let vals = new Map<number, Uint8Array>();
for (const kv of appState.keyValue) {
for (const kv of appState!.keyValue!) {
if (kv.key === metaKey) continue;

// Take the first byte off the key to be the
// numeric index
const key = encoding.b64.decode(kv.key)[0];
const key = encoding.b64.decode(kv.key)[0]!;
const value = encoding.b64.decode(kv.value.bytes);
vals.set(key, value);
}

const byteArrays: Uint8Array[] = [];
for (let i = 0; i < MAX_KEYS; i++) {
if (vals.has(i)) byteArrays.push(vals.get(i));
if (vals.has(i)) byteArrays.push(vals.get(i)!);
}

return encoding.bytes.concat(...byteArrays);
Expand Down
Loading

0 comments on commit 5cb2732

Please sign in to comment.