|
| 1 | +package blockchain |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/block-vision/sui-go-sdk/models" |
| 8 | + "github.com/docker/docker/api/types/container" |
| 9 | + "github.com/go-resty/resty/v2" |
| 10 | + "github.com/smartcontractkit/chainlink-testing-framework/framework" |
| 11 | + "github.com/testcontainers/testcontainers-go" |
| 12 | + "github.com/testcontainers/testcontainers-go/wait" |
| 13 | + "path/filepath" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + DefaultFaucetPort = "9123/tcp" |
| 20 | + DefaultFaucetPortNum = "9123" |
| 21 | + DefaultSuiNodePort = "9000" |
| 22 | +) |
| 23 | + |
| 24 | +// SuiWalletInfo info about Sui account/wallet |
| 25 | +type SuiWalletInfo struct { |
| 26 | + Alias *string `json:"alias"` // Alias key name, usually "null" |
| 27 | + Flag int `json:"flag"` // Flag is an integer |
| 28 | + KeyScheme string `json:"keyScheme"` // Key scheme is a string |
| 29 | + Mnemonic string `json:"mnemonic"` // Mnemonic is a string |
| 30 | + PeerId string `json:"peerId"` // Peer ID is a string |
| 31 | + PublicBase64Key string `json:"publicBase64Key"` // Public key in Base64 format |
| 32 | + SuiAddress string `json:"suiAddress"` // Sui address is a 0x prefixed hex string |
| 33 | +} |
| 34 | + |
| 35 | +// funds provided key using local faucet |
| 36 | +// we can't use the best client available - block-vision/sui-go-sdk for that, since some versions have old API and it is hardcoded |
| 37 | +// https://github.com/block-vision/sui-go-sdk/blob/main/sui/faucet_api.go#L16 |
| 38 | +func fundAccount(url string, address string) error { |
| 39 | + r := resty.New().SetBaseURL(url) |
| 40 | + b := &models.FaucetRequest{ |
| 41 | + FixedAmountRequest: &models.FaucetFixedAmountRequest{ |
| 42 | + Recipient: address, |
| 43 | + }, |
| 44 | + } |
| 45 | + resp, err := r.R().SetBody(b).SetHeader("Content-Type", "application/json").Post("/gas") |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + framework.L.Info().Any("Resp", resp).Msg("Address is funded!") |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +// generateKeyData generates a wallet and returns all the data |
| 54 | +func generateKeyData(containerName string, keyCipherType string) (*SuiWalletInfo, error) { |
| 55 | + cmdStr := []string{"sui", "keytool", "generate", keyCipherType, "--json"} |
| 56 | + keyOut, err := framework.ExecContainer(containerName, cmdStr) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + // formatted JSON with, no plain --json version, remove special symbols |
| 61 | + cleanKey := strings.ReplaceAll(keyOut, "\x00", "") |
| 62 | + cleanKey = strings.ReplaceAll(cleanKey, "\x01", "") |
| 63 | + cleanKey = strings.ReplaceAll(cleanKey, "\x02", "") |
| 64 | + cleanKey = strings.ReplaceAll(cleanKey, "\n", "") |
| 65 | + cleanKey = "{" + cleanKey[2:] |
| 66 | + var key *SuiWalletInfo |
| 67 | + if err := json.Unmarshal([]byte(cleanKey), &key); err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + framework.L.Info().Interface("Key", key).Msg("Test key") |
| 71 | + return key, nil |
| 72 | +} |
| 73 | + |
| 74 | +func defaultSui(in *Input) { |
| 75 | + if in.Image == "" { |
| 76 | + in.Image = "mysten/sui-tools:devnet" |
| 77 | + } |
| 78 | + if in.Port != "" { |
| 79 | + framework.L.Warn().Msgf("'port' field is set but only default port can be used: %s", DefaultSuiNodePort) |
| 80 | + } |
| 81 | + in.Port = DefaultSuiNodePort |
| 82 | +} |
| 83 | + |
| 84 | +func newSui(in *Input) (*Output, error) { |
| 85 | + defaultSui(in) |
| 86 | + ctx := context.Background() |
| 87 | + containerName := framework.DefaultTCName("blockchain-node") |
| 88 | + |
| 89 | + absPath, err := filepath.Abs(in.ContractsDir) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + bindPort := fmt.Sprintf("%s/tcp", in.Port) |
| 95 | + |
| 96 | + req := testcontainers.ContainerRequest{ |
| 97 | + Image: in.Image, |
| 98 | + ExposedPorts: []string{in.Port, DefaultFaucetPort}, |
| 99 | + Name: containerName, |
| 100 | + Labels: framework.DefaultTCLabels(), |
| 101 | + Networks: []string{framework.DefaultNetworkName}, |
| 102 | + NetworkAliases: map[string][]string{ |
| 103 | + framework.DefaultNetworkName: {containerName}, |
| 104 | + }, |
| 105 | + HostConfigModifier: func(h *container.HostConfig) { |
| 106 | + h.PortBindings = framework.MapTheSamePort(bindPort, DefaultFaucetPort) |
| 107 | + }, |
| 108 | + ImagePlatform: "linux/amd64", |
| 109 | + Env: map[string]string{ |
| 110 | + "RUST_LOG": "off,sui_node=info", |
| 111 | + }, |
| 112 | + Cmd: []string{ |
| 113 | + "sui", |
| 114 | + "start", |
| 115 | + "--force-regenesis", |
| 116 | + "--with-faucet", |
| 117 | + }, |
| 118 | + Files: []testcontainers.ContainerFile{ |
| 119 | + { |
| 120 | + HostFilePath: absPath, |
| 121 | + ContainerFilePath: "/", |
| 122 | + }, |
| 123 | + }, |
| 124 | + // we need faucet for funding |
| 125 | + WaitingFor: wait.ForListeningPort(DefaultFaucetPort).WithStartupTimeout(10 * time.Second).WithPollInterval(200 * time.Millisecond), |
| 126 | + } |
| 127 | + |
| 128 | + c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 129 | + ContainerRequest: req, |
| 130 | + Started: true, |
| 131 | + }) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + host, err := c.Host(ctx) |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + suiAccount, err := generateKeyData(containerName, "ed25519") |
| 140 | + if err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + if err := fundAccount(fmt.Sprintf("http://%s:%s", "127.0.0.1", DefaultFaucetPortNum), suiAccount.SuiAddress); err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + return &Output{ |
| 147 | + UseCache: true, |
| 148 | + Family: "sui", |
| 149 | + ContainerName: containerName, |
| 150 | + NetworkSpecificData: &NetworkSpecificData{SuiAccount: suiAccount}, |
| 151 | + Nodes: []*Node{ |
| 152 | + { |
| 153 | + HostHTTPUrl: fmt.Sprintf("http://%s:%s", host, in.Port), |
| 154 | + DockerInternalHTTPUrl: fmt.Sprintf("http://%s:%s", containerName, in.Port), |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, nil |
| 158 | +} |
0 commit comments