Skip to content

Commit

Permalink
Merge pull request #244 from o1-labs/feat/add-memo-to-txn
Browse files Browse the repository at this point in the history
Add memo to transaction creation
  • Loading branch information
mitschabaude authored Jun 16, 2022
2 parents 0595b3b + c8b3afc commit 55223d8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/lib/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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 @@ -79,10 +79,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 @@ -92,6 +89,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 @@ -137,6 +135,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 @@ -761,10 +761,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 @@ -788,15 +790,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 @@ -859,16 +862,18 @@ function addMissingSignatures(
party.authorization = { signature };
return party as Party & { 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 @@ -906,7 +911,7 @@ async function addMissingProofs(parties: Parties): Promise<PartiesProved> {
party.authorization = { proof: Pickles.proofToString(proof) };
return party as Party & { 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 @@ -915,7 +920,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 @@ -370,6 +370,7 @@ async function deploy<S extends typeof SmartContract>(
feePayerKey,
transactionFee,
feePayerNonce,
memo,
}: {
zkappKey: PrivateKey;
verificationKey: { data: string; hash: string | Field };
Expand All @@ -378,6 +379,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 @@ -412,6 +414,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 All @@ -427,11 +430,12 @@ async function deploy<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 @@ -442,11 +446,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 @@ -455,6 +461,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 @@ -465,6 +472,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

0 comments on commit 55223d8

Please sign in to comment.