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)
| 63 | // a ComposedMsg struct and a slice of return identities. This includes verifying the |
| 64 | // message signature. |
| 65 | func 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. |