Submit payment

post
Body
all ofOptional

Represents token transfer transaction requests to/from REST.

Responses
200Success
application/json
post
POST /v1/transactions/payment HTTP/1.1
Host: api.testnet.1money.network
Content-Type: application/json
Accept: */*
Content-Length: 386

{
  "chain_id": 1,
  "nonce": 1,
  "recent_checkpoint": 100,
  "recent_epoch": 1,
  "recipient": "0x9E1E9688A44D058fF181Ed64ddFAFbBE5CC742Ab",
  "token": "0x9E1E9688A44D058fF181Ed64ddFAFbBE5CC742Ab",
  "value": "1500000000",
  "signature": {
    "r": "72956732934625920503481762689501378577921804342307439094906376029324416116949",
    "s": "29902520081700531224291681396692026253288382272435451874524203378285409371412",
    "v": "1"
  }
}
{
  "hash": "0xf55f9525be94633b56f954d3252d52b8ef42f5fd5f9491b243708471c15cc40c"
}
import (
	"fmt"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/ethereum/go-ethereum/rlp"
	"math/big"
)

var privateKey = "01833a126ec45d0191519748146b9e35647aab7fed28de1c8e17824970f964a3"

type PaymentPayload struct {
	RecentEpoch      uint64         `json:"recent_epoch"`
	RecentCheckpoint uint64         `json:"recent_checkpoint"`
	ChainID          uint64         `json:"chain_id"`
	Nonce            uint64         `json:"nonce"`
	Recipient        common.Address `json:"recipient"`
	Value            *big.Int       `json:"value"`
	Token            common.Address `json:"token"`
}

type PaymentRequest struct {
	PaymentPayload
	Signature Signature `json:"signature"`
}

type Signature struct {
	R string
	S string
	V uint64
}

func SignMessage(msg *PaymentMessage) (*Signature, error) {
	encoded, err := rlp.EncodeToBytes(msg)
	if err != nil {
		return nil, err
	}
	hash := crypto.Keccak256(encoded)
	fmt.Printf("Signature Hash: %s\n", common.BytesToHash(hash))
	key, err := crypto.HexToECDSA(privateKey)
	if err != nil {
		return nil, err
	}
	sign, err := crypto.Sign(hash, key)
	if err != nil {
		return nil, err
	}
	fmt.Printf("Signature: 0x%x\n", sign)
	fmt.Printf("Raw bytes: %v\n", sign)
	r := sign[:32]
	s := sign[32:64]
	v := sign[64]
	return &Signature{
		R: common.BytesToHash(r).Hex(),
		S: common.BytesToHash(s).Hex(),
		V: uint64(v),
	}, nil
}

func main() {
	tokenAddr := common.HexToAddress("0x2045a425D0e131E747f8be2F044413733e412d7d")
	payment := &PaymentMessage{
		RecentEpoch:      	0,
		RecentCheckpoint: 	100,
		ChainID: 		big.NewInt(1212101),
		Nonce:   		0,
		To:      		common.HexToAddress("0x937b9aff6404141681cbf39301aeb869500bbdf0"),
		Value:   		big.NewInt(1),
		Token:   		&tokenAddr,
	}
	signature, err := SignMessage(payment)
	if err != nil {
		panic(fmt.Sprintf("sign payment msg error: %v", err))
	}
	fmt.Printf("Payment msg Signature: %v\n", signature)
}

Last updated