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

Add memo to transaction creation #244

Merged
merged 2 commits into from
Jun 16, 2022
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
9 changes: 4 additions & 5 deletions src/lib/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function setCurrentTransaction(transaction: CurrentTransaction) {

type SenderSpec =
| PrivateKey
| { feePayerKey: PrivateKey; fee?: number | string | UInt64 }
| { feePayerKey: PrivateKey; fee?: number | string | UInt64; memo?: string }
| undefined;

function createUnsignedTransaction(
Expand All @@ -76,10 +76,7 @@ function createUnsignedTransaction(
}

function createTransaction(
feePayer:
| PrivateKey
| { feePayerKey: PrivateKey; fee?: number | string | UInt64 }
| undefined,
feePayer: SenderSpec,
f: () => unknown,
{ fetchMode = 'cached' as FetchMode } = {}
): Transaction {
Expand All @@ -89,6 +86,7 @@ function createTransaction(
let feePayerKey =
feePayer instanceof PrivateKey ? feePayer : feePayer?.feePayerKey;
let fee = feePayer instanceof PrivateKey ? undefined : feePayer?.fee;
let memo = feePayer instanceof PrivateKey ? '' : feePayer?.memo ?? '';

currentTransaction = {
sender: feePayerKey,
Expand Down Expand Up @@ -127,6 +125,7 @@ function createTransaction(
let transaction: Parties = {
otherParties: currentTransaction.parties,
feePayer: feePayerParty,
memo,
};

nextTransactionId.value += 1;
Expand Down
15 changes: 10 additions & 5 deletions src/lib/party.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,10 +744,12 @@ class Party {
type Parties = {
feePayer: FeePayerUnsigned;
otherParties: Party[];
memo: string;
};
type PartiesSigned = {
feePayer: FeePayer;
otherParties: (Party & { authorization: Control | LazyProof })[];
memo: string;
};

// TODO: probably shouldn't hard-code dummy signature
Expand All @@ -771,15 +773,16 @@ function toPartyUnsafe({ body, authorization }: Party): Types.Party {
function toPartiesUnsafe({
feePayer,
otherParties,
memo,
}: {
feePayer: FeePayerUnsigned;
otherParties: Party[];
memo: string;
}): Types.Parties {
return {
feePayer: toFeePayerUnsafe(feePayer),
otherParties: otherParties.map(toPartyUnsafe),
// TODO expose to Mina.transaction
memo: Ledger.memoToBase58(''),
memo: Ledger.memoToBase58(memo),
};
}

Expand Down Expand Up @@ -842,16 +845,18 @@ function addMissingSignatures(
party.authorization = { signature };
return party as P & { authorization: Control };
}
let { feePayer, otherParties } = parties;
let { feePayer, otherParties, memo } = parties;
return {
feePayer: addFeePayerSignature(feePayer),
otherParties: otherParties.map((p) => addSignature(p)),
memo,
};
}

type PartiesProved = {
feePayer: FeePayerUnsigned;
otherParties: (Party & { authorization: Control | LazySignature })[];
memo: string;
};

async function addMissingProofs(parties: Parties): Promise<PartiesProved> {
Expand Down Expand Up @@ -889,7 +894,7 @@ async function addMissingProofs(parties: Parties): Promise<PartiesProved> {
party.authorization = { proof: Pickles.proofToString(proof) };
return party as P & { authorization: Control | LazySignature };
}
let { feePayer, otherParties } = parties;
let { feePayer, otherParties, memo } = parties;
// compute proofs serially. in parallel would clash with our global variable hacks
let otherPartiesProved: (Party & {
authorization: Control | LazySignature;
Expand All @@ -898,7 +903,7 @@ async function addMissingProofs(parties: Parties): Promise<PartiesProved> {
let partyProved = await addProof(otherParties[i], i);
otherPartiesProved.push(partyProved);
}
return { feePayer, otherParties: otherPartiesProved };
return { feePayer, otherParties: otherPartiesProved, memo };
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/lib/zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ async function deploy<S extends typeof SmartContract>(
feePayerKey,
transactionFee,
feePayerNonce,
memo,
}: {
zkappKey: PrivateKey;
verificationKey: { data: string; hash: string | Field };
Expand All @@ -664,6 +665,7 @@ async function deploy<S extends typeof SmartContract>(
shouldSignFeePayer?: boolean;
transactionFee?: string | number;
feePayerNonce?: string | number;
memo?: string;
}
) {
let address = zkappKey.toPublicKey();
Expand Down Expand Up @@ -698,6 +700,7 @@ async function deploy<S extends typeof SmartContract>(
zkapp.self.balance.addInPlace(amount);
}
});
tx.transaction.memo = memo ?? '';
if (shouldSignFeePayer) {
if (feePayerKey === undefined || transactionFee === undefined) {
throw Error(
Expand Down Expand Up @@ -767,11 +770,12 @@ async function callUnproved<S extends typeof SmartContract>(
}

function addFeePayer(
{ feePayer, otherParties }: Parties,
{ feePayer, otherParties, memo }: Parties,
feePayerKey: PrivateKey | string,
{
transactionFee = 0 as number | string,
feePayerNonce = undefined as number | string | undefined,
memo: feePayerMemo = undefined as string | undefined,
}
) {
feePayer = cloneCircuitValue(feePayer);
Expand All @@ -782,11 +786,13 @@ function addFeePayer(
let senderAccount = Mina.getAccount(senderAddress);
feePayerNonce = senderAccount.nonce.toString();
}
let newMemo = memo;
if (feePayerMemo) newMemo = Ledger.memoToBase58(feePayerMemo);
feePayer.body.nonce = UInt32.fromString(`${feePayerNonce}`);
feePayer.body.publicKey = senderAddress;
feePayer.body.fee = UInt64.fromString(`${transactionFee}`);
Party.signFeePayerInPlace(feePayer, feePayerKey);
return { feePayer, otherParties };
return { feePayer, otherParties, memo: newMemo };
}

function signFeePayer(
Expand All @@ -795,6 +801,7 @@ function signFeePayer(
{
transactionFee = 0 as number | string,
feePayerNonce = undefined as number | string | undefined,
memo: feePayerMemo = undefined as string | undefined,
}
) {
let parties: Types.Json.Parties = JSON.parse(transactionJson);
Expand All @@ -805,6 +812,7 @@ function signFeePayer(
let senderAccount = Mina.getAccount(senderAddress);
feePayerNonce = senderAccount.nonce.toString();
}
if (feePayerMemo) parties.memo = Ledger.memoToBase58(feePayerMemo);
parties.feePayer.body.nonce = `${feePayerNonce}`;
parties.feePayer.body.publicKey = Ledger.publicKeyToString(senderAddress);
parties.feePayer.body.fee = `${transactionFee}`;
Expand Down