|
| 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 | +type SuiKeyInfo struct { |
| 19 | + Alias *string `json:"alias"` // Alias key name, usually "null" |
| 20 | + Flag int `json:"flag"` // Flag is an integer |
| 21 | + KeyScheme string `json:"keyScheme"` // Key scheme is a string |
| 22 | + Mnemonic string `json:"mnemonic"` // Mnemonic is a string |
| 23 | + PeerId string `json:"peerId"` // Peer ID is a string |
| 24 | + PublicBase64Key string `json:"publicBase64Key"` // Public key in Base64 format |
| 25 | + SuiAddress string `json:"suiAddress"` // Sui address is a 0x prefixed hex string |
| 26 | +} |
| 27 | + |
| 28 | +func fundAccount(url string, address string) error { |
| 29 | + r := resty.New().SetBaseURL(url).EnableTrace().SetDebug(true) |
| 30 | + b := &models.FaucetRequest{ |
| 31 | + FixedAmountRequest: &models.FaucetFixedAmountRequest{ |
| 32 | + Recipient: address, |
| 33 | + }, |
| 34 | + } |
| 35 | + resp, err := r.R().SetBody(b).SetHeader("Content-Type", "application/json").Post("/gas") |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + framework.L.Info().Any("Resp", resp).Msg("Address is funded!") |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +func generateKeyData(containerName string, keyCipherType string) (*SuiKeyInfo, error) { |
| 44 | + cmdStr := []string{"sui", "keytool", "generate", keyCipherType, "--json"} |
| 45 | + keyOut, err := framework.ExecContainer(containerName, cmdStr) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + cleanKey := strings.ReplaceAll(keyOut, "\x00", "") |
| 50 | + cleanKey = strings.ReplaceAll(cleanKey, "\x01", "") |
| 51 | + cleanKey = strings.ReplaceAll(cleanKey, "\x02", "") |
| 52 | + cleanKey = strings.ReplaceAll(cleanKey, "\n", "") |
| 53 | + var key *SuiKeyInfo |
| 54 | + if err := json.Unmarshal([]byte("{"+cleanKey[2:]), &key); err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + framework.L.Info().Interface("Key", key).Msg("Test key") |
| 58 | + return key, nil |
| 59 | +} |
| 60 | + |
| 61 | +func defaultSui(in *Input) { |
| 62 | + if in.Image == "" { |
| 63 | + in.Image = "mysten/sui-tools:devnet" |
| 64 | + } |
| 65 | + if in.Port != "" { |
| 66 | + framework.L.Warn().Msg("'port' field is set but only default port can be used: 9000") |
| 67 | + } |
| 68 | + in.Port = "9000" |
| 69 | +} |
| 70 | + |
| 71 | +func newSui(in *Input) (*Output, error) { |
| 72 | + defaultSui(in) |
| 73 | + ctx := context.Background() |
| 74 | + containerName := framework.DefaultTCName("blockchain-node") |
| 75 | + |
| 76 | + absPath, err := filepath.Abs(in.ContractsDir) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + bindPort := fmt.Sprintf("%s/tcp", in.Port) |
| 82 | + |
| 83 | + req := testcontainers.ContainerRequest{ |
| 84 | + Image: in.Image, |
| 85 | + ExposedPorts: []string{in.Port, "9123/tcp"}, |
| 86 | + Name: containerName, |
| 87 | + Labels: framework.DefaultTCLabels(), |
| 88 | + Networks: []string{framework.DefaultNetworkName}, |
| 89 | + NetworkAliases: map[string][]string{ |
| 90 | + framework.DefaultNetworkName: {containerName}, |
| 91 | + }, |
| 92 | + HostConfigModifier: func(h *container.HostConfig) { |
| 93 | + h.PortBindings = framework.MapTheSamePort(bindPort, "9123/tcp") |
| 94 | + }, |
| 95 | + ImagePlatform: "linux/amd64", |
| 96 | + Env: map[string]string{ |
| 97 | + "RUST_LOG": "off,sui_node=info", |
| 98 | + }, |
| 99 | + Cmd: []string{ |
| 100 | + "sui", |
| 101 | + "start", |
| 102 | + "--force-regenesis", |
| 103 | + "--with-faucet", |
| 104 | + }, |
| 105 | + Files: []testcontainers.ContainerFile{ |
| 106 | + { |
| 107 | + HostFilePath: absPath, |
| 108 | + ContainerFilePath: "/", |
| 109 | + }, |
| 110 | + }, |
| 111 | + // we need faucet for funding |
| 112 | + WaitingFor: wait.ForListeningPort("9123/tcp").WithStartupTimeout(10 * time.Second).WithPollInterval(200 * time.Millisecond), |
| 113 | + } |
| 114 | + |
| 115 | + c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 116 | + ContainerRequest: req, |
| 117 | + Started: true, |
| 118 | + }) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + host, err := c.Host(ctx) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + keyData, err := generateKeyData(containerName, "ed25519") |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + err = fundAccount(fmt.Sprintf("http://%s:%s", "127.0.0.1", "9123"), keyData.SuiAddress) |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + return &Output{ |
| 135 | + UseCache: true, |
| 136 | + Family: "sui", |
| 137 | + ContainerName: containerName, |
| 138 | + GeneratedData: &GeneratedData{Mnemonic: keyData.Mnemonic}, |
| 139 | + Nodes: []*Node{ |
| 140 | + { |
| 141 | + HostHTTPUrl: fmt.Sprintf("http://%s:%s", host, in.Port), |
| 142 | + DockerInternalHTTPUrl: fmt.Sprintf("http://%s:%s", containerName, in.Port), |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, nil |
| 146 | +} |
0 commit comments