|
1 | 1 | package utils_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/hex" |
| 5 | + "math/rand" |
| 6 | + "strings" |
4 | 7 | "testing" |
| 8 | + "time" |
5 | 9 |
|
| 10 | + "github.com/mr-tron/base58" |
6 | 11 | "github.com/stretchr/testify/require" |
7 | 12 |
|
8 | 13 | "github.com/pushchain/push-chain-node/utils" |
9 | 14 | ) |
10 | 15 |
|
11 | 16 | const ( |
12 | | - eip55Addr = "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed" |
13 | | - lowerAddr = "0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed" |
14 | | - upperAddr = "0X5AAEB6053F3E94C9B9A09F33669435E7EF1BEAED" |
15 | | - noPfxAddr = "5aaeb6053f3e94c9b9a09f33669435e7ef1beaed" |
16 | | - mixedHash = "0xB28F49668e7e76dc96D7aaBE5b7f63FEcfbd1c3574774c05e8204e749fd96fbd" |
17 | | - lowerHash = "0xb28f49668e7e76dc96d7aabe5b7f63fecfbd1c3574774c05e8204e749fd96fbd" |
18 | | - noPfxHash = "b28f49668e7e76dc96d7aabe5b7f63fecfbd1c3574774c05e8204e749fd96fbd" |
19 | | - solPubkey = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" |
20 | | - solSig = "5j7s6NiJS3JAkvgkoc18WVAsiSaci2pxB2A6ueCJP4tprA2TFg9wSyTLeYouxPBJEMzJinENTkpA52YStRW5Dia7" |
| 17 | + eip55Addr = "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed" |
| 18 | + lowerAddr = "0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed" |
| 19 | + upperAddr = "0X5AAEB6053F3E94C9B9A09F33669435E7EF1BEAED" |
| 20 | + noPfxAddr = "5aaeb6053f3e94c9b9a09f33669435e7ef1beaed" |
| 21 | + mixedHash = "0xB28F49668e7e76dc96D7aaBE5b7f63FEcfbd1c3574774c05e8204e749fd96fbd" |
| 22 | + lowerHash = "0xb28f49668e7e76dc96d7aabe5b7f63fecfbd1c3574774c05e8204e749fd96fbd" |
| 23 | + noPfxHash = "b28f49668e7e76dc96d7aabe5b7f63fecfbd1c3574774c05e8204e749fd96fbd" |
| 24 | + solPubkey = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" |
| 25 | + solSig = "5j7s6NiJS3JAkvgkoc18WVAsiSaci2pxB2A6ueCJP4tprA2TFg9wSyTLeYouxPBJEMzJinENTkpA52YStRW5Dia7" |
21 | 26 | ) |
22 | 27 |
|
23 | 28 | func TestCanonicalizeEVMAddress_EquivalentEncodingsConverge(t *testing.T) { |
@@ -134,3 +139,72 @@ func TestCAIP2Namespace(t *testing.T) { |
134 | 139 | require.Equal(t, "solana", utils.CAIP2Namespace("solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1")) |
135 | 140 | require.Equal(t, "", utils.CAIP2Namespace("no-colon")) |
136 | 141 | } |
| 142 | + |
| 143 | +// referenceSolanaTxHash reproduces the pre-fix behaviour for pure-base58 input: |
| 144 | +// decode unconditionally, convert only on an exact 64-byte result, otherwise |
| 145 | +// return the input untouched. The length band added in canonicalizeSolanaTxHash |
| 146 | +// must not change the result for any input. |
| 147 | +func referenceSolanaTxHash(s string) string { |
| 148 | + if raw, err := base58.Decode(s); err == nil && len(raw) == 64 { |
| 149 | + return "0x" + hex.EncodeToString(raw) |
| 150 | + } |
| 151 | + return s |
| 152 | +} |
| 153 | + |
| 154 | +func TestCanonicalizeTxHashByNamespace_Solana_LengthBandIsOutputEquivalent(t *testing.T) { |
| 155 | + // Only 64..88 base58 chars can decode to exactly 64 bytes, so the band gate |
| 156 | + // is a pure performance change. Sweep across it — 63/64/88/89 are the edges. |
| 157 | + rng := rand.New(rand.NewSource(1)) |
| 158 | + alphabet := []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") |
| 159 | + |
| 160 | + lengths := []int{1, 2, 31, 32, 43, 44, 63, 64, 65, 87, 88, 89, 90, 128, 200, 300} |
| 161 | + for n := 3; n < 63; n += 7 { |
| 162 | + lengths = append(lengths, n) |
| 163 | + } |
| 164 | + |
| 165 | + for _, n := range lengths { |
| 166 | + for variant := 0; variant < 4; variant++ { |
| 167 | + b := make([]byte, n) |
| 168 | + for i := range b { |
| 169 | + switch variant { |
| 170 | + case 0: |
| 171 | + b[i] = '1' // all-zero decode: the short edge of the band |
| 172 | + case 1: |
| 173 | + b[i] = 'z' // largest digit: the long edge |
| 174 | + default: |
| 175 | + b[i] = alphabet[rng.Intn(len(alphabet))] |
| 176 | + } |
| 177 | + } |
| 178 | + in := string(b) |
| 179 | + require.Equal(t, referenceSolanaTxHash(in), |
| 180 | + utils.LenientCanonicalizeTxHash("solana:devnet", in), |
| 181 | + "length band changed the result for a %d-char input %q", n, in) |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func TestCanonicalizeTxHashByNamespace_Solana_RealSignatureStillConverges(t *testing.T) { |
| 187 | + // The band must not break the case it exists to serve: an 88-char base58 |
| 188 | + // signature still folds to 0x-hex. |
| 189 | + got, err := utils.CanonicalizeTxHashByNamespace("solana:devnet", solSig) |
| 190 | + require.NoError(t, err) |
| 191 | + require.Equal(t, "0x", got[:2]) |
| 192 | + require.Len(t, got, 2+128) |
| 193 | +} |
| 194 | + |
| 195 | +func TestCanonicalizeTxHashByNamespace_Solana_OversizedInputDoesNotDecode(t *testing.T) { |
| 196 | + // F-2026-18821: mr-tron/base58 decoding is quadratic, and the result for an |
| 197 | + // out-of-band length is discarded. Before the fix a single 1e5-char decode |
| 198 | + // measured 4.5-29s (and InboundKeys does three of them); after, no decode |
| 199 | + // runs at all. The bound is loose enough not to flake on a busy CI box while |
| 200 | + // still failing hard on any return to O(n^2). |
| 201 | + huge := strings.Repeat("z", 100_000) |
| 202 | + |
| 203 | + start := time.Now() |
| 204 | + got := utils.LenientCanonicalizeTxHash("solana:devnet", huge) |
| 205 | + elapsed := time.Since(start) |
| 206 | + |
| 207 | + require.Equal(t, huge, got, "out-of-band input must pass through unchanged") |
| 208 | + require.Less(t, elapsed, time.Second, |
| 209 | + "oversized base58 tx_hash must not be decoded (took %s)", elapsed) |
| 210 | +} |
0 commit comments