retriever

package
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2025 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GATEWAY_GENTOKEN_URL      = "/gateway/gentoken"
	GATEWAY_UPLOADFILE_URL    = "/gateway/upload/file"
	GATEWAY_BATCHUPLOAD_URL   = "/gateway/upload/batch/file"
	GATEWAY_BATCHREQUEST_URL  = "/gateway/upload/batch/request"
	GATEWAY_GETFILE_URL       = "/gateway/download"
	RETRIEVER_QUERYDATA_URL   = "/querydata"
	RETRIEVER_FETCHDATA_URL   = "/cache-fetch"
	RETRIEVER_NODESTATUS_URL  = "/status"
	RETRIEVER_GETCAPACITY_URL = "/capacity"

	DEFAULT_PART_SIZE = 32 * 1024 * 1024
)

Variables

This section is empty.

Functions

func AuthorizeGateways added in v0.2.5

func AuthorizeGateways(url, rpc, mnemonic string) error

AuthorizeGateways authorizes the gateway pointed to by the given URL and all its peer gateways within the same cluster, enabling them to help the user manage territory and data.

Parameters:

  • url: Domain name of the gateway (or gateway cluster) to be authorized (e.g., "http://gateway.example.com")
  • rpc: The CESS blockchain RPC endpoint URL for blockchain interactions
  • mnemonic: The mnemonic phrase for the account that will authorize the gateways

Returns:

  • error: Returns nil if authorization is successful, otherwise returns an error describing what went wrong Possible error scenarios:
  • CDN node availability check fails
  • Blockchain client initialization fails
  • Querying OSS entries from blockchain fails
  • Authorization transaction fails for matching OSS entries

func CreateStorageOrder

func CreateStorageOrder(cli *chain.Client, info FileInfo, caller *signature.KeyringPair, event any) (string, error)

CreateStorageOrder creates a storage order on the blockchain for a file. Parameters:

cli - The blockchain client instance for submitting the order.
info - Struct containing file details (fragments, owner, name, territory, size).
caller - The keyring pair for signing the transaction.
event - Event handler for processing transaction events.

Returns:

string - The transaction block hash.
error - An error if the order creation fails, including encoding errors or transaction submission failures.

func DecryptKey added in v0.2.1

func DecryptKey(mnemonic string, capsule []byte) ([]byte, error)

DecryptKey retrieves the original symmetric encryption key using the data owner's mnemonic. This operates on the original (non-re-encrypted) capsule structure.

Parameters:

mnemonic - Data owner's mnemonic phrase for secret derivation
capsule  - Original encrypted capsule bytes (JSON marshaled)

Returns:

[]byte - Decrypted symmetric key bytes
error  - Possible errors include:
           - Invalid capsule format
           - Invalid mnemonic phrase
           - Key decryption operation failure

func DecryptReKey added in v0.2.1

func DecryptReKey(mnemonic string, pkX, newCapsule []byte) ([]byte, error)

DecryptReKey decrypts re-encrypted key material using recipient's secret derived from mnemonic. This enables the target party to access the original encrypted data through proxy re-encryption.

Parameters:

mnemonic   - Recipient's mnemonic phrase for secret derivation
pkX        - Public key X bytes used in re-encryption (32-byte expected)
newCapsule - Re-encrypted capsule bytes (JSON marshaled)

Returns:

[]byte - Decrypted symmetric key bytes
error  - Possible errors include:
           - Invalid public key X format
           - Public key deserialization failure
           - Invalid capsule format
           - Invalid mnemonic phrase
           - Key decryption operation failure

func DownloadData

func DownloadData(baseUrl, fid, segment, fpath string, capsule, rk, pkX []byte) error

DownloadData retrieves data from the gateway and writes it to local file system. Handles HTTP headers for decryption when re-encryption key (rk) and public key (pkX) are provided.

Parameters:

baseUrl  - Base URL of the gateway service
fid      - File id
segment  - Segment id
fpath    - Local path to save downloaded file
capsule  - Proxy re-encryption capsule
rk       - Re-encryption key bytes, generated by the re-encrypt method
pkX      - Public key bytes for encryption, generated by the re-encrypt method

Returns:

error - Possible errors include:
          - URL construction failure
          - HTTP request failure
          - File creation/write failure

func GenGatewayAccessToken

func GenGatewayAccessToken(baseUrl, message, account string, sign []byte, exp time.Duration) (string, error)

GenGatewayAccessToken generates an access token for the gateway using the provided base URL, message, account, and signature. It sends a POST request to the "/gateway/gentoken" endpoint with the necessary parameters. Message must be the current Unix timestamp (seconds) and expire after one minute to prevent the signature from being reused maliciously. Parameters:

baseUrl - The base URL of the gateway.
message - The message to be signed.
account - The account associated with the token.
sign - The signature of the message.

Returns:

string - The generated access token.
error - An error if the request fails or the response is invalid.

func GenReEncryptionKey

func GenReEncryptionKey(mnemonic string, pkB []byte) ([]byte, []byte, error)

GenReEncryptionKey generates a re-encryption key and corresponding public key using Schnorrkel scheme. This implements proxy re-encryption mechanism for decentralized storage systems.

Parameters:

mnemonic - User's mnemonic phrase for key derivation
pkB      - Recipient's public key bytes (32-byte expected)

Returns:

[]byte - Marshaled re-encryption key (rk)
[]byte - Encoded public key bytes for encryption (pkX)
error  - Possible errors include:
           - Invalid mnemonic phrase
           - Public key deserialization failure
           - Re-encryption key generation failure
           - Key serialization failure

func GetPreCapsuleAndGatewayPubkey

func GetPreCapsuleAndGatewayPubkey(baseUrl, fid string) ([]byte, []byte, error)

GetPreCapsuleAndGatewayPubkey retrieves pre-encapsulation capsule data and gateway public key for a given file by sending HTTP GET request to the gateway service. This is essential for subsequent encryption operations.

Parameters:

baseUrl - Base URL of the gateway service
fid     - Unique file identifier

Returns:

[]byte - Proxy re-encryption capsule
[]byte - Gateway's public key
error  - Possible errors include:
           - URL construction failure
           - HTTP request failure
           - JSON unmarshalling failure

func ProxyReEncrypt added in v0.2.1

func ProxyReEncrypt(baseUrl, token, did string, capsule, rk []byte) ([]byte, error)

ProxyReEncrypt performs proxy re-encryption by sending a request to the gateway server. This handles remote cryptographic transformation of capsules for decentralized storage access.

Parameters:

baseUrl - Gateway server base URL (e.g., "https://api.example.com")
token   - Authentication token for gateway API access
did     - Decentralized identifier for target data
capsule - Original encrypted capsule bytes (JSON marshaled)
rk      - Re-encryption key bytes (ristretto255 scalar serialization)

Returns:

[]byte - Re-encrypted capsule bytes (JSON marshaled)
error  - Possible errors include:
           - Invalid URL path construction
           - Request payload serialization failure
           - Gateway communication failure
           - Response payload deserialization failure

func QueryDealMap

func QueryDealMap(cli *chain.Client, fid string) (map[int]struct{}, error)

QueryDealMap queries the completed segment indices of a file's storage deals from the blockchain. Parameters:

cli - The blockchain client instance for interacting with the chain.
fid - The unique file identifier string.

Returns:

map[int]struct{} - A set of completed segment indices.
error - An error if the query fails, including underlying blockchain query errors.

func ReEncryptKey added in v0.2.1

func ReEncryptKey(capsule, rkb []byte) ([]byte, error)

ReEncryptKey performs proxy re-encryption on a capsule using a re-encryption key. This transforms the original capsule into a new version decryptable by the target party.

Parameters:

capsule - Original encrypted capsule bytes (JSON marshaled)
rkb     - Re-encryption key bytes (ristretto255 scalar serialization)

Returns:

[]byte - New re-encrypted capsule bytes (JSON marshaled)
error  - Possible errors include:
           - Invalid capsule format
           - Invalid re-encryption key format
           - Re-encryption operation failure
           - Result serialization failure

func RequestBatchUpload added in v0.2.1

func RequestBatchUpload(baseUrl, token, territory, filename string, fileSize int64, encrypt, asyncUpload, noTxProxy bool) (string, error)

RequestBatchUpload initiates a batch file upload session. It sends a POST request to the batch upload endpoint with file metadata. Parameters:

baseUrl - The base URL of the gateway.
token - The access token for authentication.
territory - The territory where the file will be uploaded.
filename - The name of the file to be uploaded.
fileSize - The total size of the file in bytes.
encrypt - Whether the data needs to be encrypted.
asyncUpload - Whether to upload asynchronously.
noTxProxy - Whether to bypass transaction proxy.

Returns:

string - A unique hash identifier for the upload session.
error - An error if the session initialization fails.

func SendHttpRequest

func SendHttpRequest(method, url string, headers map[string]string, dataReader *bytes.Buffer) ([]byte, error)

func SignedSR25519WithMnemonic

func SignedSR25519WithMnemonic(mnemonic string, msg []byte) ([]byte, error)

SignedSR25519WithMnemonic signs a message using the SR25519 scheme with a given mnemonic. It appends "<Bytes>" and "</Bytes>" to the message before signing. Parameters:

mnemonic - The mnemonic phrase used for signing.
msg - The message to be signed.

Returns:

[]byte - The signed message.
error - An error if the mnemonic is invalid or signing fails.

func UploadFile

func UploadFile(baseUrl, token, territory, filename string, file io.Reader, encrypt bool) (string, error)

UploadFile uploads a file to the specified territory. It sends a POST request to the "/gateway/upload/file" endpoint with the necessary parameters. Parameters:

	baseUrl - The base URL of the gateway.
	token - The access token for authentication.
	territory - The territory to which the file will be uploaded.
	filename - The name of the file to be uploaded.
	file - The file content to be uploaded.
    encrypt - Whether the data needs to be encrypted (using proxy re-encryption technology)

Returns:

string - The file identifier (FID) if the upload is successful.
error - An error if the upload fails.

Types

type BatchFilesInfo added in v0.2.1

type BatchFilesInfo struct {
	Hash         string    `json:"hash,omitempty"`
	FileName     string    `json:"file_name,omitempty"`
	Owner        []byte    `json:"owner,omitempty"`
	Territory    string    `json:"territory,omitempty"`
	FilePath     string    `json:"-"`
	UploadedSize int64     `json:"uploaded_size,omitempty"`
	TotalSize    int64     `json:"total_size,omitempty"`
	AsyncUpload  bool      `json:"async_upload,omitempty"`
	NoTxProxy    bool      `json:"no_tx_proxy,omitempty"`
	Encrypt      bool      `json:"encrypt,omitempty"`
	UpdateDate   time.Time `json:"update_date,omitempty"`
}

type BatchUploadResp added in v0.2.1

type BatchUploadResp struct {
	Fid      string   `json:"fid"`
	ChunkEnd int64    `json:"chunk_end"`
	FileInfo FileInfo `json:"file_info"`
}

func BatchUploadFile added in v0.2.1

func BatchUploadFile(baseUrl, token, hash string, reader io.ReaderAt, start, end int64) (BatchUploadResp, error)

BatchUploadFile uploads a specific byte range of a file as part of a batch upload session. It sends a multipart POST request with the specified byte range to the batch upload endpoint. Parameters:

baseUrl - The base URL of the gateway.
token - The access token for authentication.
hash - The unique hash identifier obtained from RequestBatchUpload.
reader - The io.ReaderAt to read the file content from.
start - The starting byte position of the range (inclusive).
end - The ending byte position of the range (exclusive).

Returns:

BatchUploadResp - The upload result or confirmation.
error - An error if the chunk upload fails.

type FileInfo

type FileInfo struct {
	Fid       string     `json:"fid"`
	FileName  string     `json:"file_name"`
	BaseDir   string     `json:"base_dir"`
	FileSize  int64      `json:"file_size"`
	Owner     []byte     `json:"owner"`
	Territory string     `json:"territory"`
	Segments  []string   `json:"segments"`
	Fragments [][]string `json:"fragments"`
}

func AsyncUploadFile

func AsyncUploadFile(baseUrl, token, territory, filename string, file io.Reader, noProxy, encrypt bool) (FileInfo, error)

AsyncUploadFile uploads a file asynchronously to the specified territory. It sends a POST request to the "/gateway/upload/file" endpoint with the necessary parameters. Parameters:

	baseUrl - The base URL of the gateway.
	token - The access token for authentication.
	territory - The territory to which the file will be uploaded.
	filename - The name of the file to be uploaded.
	file - The file content to be uploaded.
	noProxy - Whether to not create file orders through OSS proxy.
    encrypt - Whether the data needs to be encrypted (using proxy re-encryption technology)

Returns:

FileInfo - Information about the uploaded file.
error - An error if the upload fails.

type ReencryptReq added in v0.2.1

type ReencryptReq struct {
	Did     string `json:"did"`
	Capsule []byte `json:"capsule"`
	Rk      []byte `json:"rk"`
}

type Response

type Response struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data any    `json:"data"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL