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

Method UnmarshalJSON

lib/tx.go:231–315  ·  view source on GitHub ↗

MarshalJSON() implements the json.Unmarshaler interface for the Transaction type

(jsonBytes []byte)

Source from the content-addressed store, hash-verified

229
230// MarshalJSON() implements the json.Unmarshaler interface for the Transaction type
231func (x *Transaction) UnmarshalJSON(jsonBytes []byte) (err error) {
232 // create a new json object reference to ensure a non nil result
233 j := new(jsonTx)
234 // populate the json object with json bytes
235 if err = json.Unmarshal(jsonBytes, j); err != nil {
236 return err
237 }
238 // first try unmarshalling using the global plugin registration
239 if len(j.Msg) > 0 {
240 anyMsg, e := AnyFromJSONForMessageType(j.Type, j.Msg)
241 if e == nil {
242 *x = Transaction{
243 MessageType: j.Type,
244 Msg: anyMsg,
245 Signature: j.Signature,
246 CreatedHeight: j.CreatedHeight,
247 Time: j.Time,
248 Fee: j.Fee,
249 Memo: j.Memo,
250 NetworkId: j.NetworkId,
251 ChainId: j.ChainId,
252 }
253 return nil
254 } else if e.Code() != CodeUnknownMsgName {
255 return e
256 }
257 }
258 // get the type of the message payload based on the 'message types' that were globally registered upon app start
259 m, found := RegisteredMessages[j.Type]
260 // if the message type is not found among the registered messages
261 if !found {
262 if j.MsgTypeURL == "" && j.MsgBytes == "" {
263 // exit with error
264 return ErrUnknownMessageName(j.Type)
265 }
266 var msgValue []byte
267 if j.MsgBytes != "" {
268 msgValue, err = StringToBytes(j.MsgBytes)
269 if err != nil {
270 return err
271 }
272 }
273 // populate the underlying transaction object using raw any bytes
274 *x = Transaction{
275 MessageType: j.Type,
276 Msg: &anypb.Any{TypeUrl: j.MsgTypeURL, Value: msgValue},
277 Signature: j.Signature,
278 CreatedHeight: j.CreatedHeight,
279 Time: j.Time,
280 Fee: j.Fee,
281 Memo: j.Memo,
282 NetworkId: j.NetworkId,
283 ChainId: j.ChainId,
284 }
285 return nil
286 }
287 // create a new instance of the message
288 msg := m.New()

Callers

nothing calls this directly

Calls 7

ErrUnknownMessageNameFunction · 0.85
StringToBytesFunction · 0.85
NewAnyFunction · 0.85
UnmarshalMethod · 0.65
CodeMethod · 0.65
NewMethod · 0.65

Tested by

no test coverage detected