본문으로 건너뛰기

Cosmos gRPC 개요

업데이트됨:
2026년 8월 7일

With a Quicknode Injective endpoint, you can interact with Injective chains using either REST or gRPC. The REST API serves JSON over HTTP, making it easy to fetch balances, transactions, and other on-chain data with standard web tools. The gRPC API uses Protocol Buffers over HTTP/2, offering faster, strongly typed requests and streaming support for real-time data. Use REST for quick, human-readable integrations, and gRPC when you need high-performance backend services at scale.

Authentication Required for Injective gRPC

To ensure secure access to Injective gRPC, users are required to authenticate themselves. This authentication process is necessary before utilizing any method. Quicknode endpoints consist of two crucial components: the endpoint 그리고 이에 상응하는 토큰. 사용자는 메서드 호출을 수행하기 전에 이 두 구성 요소를 사용하여 인증 자격 증명을 포함한 gRPC 구성해야 합니다.

Authentication for the Injective gRPC can be handled in two ways:

  1. 기본 인증
  2. x-token 인증

이 문서 전반에 걸쳐, 우리는 다음 중 하나를 getClientWithBasicAuth 또는 getClientWithXToken 이러한 다양한 인증 메커니즘을 처리하는 방법을 보여주는 함수들입니다.

기본 인증

getClientWithBasicAuth 이 함수는 자격 증명을 base64로 인코딩하는 기본 인증(Basic Authentication)을 사용하여 인증을 처리하는 방법을 보여줍니다. 다음은 해당 함수의 코드 구현입니다. getClientWithBasicAuth 기능뿐만 아니라 basicAuth RPC 자격 증명 구현:

import (
"문맥"
"crypto/tls"
"encoding/base64"
"fmt"
"google.golang.grpc"
"google.golang.grpc"
)

func getClientWithBasicAuth(endpoint, token string) (*grpc.ClientConn, error) {
target := endpoint + ".injective-mainnet.quiknode.pro:9090" // for TLS connections
conn, err := grpc.Dial(target,
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
grpc.WithPerRPCCredentials(basicAuth{
username: endpoint,
password: token,
}),
)
if err != nil {
return nil, fmt.Errorf("Unable to dial endpoint %w", err)
}
return conn, nil
}

// basicAuth implements the credentials.PerRPCCredentials interface to support basic authentication for grpc requests.
type basicAuth struct {
username string
password string
}

func (b basicAuth) GetRequestMetadata(ctx context.Context, in ...string) (map[string]string, error) {
auth := b.username + ":" + b.password
enc := base64.StdEncoding.EncodeToString([]byte(auth))
return map[string]string{"authorization": "Basic " + enc}, nil
}

func (basicAuth) RequireTransportSecurity() bool {
return false
}

getClientWithBasicAuth 이 함수는 gRPC 필요한 보안 옵션으로 구성하고, 지정된 endpoint 연결을 설정합니다. 9090. 이 함수는 endpoint 토큰을 입력 매개변수로 받아, 인증된 API 호출을 수행하는 데 사용할 수 있는 gRPC 연결을 반환합니다.

conn, err := getClientWithBasicAuth("ENDPOINT_NAME", "TOKEN")
if err != nil {
log.Fatalf("err: %v", err)
}
defer conn.Close()

x-token 인증

getClientWithXToken 이 함수는 x-token을 사용하여 인증을 처리하는 방법을 보여줍니다. 이 방법은 각 요청의 x-token 헤더에 토큰을 추가합니다.


import (
"문맥"
"crypto/tls"
"fmt"
"google.golang.grpc"
"google.golang.grpc"
)

func getClientWithXToken(endpoint, token string) (*grpc.ClientConn, error) {
target := endpoint + ".injective-mainnet.quiknode.pro:9090" // for TLS connections
conn, err := grpc.Dial(target,
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
grpc.WithPerRPCCredentials(auth{
token: token,
}),
)
if err != nil {
return nil, fmt.Errorf("Unable to dial endpoint %w", err)
}
return conn, nil
}

// auth implements the credentials.PerRPCCredentials interface to support x-token authentication for grpc requests.
type auth struct {
token string
}

func (a *auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"x-token": a.token,
}, nil
}

func (auth) RequireTransportSecurity() bool {
return false
}

이 방법은 기본 인증 예제와 유사하게 gRPC 구성하지만, x-token 헤더에 인증 토큰을 포함시킵니다. 이 함수를 사용하여 API 호출을 수행하는 방법은 다음과 같습니다:


conn, err := getClientWithXToken("ENDPOINT_NAME", "TOKEN")
if err != nil {
log.Fatalf("err: %v", err)
}
defer conn.Close()