Skip to content

Commit

Permalink
fix callchain
Browse files Browse the repository at this point in the history
  • Loading branch information
Lawliet-Chan committed Jul 21, 2024
1 parent 710073e commit 65597b8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
53 changes: 49 additions & 4 deletions example/client/asset/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@ package asset

import (
"encoding/json"
"github.com/HyperService-Consortium/go-hexutil"
"github.com/sirupsen/logrus"
. "github.com/yu-org/yu/common"
"github.com/yu-org/yu/core"
"github.com/yu-org/yu/core/context"
. "github.com/yu-org/yu/core/keypair"
. "github.com/yu-org/yu/example/client/callchain"
)

func QueryAccount(pubkey PubKey) {
params := map[string]string{"account": pubkey.Address().String()}
paramsByt, err := json.Marshal(params)
if err != nil {
panic(err)
}
rdCall := &RdCall{
TripodName: "asset",
FuncName: "QueryBalance",
Params: string(paramsByt),
}
resp, err := CallChainByReading(rdCall)
if err != nil {
panic(err)
}
resp := CallChainByReading(rdCall, map[string]string{"account": pubkey.Address().String()})
respMap := make(context.H)
err := json.Unmarshal(resp, &respMap)
err = json.Unmarshal(resp, &respMap)
if err != nil {
panic("json decode qryAccount response error: " + err.Error())
}
Expand Down Expand Up @@ -45,7 +56,24 @@ func CreateAccount(privkey PrivKey, pubkey PubKey, amount uint64) {
Params: string(paramsByt),
LeiPrice: 0,
}
CallChainByWritingWithSig(privkey, pubkey, wrCall)
byt, err := json.Marshal(wrCall)
if err != nil {
panic(err)
}
msgHash := BytesToHash(byt)
sig, err := privkey.SignData(msgHash.Bytes())
if err != nil {
panic(err)
}
postBody := &core.WritingPostBody{
Pubkey: pubkey.String(),
Signature: hexutil.Encode(sig),
Call: wrCall,
}
err = CallChainByWriting(postBody)
if err != nil {
panic(err)
}
}

type TransferInfo struct {
Expand All @@ -68,5 +96,22 @@ func TransferBalance(privkey PrivKey, pubkey PubKey, to Address, amount, leiPric
Params: string(paramsByt),
LeiPrice: leiPrice,
}
CallChainByWritingWithSig(privkey, pubkey, wrCall)
byt, err := json.Marshal(wrCall)
if err != nil {
panic(err)
}
msgHash := BytesToHash(byt)
sig, err := privkey.SignData(msgHash.Bytes())
if err != nil {
panic(err)
}
postBody := &core.WritingPostBody{
Pubkey: pubkey.String(),
Signature: hexutil.Encode(sig),
Call: wrCall,
}
err = CallChainByWriting(postBody)
if err != nil {
panic(err)
}
}
9 changes: 3 additions & 6 deletions example/client/callchain/callchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ func CallChainByReading(rdCall *RdCall) ([]byte, error) {
return io.ReadAll(resp.Body)
}

func CallChainByWriting(wrCall *WrCall) error {
func CallChainByWriting(postWriting *WritingPostBody) error {
u := url.URL{Scheme: "http", Host: "localhost:7999", Path: WrApiPath}
postBody := WritingPostBody{
Call: wrCall,
}
bodyByt, err := json.Marshal(postBody)
bodyByt, err := json.Marshal(postWriting)
if err != nil {
return err
}
Expand All @@ -56,7 +53,7 @@ func CallChainByWriting(wrCall *WrCall) error {
return err
}

func CallChainByWritingWithSig(privKey *ecdsa.PrivateKey, wrCall *WrCall) error {
func CallChainByWritingWithECDSA(privKey *ecdsa.PrivateKey, wrCall *WrCall) error {
msgByt, err := json.Marshal(wrCall)
if err != nil {
return err
Expand Down

0 comments on commit 65597b8

Please sign in to comment.