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

feat: add solana token 2022 support #3968

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions rust/chains/tw_solana/src/defined_addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ macro_rules! define {
define!(SYSTEM_PROGRAM_ID_ADDRESS = "11111111111111111111111111111111");
define!(STAKE_PROGRAM_ID_ADDRESS = "Stake11111111111111111111111111111111111111");
define!(TOKEN_PROGRAM_ID_ADDRESS = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
define!(TOKEN_2022_PROGRAM_ID_ADDRESS = "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb");
define!(ASSOCIATED_TOKEN_PROGRAM_ID_ADDRESS = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
define!(SYSVAR_RENT_ID_ADDRESS = "SysvarRent111111111111111111111111111111111");
define!(SYSVAR_CLOCK_ID_ADDRESS = "SysvarC1ock11111111111111111111111111111111");
Expand Down
9 changes: 6 additions & 3 deletions rust/chains/tw_solana/src/modules/compiled_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,21 @@ impl CompiledKeys {
(meta.is_signer == is_signer && meta.is_writable == is_writable).then_some(*account)
};

let writable_signer_keys: Vec<SolanaAddress> = ordered_keys
let mut writable_signer_keys: Vec<SolanaAddress> = ordered_keys
.iter()
.filter_map(|key| filter(key, true, true))
.collect();
let readonly_signer_keys: Vec<SolanaAddress> = ordered_keys
writable_signer_keys.sort();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please revert the keys sorting? Even though, they're sorted in official Solana lib, but in WalletCore I disabled it explicitly to save backward compatibility to have the same transaction signature after the Rust transition.
Please note that the keys are not required to be sorted to be valid transaction

let mut readonly_signer_keys: Vec<SolanaAddress> = ordered_keys
.iter()
.filter_map(|key| filter(key, true, false))
.collect();
let writable_non_signer_keys: Vec<SolanaAddress> = ordered_keys
readonly_signer_keys.sort();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

let mut writable_non_signer_keys: Vec<SolanaAddress> = ordered_keys
.iter()
.filter_map(|key| filter(key, false, true))
.collect();
writable_non_signer_keys.sort();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

let readonly_non_signer_keys: Vec<SolanaAddress> = ordered_keys
.iter()
.filter_map(|key| filter(key, false, false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ impl TokenInstructionBuilder {
other_main_pubkey: SolanaAddress,
token_mint_pubkey: SolanaAddress,
token_pubkey: SolanaAddress,
is_token_2022: bool,
satoshiotomakan marked this conversation as resolved.
Show resolved Hide resolved
) -> Instruction {
let account_metas = vec![
AccountMeta::new(funding_pubkey, true),
AccountMeta::new(token_pubkey, false),
AccountMeta::readonly(other_main_pubkey, false),
AccountMeta::readonly(token_mint_pubkey, false),
AccountMeta::readonly(*SYSTEM_PROGRAM_ID_ADDRESS, false),
AccountMeta::readonly(*TOKEN_PROGRAM_ID_ADDRESS, false),
AccountMeta::readonly(if is_token_2022 { *TOKEN_2022_PROGRAM_ID_ADDRESS } else { *TOKEN_PROGRAM_ID_ADDRESS }, false),
AccountMeta::readonly(*SYSVAR_RENT_ID_ADDRESS, false),
];
let data = Data::default();
Expand All @@ -96,15 +97,16 @@ impl TokenInstructionBuilder {
signer: SolanaAddress,
amount: u64,
decimals: u8,
is_token_2022: bool,
satoshiotomakan marked this conversation as resolved.
Show resolved Hide resolved
) -> Instruction {
let account_metas = vec![
AccountMeta::new(sender_token_pubkey, false),
AccountMeta::readonly(token_mint_pubkey, false),
AccountMeta::new(recipient_token_pubkey, false),
AccountMeta::new(signer, true),
AccountMeta::readonly(signer, true),
];

let data = TokenInstruction::TransferChecked { amount, decimals }.pack();
Instruction::new(*TOKEN_PROGRAM_ID_ADDRESS, data, account_metas)
Instruction::new(if is_token_2022 { *TOKEN_2022_PROGRAM_ID_ADDRESS } else { *TOKEN_PROGRAM_ID_ADDRESS }, data, account_metas)
}
}
4 changes: 4 additions & 0 deletions rust/chains/tw_solana/src/modules/message_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ impl<'a> MessageBuilder<'a> {
other_main_address,
token_mint_address,
token_address,
create_token_acc.is_token_2022
);
let mut builder = InstructionBuilder::default();
builder
Expand Down Expand Up @@ -378,6 +379,7 @@ impl<'a> MessageBuilder<'a> {
signer,
token_transfer.amount,
decimals,
token_transfer.is_token_2022
)
.with_references(references);

Expand Down Expand Up @@ -432,6 +434,7 @@ impl<'a> MessageBuilder<'a> {
recipient_main_address,
token_mint_address,
recipient_token_address,
create_and_transfer.is_token_2022
);
let transfer_instruction = TokenInstructionBuilder::transfer_checked(
sender_token_address,
Expand All @@ -440,6 +443,7 @@ impl<'a> MessageBuilder<'a> {
signer,
create_and_transfer.amount,
decimals,
create_and_transfer.is_token_2022
)
.with_references(references);

Expand Down
37 changes: 37 additions & 0 deletions rust/tw_any_coin/tests/chains/solana/solana_sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ fn test_solana_sign_create_token_account() {
main_address: "B1iGmDJdvmxyUiYM8UEo2Uw2D58EmUrw4KyLYMmrhf8V".into(),
token_mint_address: "SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt".into(),
token_address: "EDNd1ycsydWYwVmrYZvqYazFqwk1QjBgAUKFjBoz1jKP".into(),
is_token_2022: false
};
let input = Proto::SigningInput {
private_key: b58("9YtuoD4sH4h88CVM8DSnkfoAaLY7YeGC2TarDJ8eyMS5"),
Expand All @@ -413,6 +414,7 @@ fn test_solana_sign_create_token_account_5ktpn1() {
main_address: "Eg5jqooyG6ySaXKbQUu4Lpvu2SqUPZrNkM4zXs9iUDLJ".into(),
token_mint_address: "SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt".into(),
token_address: "ANVCrmRw7Ww7rTFfMbrjApSPXEEcZpBa6YEiBdf98pAf".into(),
is_token_2022: false
};
let input = Proto::SigningInput {
private_key: "4b9d6f57d28b06cbfa1d4cc710953e62d653caf853415c56ffd9d150acdeb7f7"
Expand All @@ -438,6 +440,7 @@ fn test_solana_sign_create_token_account_for_other_3e6ufv() {
main_address: "3xJ3MoUVFPNFEHfWdtNFa8ajXUHsJPzXcBSWMKLd76ft".into(),
token_mint_address: "SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt".into(),
token_address: "67BrwFYt7qUnbAcYBVx7sQ4jeD2KWN1ohP6bMikmmQV3".into(),
is_token_2022: false
};
let input = Proto::SigningInput {
private_key: "4b9d6f57d28b06cbfa1d4cc710953e62d653caf853415c56ffd9d150acdeb7f7"
Expand Down Expand Up @@ -469,6 +472,7 @@ fn test_solana_sign_create_token_account_with_priority_fee_price() {
// WBTC
token_mint_address: "3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh".into(),
token_address: "94JfTH8qCeBQDBatvULREG43msjSQscT7oHnqx7jppX".into(),
is_token_2022: false
};

let input = Proto::SigningInput {
Expand Down Expand Up @@ -496,6 +500,7 @@ fn test_solana_sign_token_transfer() {
// 0.004
amount: 4000,
decimals: 6,
is_token_2022: false,
..Proto::TokenTransfer::default()
};
let input = Proto::SigningInput {
Expand All @@ -522,6 +527,7 @@ fn test_solana_sign_token_transfer_2pmvzp() {
// 0.0061
amount: 6100,
decimals: 6,
is_token_2022: false,
..Proto::TokenTransfer::default()
};
let input = Proto::SigningInput {
Expand Down Expand Up @@ -549,6 +555,7 @@ fn test_solana_sign_create_and_transfer_token() {
// 0.0029
amount: 2900,
decimals: 6,
is_token_2022: false,
..Proto::CreateAndTransferToken::default()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can ignore is_token_2022 field here as ..Proto::CreateAndTransferToken::default() leaves it default (false) value. Same in other places

};
let input = Proto::SigningInput {
Expand Down Expand Up @@ -578,6 +585,7 @@ fn test_solana_sign_create_and_transfer_token_2() {
// 0.004
amount: 4000,
decimals: 6,
is_token_2022: false,
..Proto::CreateAndTransferToken::default()
};
let input = Proto::SigningInput {
Expand Down Expand Up @@ -611,6 +619,7 @@ fn test_solana_sign_create_and_transfer_token_with_memo_and_references() {
"CuieVDEDtLo7FypA9SbLM9saXFdb1dsshEkyErMqkRQq".into(),
"tFpP7tZUt6zb7YZPpQ11kXNmsc5YzpMXmahGMvCHhqS".into(),
],
is_token_2022: false
};
let input = Proto::SigningInput {
private_key: b58("66ApBuKpo2uSzpjGBraHq7HP8UZMUJzp3um8FdEjkC9c"),
Expand Down Expand Up @@ -638,6 +647,7 @@ fn test_solana_sign_create_and_transfer_token_with_durable_nonce() {
// 0.004
amount: 4000,
decimals: 6,
is_token_2022: false,
..Proto::CreateAndTransferToken::default()
};
let input = Proto::SigningInput {
Expand Down Expand Up @@ -918,3 +928,30 @@ fn test_solana_sign_raw_message_v0() {
// Successfully broadcasted: https://explorer.solana.com/tx/4ffBzXxLPYEEdCYpQGETkCTCCsH6iTdmKzwUZXZZgFemdhRpxQwboguFFoKCeGF3SsZPzuwwE7LbRwLgJbsyRqyP?cluster=testnet
assert_eq!(output.encoded, "6NijVxwQoDjqt6A41HXCK9kXwNDp48uLgvRyE8uz6NY5dEzaEDLzjzuMnc5TGatHZZUXehKrzUGzbg9jPSdn6pVsMc9TXNH6JGe5RJLmHwWey3MC1p8Hs2zhjw5P439P57NToatraDX9ZwvBtK4EzZzRjWbyGdicheTPjeYKCzvPCLxDkTFtPCM9VZGGXSN2Bne92NLDvf6ntNm5pxsPkZGxPe4w9Eq26gkE83hZyrYXKaiDh8TbqbHatSkw");
}

#[test]
fn test_solana_sign_create_and_transfer_token_2022() {
let create_transfer_token: Proto::CreateAndTransferToken = Proto::CreateAndTransferToken {
recipient_main_address: "EbHdsfVpWzeQV4TceYQ2xENS8meBHyztyTKVSFtgHPUw".into(),
token_mint_address: "BSQCmMAFB9itonyVSLsUxX92Ne1rgBZFqothBk3q91k6".into(),
recipient_token_address: "FzsLNpzsLMBbm1LWpM6P3W4tKrCkd8KqnMmADNvArW5d".into(),
sender_token_address: "EQxRyhzjyhRX4TJXt7FmQ3HfFdRcu49krjxHMszidQYS".into(),
amount: 1000000000,
decimals: 9,
is_token_2022: true,
..Proto::CreateAndTransferToken::default()
};
let input = Proto::SigningInput {
private_key: b58("MCyXa2gTJELxTPemyVi5ydDcQ3vVgFyddQYXj6UM3tw"),
recent_blockhash: "9S1R94w4KzF15EYP5iP7gTigvyPd4JRfEkcUdhsirxgA".into(),
transaction_type: TransactionType::create_and_transfer_token_transaction(
create_transfer_token,
),
..Proto::SigningInput::default()
};
let mut signer = AnySignerHelper::<Proto::SigningOutput>::default();
let output = signer.sign(CoinType::Solana, input);
assert_eq!(output.error, SigningError::OK);
assert_eq!(output.encoded, "2hBQNSnumbABqKWgpeAFnCEYqb27FMMiZBn4BHrfv2CeYvzyhhrWhPm2uVHRjiJ4CwUFrFZgoTBKDhHqqDYkFVCnU82DAvsEjAQCcKGET16CGr14wNbmhL34KZv9qwvpiyRRBwDN8DJy9kLcRdYXKr8Vx5Emm5iaUu33BMbqLpcETN7HB4wFWrxrDyYgP6jbaC8v3g1wyp3xp7Q3LJ9NXki4ATmjV2T1MrpSzTjJAgQvhsx7mb2BARU7Bdrp92qgNrNWkpwr6vru2CF5UVJRySjYHaJo8YdKKMwFR8ZuwUfhbk9FFdSVGhxqX8pfj6NmUU55KMrxTtupjgifKuFZng7avzsRmysHZuBNeywrip7Gw66MaBsbsYons5jFarqUpboXyHiF4R1RQX4eQYqfmDPksv7Dh68M2wCcmiUcu3AdMMs4VHwWmkks3WfAGKKL1cN54f7SN5yP1FoGAMRbtLSCS5GCFt2pnQfkMmwqrNBtDT2pYB4f3iuGfXdHJ7APcoFtyBfUfwShUnYPq5KX3bgwAB73C38R1Hrh9e7mJJCG8i5d1CtLH12N4");
// https://explorer.solana.com/tx/Lg1xWzsC9GatQMu1ZXv23t7snC92RRvbKJe22bsS76GUb8C8a9q3HPkiUnFoK6AWKSoNSsmko1EBnvKkCnL8b7w?cluster=devnet
}
9 changes: 9 additions & 0 deletions src/proto/Solana.proto
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ message CreateTokenAccount {

// Token address
string token_address = 3;

// optional is token 2022
bool is_token_2022 = 4;
}

// Transfer tokens
Expand All @@ -103,6 +106,9 @@ message TokenTransfer {

// optional referenced public keys
repeated string references = 7;

// optional is token 2022
bool is_token_2022 = 8;
}

// CreateTokenAccount and TokenTransfer combined
Expand Down Expand Up @@ -130,6 +136,9 @@ message CreateAndTransferToken {

// optional referenced public keys
repeated string references = 8;

// optional is token 2022
bool is_token_2022 = 9;
}

message CreateNonceAccount {
Expand Down
Loading