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

Implemented avoid throwing an error if private key is missing unless .send() is executed #931

Merged
merged 8 commits into from
May 31, 2023
16 changes: 16 additions & 0 deletions src/lib/account_update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ describe('AccountUpdate', () => {
expect(TokenId.toBase58(x)).toEqual(Ledger.fieldToBase58(x));
expect(TokenId.fromBase58(defaultTokenId).toString()).toEqual('1');
});

it('does not throw an error if private key is missing unless if .send is executed', async () => {
let Local = Mina.LocalBlockchain({ proofsEnabled: false });
Mina.setActiveInstance(Local);

const feePayerKey = Local.testAccounts[0].privateKey;
const feePayer = Local.testAccounts[0].publicKey;

let tx = await Mina.transaction(feePayer, () => {
let accountUpdate = AccountUpdate.fundNewAccount(feePayer);
});
tx.sign();
await expect(tx.send()).rejects.toThrow(
'Check signature: Invalid signature on fee payer for key'
);
});
});

// to check that we got something that looks like a Field
Expand Down
21 changes: 13 additions & 8 deletions src/lib/account_update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1877,10 +1877,11 @@ function addMissingSignatures(
pk.equals(accountUpdate.body.publicKey).toBoolean()
);
if (i === -1) {
let pk = PublicKey.toBase58(accountUpdate.body.publicKey);
throw Error(
`addMissingSignatures: Cannot add signature for fee payer (${pk}), private key is missing.`
);
// private key is missing, but we are not throwing an error here
// there is a change signature will be added by the wallet
// if not, error will be thrown by verifyAccountUpdate
// while .send() execution
return { body, authorization: Ledger.dummySignature() }
}
privateKey = additionalKeys[i];
}
Expand All @@ -1898,10 +1899,14 @@ function addMissingSignatures(
let i = additionalPublicKeys.findIndex((pk) =>
pk.equals(accountUpdate.body.publicKey).toBoolean()
);
if (i === -1)
throw Error(
`addMissingSignatures: Cannot add signature for ${accountUpdate.publicKey.toBase58()}, private key is missing.`
);
if (i === -1) {
// private key is missing, but we are not throwing an error here
// there is a change signature will be added by the wallet
// if not, error will be thrown by verifyAccountUpdate
// while .send() execution
Authorization.setSignature(accountUpdate, Ledger.dummySignature());
return accountUpdate as AccountUpdate & { lazyAuthorization: undefined };
}
privateKey = additionalKeys[i];
}
let transactionCommitment = accountUpdate.body.useFullCommitment.toBoolean()
Expand Down
8 changes: 8 additions & 0 deletions src/lib/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,14 @@ async function verifyAccountUpdate(

let { commitment, fullCommitment } = transactionCommitments;

// check if addMissingSignatures failed to include a signature
// due to a missing private key
if (accountUpdate.authorization === Ledger.dummySignature()) {
let pk = PublicKey.toBase58(accountUpdate.body.publicKey);
throw Error(
`verifyAccountUpdate: Detected a missing signature for (${pk}), private key was missing.`
);
}
// we are essentially only checking if the update is empty or an actual update
function includesChange<T extends {}>(
val: T | string | null | (string | null)[]
Expand Down
Loading