filecoin

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: MIT Imports: 14 Imported by: 0

README

filecoin-client

goproxy.cn

需要自行部署Lotus Node节点:https://lotu.sh/en+getting-started

此库仅添加部分方法,但已经满足钱包充值提现逻辑了,如果需要其他方法,请Fork后自行添加。

充值流程:获取头部高度,从本地高度遍历到头部高度,再根据高度获取区块CID,根据区块CID获取区块的所有消息,判断消息的类型是否0(0为发送Fil),和接收地址是否是本地的地址。

说明请查询client_test文件。

安装

go get github.com/shandiekeji/filecoin-client

使用

package main

import (
	"context"
	"github.com/filecoin-project/go-address"
	"github.com/shandiekeji/filecoin-client"
)

func main() {
	client := filecoin.NewClient("http://127.0.0.1:1234/rpc/v0", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBbGxvdyI6WyJyZWFkIiwid3JpdGUiLCJzaWduIiwiYWRtaW4iXX0.cF__3r_0IR9KwZ2nLkqcBW8vuPePruZieJAVvTAoUA4")

	addr, _ := address.NewFromString("t1e3soclcq34tq7wmykp7xkkmpkzjnghumm3syyay")
	bal, err := client.WalletBalance(context.Background(), addr)
	if err != nil {
		panic(err)
	}

	println(bal.String())
}

离线签名版

公共节点申请:https://infura.io

具体实现逻辑在官方库中:https://github.com/filecoin-project/go-filecoin

package main

import (
	"context"
	"encoding/hex"
	"github.com/filecoin-project/go-address"
	"github.com/filecoin-project/go-state-types/abi"
	"github.com/shandiekeji/filecoin-client"
	"github.com/shandiekeji/filecoin-client/local"
	"github.com/shandiekeji/filecoin-client/types"
	"github.com/shopspring/decimal"
)

func main() {
	// 设置网络类型
	address.CurrentNetwork = address.Mainnet

	// 生产新的地址
	// 新地址有转入fil才激活,不然没法用
	ki, addr, err := local.WalletNew(types.KTSecp256k1)
	if err != nil {
		panic(err)
	}

	println(hex.EncodeToString(ki.PrivateKey))
	println(addr.String())

	// 50a5e6234f5fdfc026bd889347409e11b6ef5b6034a7b0572d7e24ed1e9ba0e4
	// f1dynqskhlixt5eswpff3a72ksprqmeompv3pbesy

	to, _ := address.NewFromString("f1yfi4yslez2hz3ori5grvv3xdo3xkibc4v6xjusy")

	// 转移0.001FIL到f1yfi4yslez2hz3ori5grvv3xdo3xkibc4v6xjusy
	msg := &types.Message{
		Version:    0,
		To:         to,
		From:       *addr,
		Nonce:      0,
		Value:      filecoin.FromFil(decimal.NewFromFloat(0.001)),
		GasLimit:   0,
		GasFeeCap:  abi.NewTokenAmount(10000),
		GasPremium: abi.NewTokenAmount(10000),
		Method:     0,
		Params:     nil,
	}

	client := filecoin.New("https://1lB5G4SmGdSTikOo7l6vYlsktdd:b58884915362a99b4fc18c2bf8af8358@filecoin.infura.io")

	// 最大手续费0.0001 FIL
	//maxFee := filecoin.FromFil(decimal.NewFromFloat(0.0001))

	// 估算GasLimit
	//msg, err = client.GasEstimateMessageGas(context.Background(), msg, &types.MessageSendSpec{MaxFee: maxFee}, nil)
	//if err != nil {
	//	panic(err)
	//}

	// 离线签名
	s, err := local.WalletSignMessage(types.KTSecp256k1, ki.PrivateKey, msg)
	if err != nil {
		panic(err)
	}

	println(hex.EncodeToString(s.Signature.Data))
	// 47bcbb167fd9040bd02dba02789bc7bc0463c290db1be9b07065c12a64fb84dc546bef7aedfba789d0d7ce2c4532f8fa0d2dd998985ad3ec1a8b064c26e4625a01

	// 验证签名
	if err := local.WalletVerifyMessage(s); err != nil {
		panic(err)
	}

	mid, err := client.MpoolPush(context.Background(), s)
	if err != nil {
		panic(err)
	}

	println(mid.String())
}

暂时不支持bls类型,仅支持secp256k1,所以离线签名所有类型请选择types.KTSecp256k1

私钥存储在数据库请进行加密存储,建议进行AES加密,将AES密钥放在代码中,别放配置文件,编译到执行文件中。

Util

util.go 中提供FIL小数与大数之间的转换,但没有严格测试,请小心使用。

充值流程

参见:/examples/recharge

Lotus文档

https://lotu.sh/en+api-methods

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromFil

func FromFil(v decimal.Decimal) abi.TokenAmount

将小数的Fil转换为大数

func ToFil

func ToFil(v abi.TokenAmount) decimal.Decimal

将大数的Fil转换为小数

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func New

func New(addr string) *Client

func NewClient

func NewClient(addr string, token string) *Client

func (*Client) AuthNew

func (c *Client) AuthNew(ctx context.Context, perms []string) ([]byte, error)

AuthNew

func (*Client) AuthVerify

func (c *Client) AuthVerify(ctx context.Context, token string) ([]string, error)

AuthVerify

func (*Client) BeaconGetEntry

func (c *Client) BeaconGetEntry(ctx context.Context, epoch int64) (*types.BeaconEntry, error)

BeaconGetEntry returns the beacon entry for the given filecoin epoch. If the entry has not yet been produced, the call will block until the entry becomes available

func (*Client) ChainExport

func (c *Client) ChainExport(ctx context.Context, tsk types.TipSetKey) ([]byte, error)

ChainExport returns a stream of bytes with CAR dump of chain data.

func (*Client) ChainGetBlock

func (c *Client) ChainGetBlock(ctx context.Context, id cid.Cid) (*types.BlockHeader, error)

ChainGetBlock returns the block specified by the given CID.

func (*Client) ChainGetBlockMessages

func (c *Client) ChainGetBlockMessages(ctx context.Context, id cid.Cid) (*types.BlockMessages, error)

ChainGetBlockMessages returns messages stored in the specified block.

func (*Client) ChainGetGenesis

func (c *Client) ChainGetGenesis(ctx context.Context) (*types.TipSet, error)

ChainGetGenesis returns the genesis tipset.

func (*Client) ChainGetMessage

func (c *Client) ChainGetMessage(ctx context.Context, id cid.Cid) (*types.Message, error)

ChainGetMessage reads a message referenced by the specified CID from the chain blockstore.

func (*Client) ChainGetNode

func (c *Client) ChainGetNode(ctx context.Context, p string) (*types.IpldObject, error)

ChainGetNode

func (*Client) ChainGetParentMessages

func (c *Client) ChainGetParentMessages(ctx context.Context, id cid.Cid) ([]types.ParentMessage, error)

ChainGetParentMessages returns messages stored in parent tipset of the specified block.

func (*Client) ChainGetParentReceipts

func (c *Client) ChainGetParentReceipts(ctx context.Context, id cid.Cid) ([]*types.MessageReceipt, error)

ChainGetParentReceipts returns receipts for messages in parent tipset of the specified block.

func (*Client) ChainGetPath

func (c *Client) ChainGetPath(ctx context.Context, from types.TipSetKey, to types.TipSetKey) (*types.HeadChange, error)

ChainGetPath returns a set of revert/apply operations needed to get from one tipset to another

func (*Client) ChainGetRandomnessFromBeacon

func (c *Client) ChainGetRandomnessFromBeacon(ctx context.Context, tsk types.TipSetKey, personalization int64, randEpoch int64, entropy []byte) ([]byte, error)

ChainGetRandomnessFromBeacon is used to sample the beacon for randomness.

func (*Client) ChainGetRandomnessFromTickets

func (c *Client) ChainGetRandomnessFromTickets(ctx context.Context, tsk types.TipSetKey, personalization int64, randEpoch int64, entropy []byte) ([]byte, error)

ChainGetRandomnessFromTickets is used to sample the chain for randomness.

func (*Client) ChainGetTipSet

func (c *Client) ChainGetTipSet(ctx context.Context, tsk types.TipSetKey) (*types.TipSet, error)

ChainGetTipSet returns the tipset specified by the given TipSetKey.

func (*Client) ChainGetTipSetByHeight

func (c *Client) ChainGetTipSetByHeight(ctx context.Context, height int64, tsk types.TipSetKey) (*types.TipSet, error)

ChainGetTipSetByHeight looks back for a tipset at the specified epoch. If there are no blocks at the specified epoch, a tipset at an earlier epoch will be returned.

func (*Client) ChainHasObj

func (c *Client) ChainHasObj(ctx context.Context, o cid.Cid) (bool, error)

ChainHasObj checks if a given CID exists in the chain blockstore.

func (*Client) ChainHead

func (c *Client) ChainHead(ctx context.Context) (*types.TipSet, error)

ChainHead returns the current head of the chain.

func (*Client) ChainNotify

func (c *Client) ChainNotify()

ChainNotify returns channel with chain head updates. First message is guaranteed to be of len == 1, and type == 'current'.

func (*Client) ChainReadObj

func (c *Client) ChainReadObj(ctx context.Context, obj cid.Cid) ([]byte, error)

ChainReadObj reads ipld nodes referenced by the specified CID from chain blockstore and returns raw bytes.

func (*Client) ChainSetHead

func (c *Client) ChainSetHead(ctx context.Context, tsk types.TipSetKey) error

ChainSetHead forcefully sets current chain head. Use with caution.

func (*Client) ChainStatObj

func (c *Client) ChainStatObj(ctx context.Context, obj, base cid.Cid) (types.ObjStat, error)

ChainStatObj returns statistics about the graph referenced by 'obj'. If 'base' is also specified, then the returned stat will be a diff between the two objects.

func (*Client) ChainTipSetWeight

func (c *Client) ChainTipSetWeight(ctx context.Context, tsk types.TipSetKey) (decimal.Decimal, error)

ChainTipSetWeight computes weight for the specified tipset.

func (*Client) FilecoinMethod

func (c *Client) FilecoinMethod(method string) string

Namespace Filecoin

func (*Client) GasEstimateGasLimit

func (c *Client) GasEstimateGasLimit(ctx context.Context, message *types.Message, cids []*cid.Cid) (int64, error)

GasEstimateGasLimit estimates gas used by the message and returns it. It fails if message fails to execute.

func (*Client) GasEstimateMessageGas

func (c *Client) GasEstimateMessageGas(ctx context.Context, message *types.Message, spec *types.MessageSendSpec, cids []*cid.Cid) (*types.Message, error)

GasEstimateMessageGas estimates gas values for unset message gas fields

func (*Client) MpoolGetNonce

func (c *Client) MpoolGetNonce(ctx context.Context, address address.Address) (nonce uint64, err error)

MpoolGetNonce 获取指定发送账号的下一个nonce值

func (*Client) MpoolPush

func (c *Client) MpoolPush(ctx context.Context, sm *types.SignedMessage) (cid.Cid, error)

MpoolPush pushes a signed message to mempool.

func (*Client) Request

func (c *Client) Request(ctx context.Context, method string, result interface{}, params ...interface{}) error

Request call RPC method

func (*Client) SetToken

func (c *Client) SetToken(token string) *Client

SetToken set Authorization token

func (*Client) StateGetActor

func (c *Client) StateGetActor(ctx context.Context, addr address.Address, cids []*cid.Cid) (*types.Actor, error)

StateGetActor returns the indicated actor's nonce and balance.

func (*Client) StateGetReceipt

func (c *Client) StateGetReceipt(ctx context.Context, id cid.Cid, cids []*cid.Cid) (*types.MessageReceipt, error)

StateGetReceipt returns the message receipt for the given message

func (*Client) StateReplay

func (c *Client) StateReplay(ctx context.Context, tsk types.TipSetKey, mc cid.Cid) (*types.InvocResult, error)

StateReplay returns the result of executing the indicated message, assuming it was executed in the indicated tipset.

func (*Client) StateSearchMsg

func (c *Client) StateSearchMsg(ctx context.Context, msg cid.Cid) (*types.MsgLookup, error)

StateSearchMsg searches for a message in the chain, and returns its receipt and the tipset where it was executed

func (*Client) Version

func (c *Client) Version(ctx context.Context) (*types.Version, error)

Version Get Lotus Node version

func (*Client) WalletBalance

func (c *Client) WalletBalance(ctx context.Context, addr address.Address) (abi.TokenAmount, error)

WalletBalance returns the balance of the given address at the current head of the chain.

func (*Client) WalletDefaultAddress

func (c *Client) WalletDefaultAddress(ctx context.Context) (address.Address, error)

WalletDefaultAddress returns the address marked as default in the wallet.

func (*Client) WalletDelete

func (c *Client) WalletDelete(ctx context.Context, addr address.Address) error

WalletDelete deletes an address from the wallet.

func (*Client) WalletExport

func (c *Client) WalletExport(ctx context.Context, addr address.Address) (*types.KeyInfo, error)

WalletExport returns the private key of an address in the wallet.

func (*Client) WalletHas

func (c *Client) WalletHas(ctx context.Context, addr address.Address) (bool, error)

WalletHas indicates whether the given address is in the wallet.

func (*Client) WalletImport

func (c *Client) WalletImport(ctx context.Context, ki *types.KeyInfo) (address.Address, error)

WalletImport receives a KeyInfo, which includes a private key, and imports it into the wallet.

func (*Client) WalletList

func (c *Client) WalletList(ctx context.Context) ([]address.Address, error)

WalletList lists all the addresses in the wallet.

func (*Client) WalletNew

func (c *Client) WalletNew(ctx context.Context, typ types.KeyType) (address.Address, error)

WalletNew creates a new address in the wallet with the given KeyType.

func (*Client) WalletSetDefault

func (c *Client) WalletSetDefault(ctx context.Context, addr address.Address) error

WalletSetDefault marks the given address as as the default one.

func (*Client) WalletSign

func (c *Client) WalletSign(ctx context.Context, addr address.Address, data []byte) (*crypto.Signature, error)

WalletSign signs the given bytes using the given address.

func (*Client) WalletSignMessage

func (c *Client) WalletSignMessage(ctx context.Context, addr address.Address, message *types.Message) (*types.SignedMessage, error)

WalletSignMessage signs the given message using the given address.

func (*Client) WalletVerify

func (c *Client) WalletVerify(ctx context.Context, k string, msg []byte, sig *crypto.Signature) (bool, error)

WalletVerify takes an address, a signature, and some bytes, and indicates whether the signature is valid. The address does not have to be in the wallet.

Directories

Path Synopsis
examples
pkg
btcec
Package btcec implements support for the elliptic curves needed for bitcoin.
Package btcec implements support for the elliptic curves needed for bitcoin.
bls

Jump to

Keyboard shortcuts

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