MCPcopy
hub / github.com/canopy-network/canopy / buildSignAndSendTx

Function buildSignAndSendTx

plugin/go/tutorial/rpc_test.go:373–521  ·  view source on GitHub ↗

buildSignAndSendTx builds a transaction, signs it with BLS, and sends it via raw JSON

(rpcURL string, signerKey *keyGroup, msgType string, msgJSON map[string]interface{}, fee, networkID, chainID, height uint64)

Source from the content-addressed store, hash-verified

371
372// buildSignAndSendTx builds a transaction, signs it with BLS, and sends it via raw JSON
373func buildSignAndSendTx(rpcURL string, signerKey *keyGroup, msgType string, msgJSON map[string]interface{}, fee, networkID, chainID, height uint64) (string, error) {
374 // Build the transaction structure for signing (without signature)
375 txTime := uint64(time.Now().UnixMicro())
376
377 // For signing, we need to construct the Any message bytes
378 // The server uses the registered plugin schema to convert JSON to protobuf
379 // But for signing, we need to compute what the server will compute
380
381 // Create sign bytes by first figuring out what the Any will look like
382 // The plugin registry maps message type to type URL
383 // For plugin messages, the type URL is: type.googleapis.com/types.Message<Type>
384 var typeURL string
385 switch msgType {
386 case "send":
387 typeURL = "type.googleapis.com/types.MessageSend"
388 case "reward":
389 typeURL = "type.googleapis.com/types.MessageReward"
390 case "faucet":
391 typeURL = "type.googleapis.com/types.MessageFaucet"
392 default:
393 return "", fmt.Errorf("unknown message type: %s", msgType)
394 }
395
396 // Marshal the message to proto bytes for signing
397 // We need to create the actual proto message
398 // Addresses in msgJSON are base64-encoded (for protojson compatibility)
399 var msgProto proto.Message
400 switch msgType {
401 case "send":
402 fromAddr, _ := base64.StdEncoding.DecodeString(msgJSON["fromAddress"].(string))
403 toAddr, _ := base64.StdEncoding.DecodeString(msgJSON["toAddress"].(string))
404 msgProto = &contract.MessageSend{
405 FromAddress: fromAddr,
406 ToAddress: toAddr,
407 Amount: uint64(msgJSON["amount"].(float64)),
408 }
409 case "reward":
410 adminAddr, _ := base64.StdEncoding.DecodeString(msgJSON["adminAddress"].(string))
411 recipientAddr, _ := base64.StdEncoding.DecodeString(msgJSON["recipientAddress"].(string))
412 msgProto = &contract.MessageReward{
413 AdminAddress: adminAddr,
414 RecipientAddress: recipientAddr,
415 Amount: uint64(msgJSON["amount"].(float64)),
416 }
417 case "faucet":
418 signerAddr, _ := base64.StdEncoding.DecodeString(msgJSON["signerAddress"].(string))
419 recipientAddr, _ := base64.StdEncoding.DecodeString(msgJSON["recipientAddress"].(string))
420 msgProto = &contract.MessageFaucet{
421 SignerAddress: signerAddr,
422 RecipientAddress: recipientAddr,
423 Amount: uint64(msgJSON["amount"].(float64)),
424 }
425 }
426
427 msgBytes, err := proto.Marshal(msgProto)
428 if err != nil {
429 return "", fmt.Errorf("failed to marshal message: %v", err)
430 }

Callers 3

sendFaucetTxFunction · 0.70
sendSendTxFunction · 0.70
sendRewardTxFunction · 0.70

Calls 6

postRawJSONFunction · 0.70
ErrorfMethod · 0.65
MarshalMethod · 0.65
GetSignBytesMethod · 0.65
SignMethod · 0.65
UnmarshalMethod · 0.65

Tested by

no test coverage detected