-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add confidential transfer
InitializeMint
- Loading branch information
1 parent
61cea38
commit fd54300
Showing
8 changed files
with
210 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { blob, Layout } from '@solana/buffer-layout'; | ||
import { encodeDecode } from '@solana/buffer-layout-utils'; | ||
import { PodElGamalPubkey } from 'solana-zk-token-sdk-experimental'; | ||
|
||
export const elgamalPublicKey = (property?: string): Layout<PodElGamalPubkey> => { | ||
const layout = blob(32, property); | ||
const { encode, decode } = encodeDecode(layout); | ||
|
||
const elgamalPublicKeyLayout = layout as Layout<unknown> as Layout<PodElGamalPubkey>; | ||
|
||
elgamalPublicKeyLayout.decode = (buffer: Buffer, offset: number) => { | ||
const src = decode(buffer, offset); | ||
return new PodElGamalPubkey(src); | ||
}; | ||
|
||
elgamalPublicKeyLayout.encode = (elgamalPublicKey: PodElGamalPubkey, buffer: Buffer, offset: number) => { | ||
const src = elgamalPublicKey.toBytes(); | ||
return encode(src, buffer, offset); | ||
}; | ||
|
||
return elgamalPublicKeyLayout; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './state.js'; | ||
export * from './instructions.js'; |
56 changes: 56 additions & 0 deletions
56
token/js/src/extensions/confidentialTransfer/instructions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { struct, u8 } from '@solana/buffer-layout'; | ||
import { bool, publicKey } from '@solana/buffer-layout-utils'; | ||
import { PublicKey, TransactionInstruction } from '@solana/web3.js'; | ||
import { programSupportsExtensions, TOKEN_2022_PROGRAM_ID } from '../../constants.js'; | ||
import { TokenUnsupportedInstructionError } from '../../errors.js'; | ||
import { TokenInstruction } from '../../instructions/types.js'; | ||
import { PodElGamalPubkey } from 'solana-zk-token-sdk-experimental'; | ||
import { elgamalPublicKey } from './elgamal.js'; | ||
|
||
export enum ConfidentialTransferInstruction { | ||
InitializeMint = 0, | ||
} | ||
|
||
export interface InitializeMintData { | ||
instruction: TokenInstruction.ConfidentialTransferExtension; | ||
confidentialTransferInstruction: ConfidentialTransferInstruction.InitializeMint; | ||
confidentialTransferMintAuthority: PublicKey | null; | ||
autoApproveNewAccounts: boolean; | ||
auditorElGamalPubkey: PodElGamalPubkey | null; | ||
} | ||
|
||
export const initializeMintData = struct<InitializeMintData>([ | ||
u8('instruction'), | ||
u8('confidentialTransferInstruction'), | ||
publicKey('confidentialTransferMintAuthority'), | ||
bool('autoApproveNewAccounts'), | ||
elgamalPublicKey('auditorElGamalPubkey'), | ||
]); | ||
|
||
export function createConfidentialTransferInitializeMintInstruction( | ||
mint: PublicKey, | ||
confidentialTransferMintAuthority: PublicKey | null, | ||
autoApproveNewAccounts: boolean, | ||
auditorElGamalPubkey: PodElGamalPubkey | null, | ||
programId = TOKEN_2022_PROGRAM_ID | ||
): TransactionInstruction { | ||
if (!programSupportsExtensions(programId)) { | ||
throw new TokenUnsupportedInstructionError(); | ||
} | ||
const keys = [{ pubkey: mint, isSigner: false, isWritable: true }]; | ||
|
||
const data = Buffer.alloc(initializeMintData.span); | ||
|
||
initializeMintData.encode( | ||
{ | ||
instruction: TokenInstruction.ConfidentialTransferExtension, | ||
confidentialTransferInstruction: ConfidentialTransferInstruction.InitializeMint, | ||
confidentialTransferMintAuthority: confidentialTransferMintAuthority ?? PublicKey.default, | ||
autoApproveNewAccounts: autoApproveNewAccounts, | ||
auditorElGamalPubkey: auditorElGamalPubkey ?? PodElGamalPubkey.default(), | ||
}, | ||
data | ||
); | ||
|
||
return new TransactionInstruction({ keys, programId, data }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { struct } from '@solana/buffer-layout'; | ||
import { publicKey, bool } from '@solana/buffer-layout-utils'; | ||
import type { PublicKey } from '@solana/web3.js'; | ||
import type { Mint } from '../../state/mint.js'; | ||
import { ExtensionType, getExtensionData } from '../extensionType.js'; | ||
import { PodElGamalPubkey } from 'solana-zk-token-sdk-experimental'; | ||
import { elgamalPublicKey } from './elgamal.js'; | ||
|
||
export interface ConfidentialTransferMint { | ||
confidentialTransferMintAuthority: PublicKey; | ||
autoApproveNewAccounts: boolean; | ||
auditorElGamalPubkey: PodElGamalPubkey; | ||
} | ||
|
||
export const ConfidentialTransferMintLayout = struct<ConfidentialTransferMint>([ | ||
publicKey('confidentialTransferMintAuthority'), | ||
bool('autoApproveNewAccounts'), | ||
elgamalPublicKey('auditorElGamalPubkey'), | ||
]); | ||
|
||
export const CONFIDENTIAL_TRANSFER_MINT_SIZE = ConfidentialTransferMintLayout.span; | ||
|
||
export function getConfidentialTransferMint(mint: Mint): ConfidentialTransferMint | null { | ||
const extensionData = getExtensionData(ExtensionType.ConfidentialTransferMint, mint.tlvData); | ||
if (extensionData !== null) { | ||
return ConfidentialTransferMintLayout.decode(extensionData); | ||
} else { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { expect } from 'chai'; | ||
import type { Connection, Signer } from '@solana/web3.js'; | ||
import { PublicKey } from '@solana/web3.js'; | ||
import { Keypair, SystemProgram, Transaction, sendAndConfirmTransaction } from '@solana/web3.js'; | ||
import { ElGamalKeypair, PodElGamalPubkey } from 'solana-zk-token-sdk-experimental'; | ||
import { ExtensionType, createInitializeMintInstruction, getMint, getMintLen } from '../../src'; | ||
import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../common'; | ||
|
||
import { | ||
createConfidentialTransferInitializeMintInstruction, | ||
getConfidentialTransferMint, | ||
} from '../../src/extensions/confidentialTransfer/index'; | ||
|
||
const TEST_TOKEN_DECIMALS = 2; | ||
const MINT_EXTENSIONS = [ExtensionType.ConfidentialTransferMint]; | ||
|
||
describe('confidentialTransfer', () => { | ||
let connection: Connection; | ||
let payer: Signer; | ||
let mint: PublicKey; | ||
let mintAuthority: Keypair; | ||
before(async () => { | ||
connection = await getConnection(); | ||
payer = await newAccountWithLamports(connection, 1000000000); | ||
}); | ||
|
||
async function setupConfidentialTransferMint( | ||
confidentialTransferMintAuthority: PublicKey | null, | ||
autoApproveNewAccounts: boolean, | ||
auditorPubkey: PodElGamalPubkey | null | ||
) { | ||
const mintKeypair = Keypair.generate(); | ||
mint = mintKeypair.publicKey; | ||
mintAuthority = Keypair.generate(); | ||
const mintLen = getMintLen(MINT_EXTENSIONS); | ||
|
||
const mintLamports = await connection.getMinimumBalanceForRentExemption(mintLen); | ||
const mintTransaction = new Transaction().add( | ||
SystemProgram.createAccount({ | ||
fromPubkey: payer.publicKey, | ||
newAccountPubkey: mint, | ||
space: mintLen, | ||
lamports: mintLamports, | ||
programId: TEST_PROGRAM_ID, | ||
}), | ||
createConfidentialTransferInitializeMintInstruction( | ||
mint, | ||
confidentialTransferMintAuthority, | ||
autoApproveNewAccounts, | ||
auditorPubkey | ||
), | ||
createInitializeMintInstruction(mint, TEST_TOKEN_DECIMALS, mintAuthority.publicKey, null, TEST_PROGRAM_ID) | ||
); | ||
|
||
await sendAndConfirmTransaction(connection, mintTransaction, [payer, mintKeypair], undefined); | ||
} | ||
|
||
describe('with authorities and auto approve', () => { | ||
let confidentialTransferMintAuthority: Keypair; | ||
let autoApproveNewAccounts: boolean; | ||
let auditorKeypair: ElGamalKeypair; | ||
let auditorPubkey: PodElGamalPubkey; | ||
beforeEach(async () => { | ||
confidentialTransferMintAuthority = Keypair.generate(); | ||
autoApproveNewAccounts = true; | ||
auditorKeypair = ElGamalKeypair.new_rand(); | ||
auditorPubkey = PodElGamalPubkey.encoded(auditorKeypair.pubkey_owned()); | ||
|
||
await setupConfidentialTransferMint( | ||
confidentialTransferMintAuthority.publicKey, | ||
autoApproveNewAccounts, | ||
auditorPubkey | ||
); | ||
}); | ||
|
||
it('initializes', async () => { | ||
const mintInfo = await getMint(connection, mint, undefined, TEST_PROGRAM_ID); | ||
const confidentialTransferMint = getConfidentialTransferMint(mintInfo); | ||
expect(confidentialTransferMint).to.not.be.null; | ||
if (confidentialTransferMint !== null) { | ||
expect(confidentialTransferMint.confidentialTransferMintAuthority).to.eql( | ||
confidentialTransferMintAuthority.publicKey | ||
); | ||
expect(confidentialTransferMint.autoApproveNewAccounts).to.eql(autoApproveNewAccounts); | ||
expect(confidentialTransferMint.auditorElGamalPubkey.equals(auditorPubkey)); // TODO: equals? | ||
} | ||
}); | ||
}); | ||
}); |