|
| 1 | +package rpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gagliardetto/solana-go" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// Layout references for the test data below: |
| 12 | +// |
| 13 | +// SPL Token (solana-program/token): |
| 14 | +// Mint::LEN = 82 |
| 15 | +// Account::LEN = 165 |
| 16 | +// |
| 17 | +// Token-2022 (solana-program/token-2022): |
| 18 | +// Extended records place a 1-byte AccountType discriminator at offset |
| 19 | +// Account::LEN (= 165). Mint base (82 bytes) is padded with 83 zeros so |
| 20 | +// Mint and Account share the discriminator offset. |
| 21 | +// |
| 22 | +// AccountType::Uninitialized = 0 |
| 23 | +// AccountType::Mint = 1 |
| 24 | +// AccountType::Account = 2 |
| 25 | +// |
| 26 | +// Extensions follow as TLV: [u16 LE type][u16 LE length][value...]. |
| 27 | +const ( |
| 28 | + testAccountTypeUninitialized uint8 = 0 |
| 29 | + testAccountTypeMint uint8 = 1 |
| 30 | + testAccountTypeAccount uint8 = 2 |
| 31 | + |
| 32 | + // Real Token-2022 extension type numbers used in the realistic TLV |
| 33 | + // fixtures below. |
| 34 | + testExtTypeMintCloseAuthority uint16 = 3 |
| 35 | + testExtTypeImmutableOwner uint16 = 7 |
| 36 | +) |
| 37 | + |
| 38 | +func mkAccount(owner solana.PublicKey, data []byte) *Account { |
| 39 | + return &Account{ |
| 40 | + Owner: owner, |
| 41 | + Data: DataBytesOrJSONFromBytes(data), |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// mkToken2022ExtensionData builds a synthetic Token-2022 extended account |
| 46 | +// record: base (padded to 165 bytes) + 1-byte AccountType discriminator + |
| 47 | +// one TLV entry. |
| 48 | +func mkToken2022ExtensionData(accountType uint8, extType uint16, extValue []byte) []byte { |
| 49 | + data := make([]byte, 0, token2022AccountBaseSize+1+4+len(extValue)) |
| 50 | + data = append(data, bytes.Repeat([]byte{0}, token2022AccountBaseSize)...) |
| 51 | + data = append(data, accountType) |
| 52 | + data = append(data, |
| 53 | + byte(extType), byte(extType>>8), |
| 54 | + byte(len(extValue)), byte(len(extValue)>>8), |
| 55 | + ) |
| 56 | + data = append(data, extValue...) |
| 57 | + return data |
| 58 | +} |
| 59 | + |
| 60 | +func TestIsTokenMint(t *testing.T) { |
| 61 | + // A 32-byte pubkey payload used as an extension value. |
| 62 | + pubkeyValue := bytes.Repeat([]byte{0xAB}, 32) |
| 63 | + |
| 64 | + tests := []struct { |
| 65 | + name string |
| 66 | + acc *Account |
| 67 | + want bool |
| 68 | + }{ |
| 69 | + // --- nil / empty handling --- |
| 70 | + { |
| 71 | + name: "nil account", |
| 72 | + acc: nil, |
| 73 | + want: false, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "nil Data field", |
| 77 | + acc: &Account{Owner: solana.TokenProgramID}, |
| 78 | + want: false, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "empty data, Token owner", |
| 82 | + acc: mkAccount(solana.TokenProgramID, nil), |
| 83 | + want: false, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "empty data, Token-2022 owner", |
| 87 | + acc: mkAccount(solana.Token2022ProgramID, nil), |
| 88 | + want: false, |
| 89 | + }, |
| 90 | + |
| 91 | + // --- wrong owner --- |
| 92 | + { |
| 93 | + name: "SystemProgram owner, mint-sized data", |
| 94 | + acc: mkAccount(solana.SystemProgramID, make([]byte, 82)), |
| 95 | + want: false, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "random owner, mint-sized data", |
| 99 | + acc: mkAccount(solana.MustPublicKeyFromBase58("11111111111111111111111111111112"), make([]byte, 82)), |
| 100 | + want: false, |
| 101 | + }, |
| 102 | + |
| 103 | + // --- SPL Token --- |
| 104 | + { |
| 105 | + name: "SPL Token: exact Mint::LEN = 82", |
| 106 | + acc: mkAccount(solana.TokenProgramID, make([]byte, 82)), |
| 107 | + want: true, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "SPL Token: 81 bytes (one short of Mint::LEN)", |
| 111 | + acc: mkAccount(solana.TokenProgramID, make([]byte, 81)), |
| 112 | + want: false, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "SPL Token: 83 bytes (one past Mint::LEN)", |
| 116 | + acc: mkAccount(solana.TokenProgramID, make([]byte, 83)), |
| 117 | + want: false, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "SPL Token: Account::LEN = 165 (a token account, not a mint)", |
| 121 | + acc: mkAccount(solana.TokenProgramID, make([]byte, 165)), |
| 122 | + want: false, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "SPL Token: extension-shaped data is not valid for classic Token", |
| 126 | + acc: mkAccount(solana.TokenProgramID, |
| 127 | + mkToken2022ExtensionData(testAccountTypeMint, testExtTypeMintCloseAuthority, pubkeyValue)), |
| 128 | + want: false, |
| 129 | + }, |
| 130 | + |
| 131 | + // --- Token-2022 bare (no extensions) --- |
| 132 | + { |
| 133 | + name: "Token-2022: bare Mint (82 bytes)", |
| 134 | + acc: mkAccount(solana.Token2022ProgramID, make([]byte, 82)), |
| 135 | + want: true, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "Token-2022: bare Account (165 bytes, no discriminator)", |
| 139 | + acc: mkAccount(solana.Token2022ProgramID, make([]byte, 165)), |
| 140 | + want: false, |
| 141 | + }, |
| 142 | + |
| 143 | + // --- Token-2022 invalid intermediate sizes --- |
| 144 | + { |
| 145 | + name: "Token-2022: 83 bytes (between Mint and Account sizes)", |
| 146 | + acc: mkAccount(solana.Token2022ProgramID, make([]byte, 83)), |
| 147 | + want: false, |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "Token-2022: 164 bytes (one short of Account::LEN)", |
| 151 | + acc: mkAccount(solana.Token2022ProgramID, make([]byte, 164)), |
| 152 | + want: false, |
| 153 | + }, |
| 154 | + |
| 155 | + // --- Token-2022 extended record discriminator cases --- |
| 156 | + { |
| 157 | + name: "Token-2022: extended with AccountType=Mint (1) at offset 165", |
| 158 | + acc: mkAccount(solana.Token2022ProgramID, |
| 159 | + mkToken2022ExtensionData(testAccountTypeMint, testExtTypeMintCloseAuthority, pubkeyValue)), |
| 160 | + want: true, |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "Token-2022: extended with AccountType=Account (2) at offset 165", |
| 164 | + acc: mkAccount(solana.Token2022ProgramID, |
| 165 | + mkToken2022ExtensionData(testAccountTypeAccount, testExtTypeImmutableOwner, nil)), |
| 166 | + want: false, |
| 167 | + }, |
| 168 | + { |
| 169 | + name: "Token-2022: extended with AccountType=Uninitialized (0) at offset 165", |
| 170 | + acc: mkAccount(solana.Token2022ProgramID, |
| 171 | + mkToken2022ExtensionData(testAccountTypeUninitialized, 0, nil)), |
| 172 | + want: false, |
| 173 | + }, |
| 174 | + { |
| 175 | + name: "Token-2022: extended with unknown discriminator (3)", |
| 176 | + acc: mkAccount(solana.Token2022ProgramID, |
| 177 | + mkToken2022ExtensionData(3, 0, nil)), |
| 178 | + want: false, |
| 179 | + }, |
| 180 | + { |
| 181 | + name: "Token-2022: 166 bytes, discriminator byte = Mint, no TLV payload", |
| 182 | + acc: mkAccount(solana.Token2022ProgramID, |
| 183 | + append(make([]byte, 165), testAccountTypeMint)), |
| 184 | + want: true, |
| 185 | + }, |
| 186 | + } |
| 187 | + |
| 188 | + for _, tt := range tests { |
| 189 | + t.Run(tt.name, func(t *testing.T) { |
| 190 | + t.Parallel() |
| 191 | + require.Equal(t, tt.want, IsTokenMint(tt.acc)) |
| 192 | + }) |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +// TestIsTokenMint_RealisticExtendedMintSize anchors the Token-2022 mint- |
| 197 | +// with-extensions layout at the same byte length the Token-2022 sub- |
| 198 | +// package already exercises in extension_test.go (a MintCloseAuthority |
| 199 | +// extension produces a 202-byte record). |
| 200 | +func TestIsTokenMint_RealisticExtendedMintSize(t *testing.T) { |
| 201 | + data := mkToken2022ExtensionData( |
| 202 | + testAccountTypeMint, |
| 203 | + testExtTypeMintCloseAuthority, |
| 204 | + bytes.Repeat([]byte{1}, 32), |
| 205 | + ) |
| 206 | + require.Equal(t, 202, len(data), "MintCloseAuthority TLV should produce a 202-byte record") |
| 207 | + require.True(t, IsTokenMint(mkAccount(solana.Token2022ProgramID, data))) |
| 208 | +} |
0 commit comments