Skip to content

Commit ff0c936

Browse files
authored
refactor(sdk-coin-sol): extract zk program id constant and tidy builder
1 parent 7fd78dc commit ff0c936

4 files changed

Lines changed: 40 additions & 30 deletions

File tree

modules/sdk-coin-sol/src/lib/confidentialMintBuilder.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ export class ConfidentialMintBuilder extends TransactionBuilder {
195195
if (this._mintParams) {
196196
const params = this._mintParams;
197197
const recordOwner = params.recordAccountOwnerAddress || params.contextStateAuthorityAddress;
198-
const space = Buffer.from(params.rangeRecordData, 'hex').length;
198+
const rangeRecordBytes = Buffer.from(params.rangeRecordData, 'hex');
199+
const space = rangeRecordBytes.length;
199200

200201
instructions.push({
201202
type: InstructionBuilderTypes.CreateRecordAccount,
@@ -223,7 +224,7 @@ export class ConfidentialMintBuilder extends TransactionBuilder {
223224
params: {
224225
recordAccountAddress: params.rangeRecordAddress,
225226
recordAccountOwnerAddress: recordOwner,
226-
offset: Buffer.from(params.rangeRecordData, 'hex').length,
227+
offset: rangeRecordBytes.length,
227228
data: params.rangeRecordDataPart2,
228229
},
229230
});
@@ -247,6 +248,8 @@ export class ConfidentialMintBuilder extends TransactionBuilder {
247248
},
248249
});
249250

251+
// The range proof context state is stored in the same reusable record account used for the
252+
// proof data; the equality/validity proofs each have their own one-shot context state account.
250253
instructions.push({
251254
type: InstructionBuilderTypes.VerifyRangeProof,
252255
params: {

modules/sdk-coin-sol/src/lib/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export const JITO_MANAGER_FEE_ACCOUNT_TESTNET = 'DH7tmjoQ5zjqcgfYJU22JqmXhP5EY1t
3838
/** Solana Instructions sysvar address. Used by Token-2022 extension instructions that verify proofs in-tx. */
3939
export const INSTRUCTIONS_SYSVAR_ADDRESS = 'Sysvar1nstructions1111111111111111111111111';
4040

41+
/** Solana zk-elgamal-proof program address. */
42+
export const ZK_ELGAMAL_PROOF_PROGRAM_ID = 'ZkE1G11tXDEfKBqrPK5G1iFbnW4mdeQTw48MYUb9Gkp';
43+
4144
// Sdk instructions, mainly to check decoded types.
4245
export enum ValidInstructionTypesEnum {
4346
AdvanceNonceAccount = 'AdvanceNonceAccount',

modules/sdk-coin-sol/src/lib/solInstructionFactory.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ import {
2727
import assert from 'assert';
2828
import BigNumber from 'bignumber.js';
2929
import { struct, u8, blob } from '@solana/buffer-layout';
30-
import { INSTRUCTIONS_SYSVAR_ADDRESS, InstructionBuilderTypes, MEMO_PROGRAM_PK } from './constants';
30+
import {
31+
INSTRUCTIONS_SYSVAR_ADDRESS,
32+
InstructionBuilderTypes,
33+
MEMO_PROGRAM_PK,
34+
ZK_ELGAMAL_PROOF_PROGRAM_ID,
35+
} from './constants';
3136
import {
3237
AtaClose,
3338
AtaInit,
@@ -774,7 +779,6 @@ const CONFIDENTIAL_MINT_BURN_EXTENSION_DISCRIMINATOR = 3;
774779
/**
775780
* Discriminators for the zk-elgamal-proof program verification instructions.
776781
*/
777-
const ZK_ELGAMAL_PROOF_PROGRAM_ID = 'ZkE1G11tXDEfKBqrPK5G1iFbnW4mdeQTw48MYUb9Gkp';
778782
const VERIFY_CIPHERTEXT_COMMITMENT_EQUALITY_DISCRIMINATOR = 3;
779783
const VERIFY_BATCHED_GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_DISCRIMINATOR = 12;
780784
const VERIFY_BATCHED_RANGE_PROOF_U128_DISCRIMINATOR = 7;

modules/sdk-coin-sol/test/unit/transactionBuilder/confidentialMintBuilder.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ describe('Sol Confidential Mint Builder', () => {
2121
const validityContextStateAddress = '7Uxci7Cyi3M6utvAFP6uhzkH3yMzhfqshA4Uu4hqBfy8';
2222
const contextStateAuthorityAddress = authAccount.pub;
2323

24-
const hex36 = '00'.repeat(36);
25-
const hex64 = '00'.repeat(64);
24+
const zeroBytes36 = '00'.repeat(36);
25+
const zeroBytes64 = '00'.repeat(64);
2626
const rangeProofHex = '00'.repeat(1000);
2727

2828
const confidentialMintBuilder = () => {
@@ -39,7 +39,7 @@ describe('Sol Confidential Mint Builder', () => {
3939
tokenAddress,
4040
mintAddress,
4141
authorityAddress,
42-
decryptableZeroBalance: hex36,
42+
decryptableZeroBalance: zeroBytes36,
4343
maximumPendingBalanceCreditCounter: '2',
4444
proofInstructionOffset: 0,
4545
});
@@ -51,7 +51,7 @@ describe('Sol Confidential Mint Builder', () => {
5151
instruction.data.length.should.equal(47);
5252
instruction.data[0].should.equal(27); // ConfidentialTransferExtension discriminator
5353
instruction.data[1].should.equal(2); // ConfigureConfidentialTransferAccount extension discriminator
54-
instruction.data.slice(2, 38).toString('hex').should.equal(hex36);
54+
instruction.data.slice(2, 38).toString('hex').should.equal(zeroBytes36);
5555
instruction.data.readBigUInt64LE(38).toString().should.equal('2');
5656
instruction.data[46].should.equal(0);
5757

@@ -72,9 +72,9 @@ describe('Sol Confidential Mint Builder', () => {
7272
equalityContextStateAddress,
7373
validityContextStateAddress,
7474
contextStateAuthorityAddress,
75-
newDecryptableSupply: hex36,
76-
mintAmountAuditorCiphertextLo: hex64,
77-
mintAmountAuditorCiphertextHi: hex64,
75+
newDecryptableSupply: zeroBytes36,
76+
mintAmountAuditorCiphertextLo: zeroBytes64,
77+
mintAmountAuditorCiphertextHi: zeroBytes64,
7878
rangeRecordData: rangeProofHex,
7979
});
8080
const tx = await txBuilder.build();
@@ -99,9 +99,9 @@ describe('Sol Confidential Mint Builder', () => {
9999
confidentialMintInstruction.data.length.should.equal(169);
100100
confidentialMintInstruction.data[0].should.equal(42); // ConfidentialMint discriminator
101101
confidentialMintInstruction.data[1].should.equal(3); // confidentialMintBurn extension discriminator
102-
confidentialMintInstruction.data.slice(2, 38).toString('hex').should.equal(hex36);
103-
confidentialMintInstruction.data.slice(38, 102).toString('hex').should.equal(hex64);
104-
confidentialMintInstruction.data.slice(102, 166).toString('hex').should.equal(hex64);
102+
confidentialMintInstruction.data.slice(2, 38).toString('hex').should.equal(zeroBytes36);
103+
confidentialMintInstruction.data.slice(38, 102).toString('hex').should.equal(zeroBytes64);
104+
confidentialMintInstruction.data.slice(102, 166).toString('hex').should.equal(zeroBytes64);
105105
confidentialMintInstruction.data[166].should.equal(5);
106106
confidentialMintInstruction.data[167].should.equal(4);
107107
confidentialMintInstruction.data[168].should.equal(3);
@@ -120,9 +120,9 @@ describe('Sol Confidential Mint Builder', () => {
120120
equalityContextStateAddress,
121121
validityContextStateAddress,
122122
contextStateAuthorityAddress,
123-
newDecryptableSupply: hex36,
124-
mintAmountAuditorCiphertextLo: hex64,
125-
mintAmountAuditorCiphertextHi: hex64,
123+
newDecryptableSupply: zeroBytes36,
124+
mintAmountAuditorCiphertextLo: zeroBytes64,
125+
mintAmountAuditorCiphertextHi: zeroBytes64,
126126
rangeRecordData: part1,
127127
rangeRecordDataPart2: part2,
128128
closeRecordAccount: true,
@@ -153,7 +153,7 @@ describe('Sol Confidential Mint Builder', () => {
153153
tokenAddress,
154154
mintAddress,
155155
authorityAddress,
156-
decryptableZeroBalance: hex36,
156+
decryptableZeroBalance: zeroBytes36,
157157
maximumPendingBalanceCreditCounter: '65536',
158158
});
159159
txBuilder.confidentialMint({
@@ -165,9 +165,9 @@ describe('Sol Confidential Mint Builder', () => {
165165
equalityContextStateAddress,
166166
validityContextStateAddress,
167167
contextStateAuthorityAddress,
168-
newDecryptableSupply: hex36,
169-
mintAmountAuditorCiphertextLo: hex64,
170-
mintAmountAuditorCiphertextHi: hex64,
168+
newDecryptableSupply: zeroBytes36,
169+
mintAmountAuditorCiphertextLo: zeroBytes64,
170+
mintAmountAuditorCiphertextHi: zeroBytes64,
171171
rangeRecordData: rangeProofHex,
172172
});
173173
const tx = await txBuilder.build();
@@ -188,9 +188,9 @@ describe('Sol Confidential Mint Builder', () => {
188188
equalityContextStateAddress,
189189
validityContextStateAddress,
190190
contextStateAuthorityAddress,
191-
newDecryptableSupply: hex36,
192-
mintAmountAuditorCiphertextLo: hex64,
193-
mintAmountAuditorCiphertextHi: hex64,
191+
newDecryptableSupply: zeroBytes36,
192+
mintAmountAuditorCiphertextLo: zeroBytes64,
193+
mintAmountAuditorCiphertextHi: zeroBytes64,
194194
rangeRecordData: '00'.repeat(64),
195195
});
196196
const tx = await txBuilder.build();
@@ -211,9 +211,9 @@ describe('Sol Confidential Mint Builder', () => {
211211
equalityContextStateAddress,
212212
validityContextStateAddress,
213213
contextStateAuthorityAddress,
214-
newDecryptableSupply: hex36,
215-
mintAmountAuditorCiphertextLo: hex64,
216-
mintAmountAuditorCiphertextHi: hex64,
214+
newDecryptableSupply: zeroBytes36,
215+
mintAmountAuditorCiphertextLo: zeroBytes64,
216+
mintAmountAuditorCiphertextHi: zeroBytes64,
217217
rangeRecordData: rangeProofHex,
218218
});
219219
const tx = await txBuilder.build();
@@ -245,9 +245,9 @@ describe('Sol Confidential Mint Builder', () => {
245245
equalityContextStateAddress,
246246
validityContextStateAddress,
247247
contextStateAuthorityAddress,
248-
newDecryptableSupply: hex36,
249-
mintAmountAuditorCiphertextLo: hex64,
250-
mintAmountAuditorCiphertextHi: hex64,
248+
newDecryptableSupply: zeroBytes36,
249+
mintAmountAuditorCiphertextLo: zeroBytes64,
250+
mintAmountAuditorCiphertextHi: zeroBytes64,
251251
rangeRecordData: rangeProofHex,
252252
})
253253
).throwError(/Invalid or missing tokenAddress/);

0 commit comments

Comments
 (0)