|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/ed25519" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + aptoslib "github.com/aptos-labs/aptos-go-sdk" |
| 10 | + "github.com/aptos-labs/aptos-go-sdk/crypto" |
| 11 | + "github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain" |
| 12 | +) |
| 13 | + |
| 14 | +// AccountGenerator is an interface for generating Aptos accounts. |
| 15 | +type AccountGenerator interface { |
| 16 | + Generate() (*aptoslib.Account, error) |
| 17 | +} |
| 18 | + |
| 19 | +var ( |
| 20 | + _ AccountGenerator = (*accountGenCTFDefault)(nil) |
| 21 | + _ AccountGenerator = (*accountGenNewSingleSender)(nil) |
| 22 | + _ AccountGenerator = (*accountGenPrivateKey)(nil) |
| 23 | +) |
| 24 | + |
| 25 | +// accountGenCTFDefault is a default account generator for CTF (Chainlink Testing Framework). |
| 26 | +type accountGenCTFDefault struct { |
| 27 | + // The account address string to use for generating the account. |
| 28 | + accountStr string |
| 29 | + // privateKeyStr is the private key string to use for generating the account. |
| 30 | + privateKeyStr string |
| 31 | +} |
| 32 | + |
| 33 | +// AccountGenCTFDefault creates a new instance of accountGenCTFDefault. It uses the default |
| 34 | +// Aptos account and private key from the blockchain package. |
| 35 | +func AccountGenCTFDefault() *accountGenCTFDefault { |
| 36 | + return &accountGenCTFDefault{ |
| 37 | + accountStr: blockchain.DefaultAptosAccount, |
| 38 | + privateKeyStr: blockchain.DefaultAptosPrivateKey, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Generate generates an Aptos account using the default address and private key from the |
| 43 | +// blockchain package. It returns an error if the address or private key is invalid. |
| 44 | +func (g *accountGenCTFDefault) Generate() (*aptoslib.Account, error) { |
| 45 | + var address aptoslib.AccountAddress |
| 46 | + |
| 47 | + if err := address.ParseStringRelaxed(g.accountStr); err != nil { |
| 48 | + return nil, fmt.Errorf("failed to parse account address %s: %w", g.accountStr, err) |
| 49 | + } |
| 50 | + |
| 51 | + privateKeyBytes, err := hex.DecodeString(strings.TrimPrefix(g.privateKeyStr, "0x")) |
| 52 | + if err != nil { |
| 53 | + return nil, fmt.Errorf("failed to decode private key: %w", err) |
| 54 | + } |
| 55 | + privateKey := ed25519.NewKeyFromSeed(privateKeyBytes) |
| 56 | + |
| 57 | + return aptoslib.NewAccountFromSigner(&crypto.Ed25519PrivateKey{Inner: privateKey}, address) |
| 58 | +} |
| 59 | + |
| 60 | +// accountGenNewSingleSender is an account generator that creates a new single sender account. |
| 61 | +type accountGenNewSingleSender struct{} |
| 62 | + |
| 63 | +// AccountGenNewSingleSender creates a new instance of accountGenNewSingleSender. |
| 64 | +func AccountGenNewSingleSender() *accountGenNewSingleSender { |
| 65 | + return &accountGenNewSingleSender{} |
| 66 | +} |
| 67 | + |
| 68 | +// Generate generates a new Aptos account using the aptos library's single sender account creation |
| 69 | +// method. |
| 70 | +func (g *accountGenNewSingleSender) Generate() (*aptoslib.Account, error) { |
| 71 | + return aptoslib.NewEd25519SingleSenderAccount() |
| 72 | +} |
| 73 | + |
| 74 | +// accountGenPrivateKey is an account generator that creates an account from the private key. |
| 75 | +type accountGenPrivateKey struct { |
| 76 | + // PrivateKey is the hex formatted private key used to generate the Aptos account. |
| 77 | + PrivateKey string |
| 78 | +} |
| 79 | + |
| 80 | +// AccountGenPrivateKey creates a new instance of accountGenPrivateKey with the provided private key. |
| 81 | +func AccountGenPrivateKey(privateKey string) *accountGenPrivateKey { |
| 82 | + return &accountGenPrivateKey{ |
| 83 | + PrivateKey: privateKey, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Generate generates an Aptos account from the provided private key. It returns an error if the |
| 88 | +// private key string cannot be parsed. |
| 89 | +func (g *accountGenPrivateKey) Generate() (*aptoslib.Account, error) { |
| 90 | + privateKey := &crypto.Ed25519PrivateKey{} |
| 91 | + if err := privateKey.FromHex(g.PrivateKey); err != nil { |
| 92 | + return nil, fmt.Errorf("failed to parse private key: %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + return aptoslib.NewAccountFromSigner(privateKey) |
| 96 | +} |
0 commit comments