|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1" |
| 10 | + basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1" |
| 11 | + cosmosSecp "cosmossdk.io/api/cosmos/crypto/secp256k1" |
| 12 | + signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1" |
| 13 | + txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1" |
| 14 | + "github.com/cosmos/cosmos-proto/anyutil" |
| 15 | + "github.com/davecgh/go-spew/spew" |
| 16 | + tendermintClient "github.com/tendermint/tendermint/rpc/client/http" |
| 17 | + "github.com/tkhq/go-sdk" |
| 18 | + "github.com/tkhq/go-sdk/pkg/api/client" |
| 19 | + "github.com/tkhq/go-sdk/pkg/api/client/private_keys" |
| 20 | + "github.com/tkhq/go-sdk/pkg/api/models" |
| 21 | + "github.com/tkhq/go-sdk/pkg/apikey" |
| 22 | + "github.com/tkhq/go-sdk/pkg/signer/cosmos" |
| 23 | + "google.golang.org/protobuf/proto" |
| 24 | + "google.golang.org/protobuf/types/known/anypb" |
| 25 | +) |
| 26 | + |
| 27 | +func getPublicKey(turnkeyClient *client.TurnkeyPublicAPI, orgID string, privateKeyID string, apiKey *apikey.Key) ([]byte, error) { |
| 28 | + getSenderPrivateKeyRequest := private_keys.NewPublicAPIServiceGetPrivateKeyParams().WithBody(&models.V1GetPrivateKeyRequest{ |
| 29 | + OrganizationID: &orgID, |
| 30 | + PrivateKeyID: &privateKeyID, |
| 31 | + }) |
| 32 | + |
| 33 | + getSenderPrivateKeyResponse, err := turnkeyClient.PrivateKeys.PublicAPIServiceGetPrivateKey(getSenderPrivateKeyRequest, &sdk.Authenticator{Key: apiKey}) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + |
| 38 | + publicKeyBytes, err := hex.DecodeString(*getSenderPrivateKeyResponse.Payload.PrivateKey.PublicKey) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + return cosmos.CompressPublicKey(publicKeyBytes), nil |
| 44 | +} |
| 45 | + |
| 46 | +func main() { |
| 47 | + receiverPrivateKeyID := os.Getenv("RECEIVER_PRIVATE_KEY_ID") |
| 48 | + senderPrivateKeyID := os.Getenv("SENDER_PRIVATE_KEY_ID") |
| 49 | + apiPrivateKey := os.Getenv("API_PRIVATE_KEY") |
| 50 | + apiHost := os.Getenv("TURNKEY_BASE_URL") |
| 51 | + orgID := os.Getenv("ORGANIZATION_ID") |
| 52 | + |
| 53 | + ctx := context.Background() |
| 54 | + |
| 55 | + apiKey, err := apikey.FromTurnkeyPrivateKey(apiPrivateKey) |
| 56 | + if err != nil { |
| 57 | + panic(err) |
| 58 | + } |
| 59 | + |
| 60 | + publicApiClient := client.NewHTTPClientWithConfig(nil, &client.TransportConfig{ |
| 61 | + Host: apiHost, |
| 62 | + }) |
| 63 | + |
| 64 | + signer := cosmos.NewSigner(cosmos.SignerParams{ |
| 65 | + TurnkeyClient: *publicApiClient, |
| 66 | + OrganizationID: orgID, |
| 67 | + ApiHost: apiHost, |
| 68 | + ApiKey: apiKey, |
| 69 | + }) |
| 70 | + |
| 71 | + fromPublicKey, err := getPublicKey(publicApiClient, orgID, senderPrivateKeyID, apiKey) |
| 72 | + if err != nil { |
| 73 | + panic(err) |
| 74 | + } |
| 75 | + |
| 76 | + toPublicKey, err := getPublicKey(publicApiClient, orgID, receiverPrivateKeyID, apiKey) |
| 77 | + if err != nil { |
| 78 | + panic(err) |
| 79 | + } |
| 80 | + |
| 81 | + toAddress := cosmos.CosmosAddressFromPublicKey(toPublicKey) |
| 82 | + fromAddress := cosmos.CosmosAddressFromPublicKey(fromPublicKey) |
| 83 | + |
| 84 | + fmt.Println("Sending from: ") |
| 85 | + fmt.Println(fromAddress) |
| 86 | + fmt.Println("Sending to: ") |
| 87 | + fmt.Println(toAddress) |
| 88 | + |
| 89 | + pk, err := anyutil.New(&cosmosSecp.PubKey{Key: fromPublicKey}) |
| 90 | + if err != nil { |
| 91 | + panic(err) |
| 92 | + } |
| 93 | + |
| 94 | + signerInfo := []*txv1beta1.SignerInfo{ |
| 95 | + { |
| 96 | + PublicKey: pk, |
| 97 | + ModeInfo: &txv1beta1.ModeInfo{ |
| 98 | + Sum: &txv1beta1.ModeInfo_Single_{ |
| 99 | + Single: &txv1beta1.ModeInfo_Single{ |
| 100 | + Mode: signingv1beta1.SignMode_SIGN_MODE_DIRECT, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + Sequence: 2, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + fee := &txv1beta1.Fee{ |
| 109 | + Amount: []*basev1beta1.Coin{ |
| 110 | + { |
| 111 | + Denom: "uatom", |
| 112 | + Amount: "1000", |
| 113 | + }, |
| 114 | + }, |
| 115 | + GasLimit: 200000, |
| 116 | + } |
| 117 | + |
| 118 | + msg, err := anyutil.New(&bankv1beta1.MsgSend{ |
| 119 | + FromAddress: fromAddress, |
| 120 | + ToAddress: toAddress, |
| 121 | + Amount: []*basev1beta1.Coin{ |
| 122 | + { |
| 123 | + Denom: "uatom", |
| 124 | + Amount: "1", |
| 125 | + }, |
| 126 | + }, |
| 127 | + }) |
| 128 | + if err != nil { |
| 129 | + panic(err) |
| 130 | + } |
| 131 | + |
| 132 | + txBody := &txv1beta1.TxBody{ |
| 133 | + Messages: []*anypb.Any{msg}, |
| 134 | + Memo: "Turnkey demo", |
| 135 | + } |
| 136 | + |
| 137 | + authInfo := &txv1beta1.AuthInfo{ |
| 138 | + Fee: fee, |
| 139 | + SignerInfos: signerInfo, |
| 140 | + } |
| 141 | + |
| 142 | + bodyBz, err := proto.Marshal(txBody) |
| 143 | + if err != nil { |
| 144 | + panic(err) |
| 145 | + } |
| 146 | + |
| 147 | + authInfoBz, err := proto.Marshal(authInfo) |
| 148 | + if err != nil { |
| 149 | + panic(err) |
| 150 | + } |
| 151 | + |
| 152 | + signBytes, err := proto.Marshal(&txv1beta1.SignDoc{ |
| 153 | + BodyBytes: bodyBz, |
| 154 | + AuthInfoBytes: authInfoBz, |
| 155 | + ChainId: "cosmoshub-4", |
| 156 | + AccountNumber: 1878048, |
| 157 | + }) |
| 158 | + if err != nil { |
| 159 | + panic(err) |
| 160 | + } |
| 161 | + |
| 162 | + c, err := tendermintClient.New("https://cosmos-rpc.publicnode.com:443") |
| 163 | + if err != nil { |
| 164 | + panic(err) |
| 165 | + } |
| 166 | + |
| 167 | + signature, err := signer.Sign(senderPrivateKeyID, signBytes) |
| 168 | + if err != nil { |
| 169 | + panic(err) |
| 170 | + } |
| 171 | + |
| 172 | + tx := txv1beta1.Tx{ |
| 173 | + Body: txBody, |
| 174 | + AuthInfo: authInfo, |
| 175 | + Signatures: [][]byte{ |
| 176 | + signature, |
| 177 | + }, |
| 178 | + } |
| 179 | + |
| 180 | + txBytes, err := proto.Marshal(&tx) |
| 181 | + if err != nil { |
| 182 | + panic(err) |
| 183 | + } |
| 184 | + |
| 185 | + result, err := c.BroadcastTxSync(ctx, txBytes) |
| 186 | + if err != nil { |
| 187 | + panic(err) |
| 188 | + } |
| 189 | + |
| 190 | + spew.Dump(result) |
| 191 | +} |
0 commit comments