Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions js/compressed-token/src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1533,15 +1533,17 @@ export class CompressedTokenProgram {
inputCompressedTokenAccounts,
);

const CHANGE_INDEX = featureFlags.isV2() ? 1 : 0; // TODO: find better solution.

const rawData: CompressedTokenInstructionDataApprove = {
proof: recentValidityProof,
mint,
inputTokenDataWithContext,
cpiContext: null,
delegate: toAddress,
delegatedAmount: bn(amount),
delegateMerkleTreeIndex: 1, // TODO: find better solution.
changeAccountMerkleTreeIndex: 1,
delegateMerkleTreeIndex: CHANGE_INDEX,
changeAccountMerkleTreeIndex: CHANGE_INDEX,
delegateLamports: null,
};

Expand Down Expand Up @@ -1610,7 +1612,7 @@ export class CompressedTokenProgram {
mint,
inputTokenDataWithContext,
cpiContext: null,
outputAccountMerkleTreeIndex: 2, // Because of the delegate account.
outputAccountMerkleTreeIndex: featureFlags.isV2() ? 2 : 1,
};
const data = encodeRevokeInstructionData(rawData);

Expand Down
7 changes: 6 additions & 1 deletion js/stateless.js/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import { Buffer } from 'buffer';
import { ConfirmOptions, PublicKey } from '@solana/web3.js';
import { TreeInfo, TreeType } from './state/types';

export enum VERSION {
V1 = 'V1',
V2 = 'V2',
}

/**
/**
* @internal
* Feature flags. Only use if you know what you are doing.
*/
export const featureFlags = {
version: 'V2' as 'V1' | 'V2',
version: VERSION.V1,
isV2: () => featureFlags.version.toUpperCase() === 'V2',
};

Expand Down
103 changes: 61 additions & 42 deletions js/stateless.js/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import { LightWasm } from './test-helpers';
import {
getAllStateTreeInfos,
getStateTreeInfoByPubkey,
getTreeInfoByPubkey,
} from './utils/get-state-tree-infos';
import { TreeInfo } from './state/types';
import { validateNumbersForProof } from './utils';
Expand Down Expand Up @@ -988,7 +989,7 @@ export class Rpc extends Connection implements CompressionApiInterface {
treeInfos,
featureFlags.isV2()
? (proof as any).treeContext.tree
: (proof as any).tree!,
: (proof as any).merkleTree,
);
const value: MerkleContextWithMerkleProof = {
hash: bn(proof.hash.toArray('be', 32)),
Expand Down Expand Up @@ -1064,7 +1065,7 @@ export class Rpc extends Connection implements CompressionApiInterface {
stateTreeInfo,
bn(item.hash.toArray('be', 32)),
item.leafIndex,
true,
false,
),
item.owner,
bn(item.lamports),
Expand Down Expand Up @@ -1856,7 +1857,6 @@ export class Rpc extends Connection implements CompressionApiInterface {
jsonRpcResultAndContext(ValidityProofResultV2),
);
} else {
throw new Error('V1 is not supported');
res = create(
unsafeRes,
jsonRpcResultAndContext(ValidityProofResult),
Expand All @@ -1876,44 +1876,63 @@ export class Rpc extends Connection implements CompressionApiInterface {
}

const value = res.result.value as any;
return {
value: {
compressedProof: value.compressedProof,
leaves: value.accounts
.map((r: any) => r.hash)
.concat(value.addresses.map((r: any) => r.address)),
roots: value.accounts
.map((r: any) => r.root)
.concat(value.addresses.map((r: any) => r.root)),
rootIndices: value.accounts
.map((r: any) => r.rootIndex.rootIndex)
.concat(value.addresses.map((r: any) => r.rootIndex)),
proveByIndices: value.accounts
.map((r: any) => r.rootIndex.proveByIndex)
.concat(value.addresses.map((r: any) => false)),
treeInfos: value.accounts
.map((r: any) => r.merkleContext)
.concat(value.addresses.map((r: any) => r.merkleContext)),
leafIndices: value.accounts
.map((r: any) => r.leafIndex)
.concat(value.addresses.map((r: any) => 0)),
},
context: res.result.context,
};
// TODO: enable with v1 support.
// return {
// value: {
// compressedProof: value.compressedProof,
// roots: value.roots,
// rootIndices: value.rootIndices.map((r: any) => r.rootIndex),
// leafIndices: value.leafIndices,
// leaves: value.leaves,
// treeInfos: value.merkleContexts,
// proveByIndices: value.rootIndices.map(
// (r: any) => r.proveByIndex,
// ),
// },
// context: res.result.context,
// };

if (featureFlags.isV2()) {
return {
value: {
compressedProof: value.compressedProof,
leaves: value.accounts
.map((r: any) => r.hash)
.concat(value.addresses.map((r: any) => r.address)),
roots: value.accounts
.map((r: any) => r.root)
.concat(value.addresses.map((r: any) => r.root)),
rootIndices: value.accounts
.map((r: any) => r.rootIndex.rootIndex)
.concat(value.addresses.map((r: any) => r.rootIndex)),
proveByIndices: value.accounts
.map((r: any) => r.rootIndex.proveByIndex)
.concat(value.addresses.map((r: any) => false)),
treeInfos: value.accounts
.map((r: any) => r.merkleContext)
.concat(
value.addresses.map((r: any) => r.merkleContext),
),
leafIndices: value.accounts
.map((r: any) => r.leafIndex)
.concat(value.addresses.map((r: any) => 0)),
},
context: res.result.context,
};
} else {
// Temporary fix for v1 backward compatibility.
const allInfos = await this.getStateTreeInfos();
const infos = value.merkleTrees.map((r: PublicKey) => {
if (r.equals(defaultTestStateTreeAccounts().addressTree)) {
return {
tree: r,
queue: defaultTestStateTreeAccounts().addressQueue,
treeType: TreeType.AddressV1,
nextTreeInfo: null,
};
}
return getTreeInfoByPubkey(allInfos, r);
});

return {
value: {
compressedProof: value.compressedProof,
roots: value.roots,
rootIndices: value.rootIndices.map((r: any) => r),
leafIndices: value.leafIndices,
leaves: value.leaves,
treeInfos: infos,
proveByIndices: value.rootIndices.map(
(r: any) => r.proveByIndex,
),
},
context: res.result.context,
};
}
}
}
4 changes: 0 additions & 4 deletions js/stateless.js/src/test-helpers/test-rpc/test-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,6 @@ export class TestRpc extends Connection implements CompressionApiInterface {
const leafIndex = leaves.findIndex(leaf =>
bn(leaf).eq(hashes[i]),
);
// const stateTreeInfo = getStateTreeInfoByPubkey(
// cachedStateTreeInfos,
// tree,
// );

/// If leaf is part of current tree, return proof
if (leafIndex !== -1) {
Expand Down
2 changes: 0 additions & 2 deletions js/stateless.js/tests/unit/utils/conversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,6 @@ describe('convertInvokeCpiWithReadOnlyToInvoke', () => {
// First account (from input_compressed_accounts)
const firstAccount = result.inputCompressedAccountsWithMerkleContext[0];
expect(firstAccount.rootIndex).toBe(789);
console.log('result', result);
console.log('firstAccount', firstAccount);

expect(firstAccount.readOnly).toBe(false);
expect(firstAccount.compressedAccount.lamports).toEqual(new BN(2000));
Expand Down
Loading