Skip to content

Commit d1995a8

Browse files
Add support for empty actions.
1 parent fcb0390 commit d1995a8

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

dist/golang/protocol/protocol.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ func Deserialize(script bitcoin.Script, isTest bool) (actions.Action, error) {
106106
return nil, ErrNotTokenized
107107
}
108108

109-
if msg.PayloadCount() != 3 {
109+
payloadCount := msg.PayloadCount()
110+
if payloadCount != 2 && payloadCount != 3 {
110111
return nil, errors.Wrapf(ErrNotTokenized, "wrong payload count: %d",
111112
msg.PayloadCount())
112113
}
@@ -119,6 +120,17 @@ func Deserialize(script bitcoin.Script, isTest bool) (actions.Action, error) {
119120
return nil, ErrUnknownVersion
120121
}
121122

123+
if payloadCount == 2 {
124+
// Action payload is not provided because it was empty.
125+
result := actions.NewActionFromCode(string(msg.PayloadAt(1)))
126+
if result == nil {
127+
return nil, errors.Wrapf(ErrNotTokenized, "unknown action code: %s",
128+
string(msg.PayloadAt(1)))
129+
}
130+
131+
return result, nil
132+
}
133+
122134
return actions.Deserialize(msg.PayloadAt(1), msg.PayloadAt(2))
123135
}
124136
}
@@ -154,9 +166,9 @@ func ActionCodeForScript(script bitcoin.Script, isTest bool) (string, error) {
154166
return "", ErrNotTokenized
155167
}
156168

157-
if msg.PayloadCount() != 3 {
158-
return "", errors.Wrapf(ErrNotTokenized, "wrong payload count: %d",
159-
msg.PayloadCount())
169+
payloadCount := msg.PayloadCount()
170+
if payloadCount != 2 && payloadCount != 3 {
171+
return "", errors.Wrapf(ErrNotTokenized, "wrong payload count: %d", payloadCount)
160172
}
161173

162174
version, _, err := bitcoin.ParsePushNumberScript(msg.PayloadAt(0))
@@ -179,7 +191,12 @@ func WrapAction(action actions.Action, isTest bool) (envelope.BaseMessage, error
179191
}
180192

181193
message := envelopeV1.NewMessage([][]byte{GetProtocolID(isTest)},
182-
[][]byte{bitcoin.PushNumberScript(int64(Version)), []byte(action.Code()), payload})
194+
[][]byte{bitcoin.PushNumberScript(int64(Version)), []byte(action.Code())})
195+
196+
if len(payload) > 0 {
197+
// Only add action payload if it isn't empty.
198+
message.AddPayload(payload)
199+
}
183200

184201
return message, nil
185202
}

dist/golang/protocol/protocol_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func Test_SerializeAction(t *testing.T) {
2121
},
2222
code: actions.CodeContractOffer,
2323
},
24+
{ // Empty action
25+
action: &actions.ContractOffer{},
26+
code: actions.CodeContractOffer,
27+
},
2428
}
2529

2630
for i, test := range tests {

0 commit comments

Comments
 (0)