|
| 1 | +package address_lookup_table |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/ed25519" |
| 5 | + |
| 6 | + "github.com/code-payments/code-server/pkg/solana" |
| 7 | + "github.com/code-payments/code-server/pkg/solana/binary" |
| 8 | + "github.com/code-payments/code-server/pkg/solana/system" |
| 9 | +) |
| 10 | + |
| 11 | +// Reference: https://github.com/solana-program/address-lookup-table/blob/main/program/src/instruction.rs |
| 12 | + |
| 13 | +// AddressLookupTab1e1111111111111111111111111 |
| 14 | +var ProgramKey = ed25519.PublicKey{2, 119, 166, 175, 151, 51, 155, 122, 200, 141, 24, 146, 201, 4, 70, 245, 0, 2, 48, 146, 102, 246, 46, 83, 193, 24, 36, 73, 130, 0, 0, 0} |
| 15 | + |
| 16 | +const ( |
| 17 | + commandCreateLookupTable uint32 = iota |
| 18 | + commandFreezeLookupTable |
| 19 | + commandExtendLookupTable |
| 20 | + commandDeactivateLookupTable |
| 21 | + commandCloseLookupTable |
| 22 | +) |
| 23 | + |
| 24 | +func Create(alt, authority, payer ed25519.PublicKey, recentSlot uint64, bumpSeed uint8) solana.Instruction { |
| 25 | + data := make([]byte, 4+8+1) |
| 26 | + |
| 27 | + var offset int |
| 28 | + binary.PutUint32(data[offset:], commandCreateLookupTable, &offset) |
| 29 | + binary.PutUint64(data[offset:], recentSlot, &offset) |
| 30 | + binary.PutUint8(data[offset:], bumpSeed, &offset) |
| 31 | + |
| 32 | + return solana.NewInstruction( |
| 33 | + ProgramKey[:], |
| 34 | + data, |
| 35 | + solana.NewAccountMeta(alt, false), |
| 36 | + solana.NewReadonlyAccountMeta(authority, true), |
| 37 | + solana.NewAccountMeta(payer, true), |
| 38 | + solana.NewReadonlyAccountMeta(system.ProgramKey[:], false), |
| 39 | + ) |
| 40 | +} |
| 41 | + |
| 42 | +func Extend(alt, authority, payer ed25519.PublicKey, addresses ...ed25519.PublicKey) solana.Instruction { |
| 43 | + data := make([]byte, 4+8+len(addresses)*ed25519.PublicKeySize) |
| 44 | + |
| 45 | + var offset int |
| 46 | + binary.PutUint32(data[offset:], commandExtendLookupTable, &offset) |
| 47 | + binary.PutUint64(data[offset:], uint64(len(addresses)), &offset) |
| 48 | + for _, address := range addresses { |
| 49 | + binary.PutKey32(data[offset:], address, &offset) |
| 50 | + } |
| 51 | + |
| 52 | + return solana.NewInstruction( |
| 53 | + ProgramKey[:], |
| 54 | + data, |
| 55 | + solana.NewAccountMeta(alt, false), |
| 56 | + solana.NewReadonlyAccountMeta(authority, true), |
| 57 | + solana.NewAccountMeta(payer, true), |
| 58 | + solana.NewReadonlyAccountMeta(system.ProgramKey[:], false), |
| 59 | + ) |
| 60 | +} |
0 commit comments