Skip to content

Commit

Permalink
feat: Add memo field to transaction API
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMinkov committed Jun 13, 2022
1 parent 35e1695 commit c8b3afc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
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
14 changes: 10 additions & 4 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,7 +801,7 @@ function signFeePayer(
{
transactionFee = 0 as number | string,
feePayerNonce = undefined as number | string | undefined,
memo = '',
memo: feePayerMemo = undefined as string | undefined,
}
) {
let parties: Types.Json.Parties = JSON.parse(transactionJson);
Expand All @@ -806,10 +812,10 @@ 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}`;
parties.memo = Ledger.memoToBase58(memo);
return signJsonTransaction(JSON.stringify(parties), feePayerKey);
}

Expand Down

0 comments on commit c8b3afc

Please sign in to comment.