MCPcopy
hub / github.com/gopherdata/gophernotes / WireMsgToComposedMsg

Function WireMsgToComposedMsg

messages.go:65–93  ·  view source on GitHub ↗

WireMsgToComposedMsg translates a multipart ZMQ messages received from a socket into a ComposedMsg struct and a slice of return identities. This includes verifying the message signature.

(msgparts [][]byte, signkey []byte)

Source from the content-addressed store, hash-verified

63// a ComposedMsg struct and a slice of return identities. This includes verifying the
64// message signature.
65func WireMsgToComposedMsg(msgparts [][]byte, signkey []byte) (ComposedMsg, [][]byte, error) {
66
67 i := 0
68 for string(msgparts[i]) != "<IDS|MSG>" {
69 i++
70 }
71 identities := msgparts[:i]
72
73 // Validate signature.
74 var msg ComposedMsg
75 if len(signkey) != 0 {
76 mac := hmac.New(sha256.New, signkey)
77 for _, msgpart := range msgparts[i+2 : i+6] {
78 mac.Write(msgpart)
79 }
80 signature := make([]byte, hex.DecodedLen(len(msgparts[i+1])))
81 hex.Decode(signature, msgparts[i+1])
82 if !hmac.Equal(mac.Sum(nil), signature) {
83 return msg, nil, &InvalidSignatureError{}
84 }
85 }
86
87 // Unmarshal contents.
88 json.Unmarshal(msgparts[i+2], &msg.Header)
89 json.Unmarshal(msgparts[i+3], &msg.ParentHeader)
90 json.Unmarshal(msgparts[i+4], &msg.Metadata)
91 json.Unmarshal(msgparts[i+5], &msg.Content)
92 return msg, identities, nil
93}
94
95// ToWireMsg translates a ComposedMsg into a multipart ZMQ message ready to send, and
96// signs it. This does not add the return identities or the delimiter.

Callers 3

runKernelFunction · 0.85
recvShellReplyMethod · 0.85
recvIOSubMethod · 0.85

Calls 1

WriteMethod · 0.80

Tested by 2

recvShellReplyMethod · 0.68
recvIOSubMethod · 0.68