diff --git a/models/signature.go b/models/signature.go index 08d39e9..baba62c 100644 --- a/models/signature.go +++ b/models/signature.go @@ -1,5 +1,7 @@ package models +import "encoding/json" + type JSONSignature struct { Type string `json:"type"` ED25519Signature @@ -12,11 +14,31 @@ type ED25519Signature struct { Signature string `json:"signature"` } +type SignatureContent struct { + Index uint64 + Signature string +} + +func (s *SignatureContent) UnmarshalJSON(raw []byte) error { + if len(raw) >= 2 && raw[0] == '"' && raw[len(raw)-1] == '"' { + return json.Unmarshal(raw, &s.Signature) + } + var t struct { + Index uint64 `json:"index"` + Signature string `json:"signature"` + } + if err := json.Unmarshal(raw, &t); err != nil { + return err + } + s.Index, s.Signature = t.Index, t.Signature + return nil +} + type MultiED25519Signature struct { - PublicKeys []string `json:"public_keys"` - Signatures []string `json:"signatures"` - Threshold uint8 `json:"threshold"` - Bitmap string `json:"bitmap"` + PublicKeys []string `json:"public_keys"` + Signatures []SignatureContent `json:"signatures"` + Threshold uint8 `json:"threshold"` + Bitmap string `json:"bitmap"` } type MultiAgentSignature struct { diff --git a/models/signature_test.go b/models/signature_test.go new file mode 100644 index 0000000..05227d6 --- /dev/null +++ b/models/signature_test.go @@ -0,0 +1,136 @@ +package models + +import ( + "encoding/json" + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_unmarshalSignature0(t *testing.T) { + cnt := ` +{ + "public_key": "0xde062760a3477cc7c779239cff774ac123196dac5f448126c8f10a396bdf61ff", + "signature": "0xcab74ee1a7be09d5dded094dbcff218f27bd451387782e76d4ab9fe5dc15d2c4c0274350869913cbcb0d24f475fe7edbf71e048e6a208f6a7ecc4a3887c63702", + "type": "ed25519_signature" +} +` + var s JSONSignature + assert.NoError(t, json.Unmarshal([]byte(cnt), &s)) + assert.Equal(t, JSONSignature{ + Type: "ed25519_signature", + ED25519Signature: ED25519Signature{ + PublicKey: "0xde062760a3477cc7c779239cff774ac123196dac5f448126c8f10a396bdf61ff", + Signature: "0xcab74ee1a7be09d5dded094dbcff218f27bd451387782e76d4ab9fe5dc15d2c4c0274350869913cbcb0d24f475fe7edbf71e048e6a208f6a7ecc4a3887c63702", + }, + MultiED25519Signature: MultiED25519Signature{}, + MultiAgentSignature: MultiAgentSignature{}, + }, s) +} + +func Test_unmarshalSignature1(t *testing.T) { + cnt := ` +{ + "public_keys": [ + "0x1b68747470733a2f2f6163636f756e74732e676f6f676c652e636f6d2073de33a2f41ce989a61922213b0de92a90b423aa36e781aa15e6d8910bf65826", + "0x1b68747470733a2f2f6163636f756e74732e676f6f676c652e636f6d2086b8bffaa6459d9a61a5f5c73b0ac4d8dfd33f2956b0652fbac35bda858eb71d" + ], + "signatures": [ + { + "index": 0, + "signature": "0x0000e3baf8dab67112e7ffd2f8c10ad5b81937dcc43d8102b7c0e851309e2515a2244aeb75f100b7d2cec2ab34024677d1b436b1fbcb09ea87e3d25ba86257a9c71dd2783876dbcfb84f11d5f39d9d97144f1bb56f53d127a71f6811d9a9eb0e62110ad8e83aa127cc9cb26670cebaf68c61abf39685ddf782b5ab7cd987b1be198d80969800000000000000010040e4f3f2cfc9c9b2976f9cd1556d28365bca89982953fcc406d839dfcf5e215605049b4dedd588bf8511732d73499e248574a8cd421403b48882fdc8112c01180e4c7b22616c67223a225253323536222c226b6964223a2239336234393531363261663063383763633761353136383632393430393730343064616633623433222c22747970223a224a5754227d3067b666000000000020d04ab232742bb4ab3a1368bd4615e4e6d0224ab71a016baf8520a332c97787370040a8f6dd3dd444765fe7cc10132156d947a61141b0261173f9b73aee05233ebde982a8170d9d08590f7ea8737864fb90ee69dda2ea7553ead8e32ab8fa01a72809" + } + ], + "signatures_required": 1, + "type": "single_sender" +} +` + var s JSONSignature + assert.NoError(t, json.Unmarshal([]byte(cnt), &s)) + assert.Equal(t, JSONSignature{ + Type: "single_sender", + ED25519Signature: ED25519Signature{}, + MultiED25519Signature: MultiED25519Signature{ + PublicKeys: []string{ + "0x1b68747470733a2f2f6163636f756e74732e676f6f676c652e636f6d2073de33a2f41ce989a61922213b0de92a90b423aa36e781aa15e6d8910bf65826", + "0x1b68747470733a2f2f6163636f756e74732e676f6f676c652e636f6d2086b8bffaa6459d9a61a5f5c73b0ac4d8dfd33f2956b0652fbac35bda858eb71d", + }, + Signatures: []SignatureContent{{ + Index: 0, + Signature: "0x0000e3baf8dab67112e7ffd2f8c10ad5b81937dcc43d8102b7c0e851309e2515a2244aeb75f100b7d2cec2ab34024677d1b436b1fbcb09ea87e3d25ba86257a9c71dd2783876dbcfb84f11d5f39d9d97144f1bb56f53d127a71f6811d9a9eb0e62110ad8e83aa127cc9cb26670cebaf68c61abf39685ddf782b5ab7cd987b1be198d80969800000000000000010040e4f3f2cfc9c9b2976f9cd1556d28365bca89982953fcc406d839dfcf5e215605049b4dedd588bf8511732d73499e248574a8cd421403b48882fdc8112c01180e4c7b22616c67223a225253323536222c226b6964223a2239336234393531363261663063383763633761353136383632393430393730343064616633623433222c22747970223a224a5754227d3067b666000000000020d04ab232742bb4ab3a1368bd4615e4e6d0224ab71a016baf8520a332c97787370040a8f6dd3dd444765fe7cc10132156d947a61141b0261173f9b73aee05233ebde982a8170d9d08590f7ea8737864fb90ee69dda2ea7553ead8e32ab8fa01a72809", + }}, + }, + MultiAgentSignature: MultiAgentSignature{}, + }, s) +} + +func Test_unmarshalSignature2(t *testing.T) { + cnt := ` +{ + "public_keys": [ + "0x1b68747470733a2f2f6163636f756e74732e676f6f676c652e636f6d2073de33a2f41ce989a61922213b0de92a90b423aa36e781aa15e6d8910bf65826", + "0x1b68747470733a2f2f6163636f756e74732e676f6f676c652e636f6d2086b8bffaa6459d9a61a5f5c73b0ac4d8dfd33f2956b0652fbac35bda858eb71d" + ], + "signatures": [ + "0x0000e3baf8dab67112e7ffd2f8c10ad5b81937dcc43d8102b7c0e851309e2515a2244aeb75f100b7d2cec2ab34024677d1b436b1fbcb09ea87e3d25ba86257a9c71dd2783876dbcfb84f11d5f39d9d97144f1bb56f53d127a71f6811d9a9eb0e62110ad8e83aa127cc9cb26670cebaf68c61abf39685ddf782b5ab7cd987b1be198d80969800000000000000010040e4f3f2cfc9c9b2976f9cd1556d28365bca89982953fcc406d839dfcf5e215605049b4dedd588bf8511732d73499e248574a8cd421403b48882fdc8112c01180e4c7b22616c67223a225253323536222c226b6964223a2239336234393531363261663063383763633761353136383632393430393730343064616633623433222c22747970223a224a5754227d3067b666000000000020d04ab232742bb4ab3a1368bd4615e4e6d0224ab71a016baf8520a332c97787370040a8f6dd3dd444765fe7cc10132156d947a61141b0261173f9b73aee05233ebde982a8170d9d08590f7ea8737864fb90ee69dda2ea7553ead8e32ab8fa01a72809" + ], + "signatures_required": 1, + "type": "single_sender" +} +` + var s JSONSignature + assert.NoError(t, json.Unmarshal([]byte(cnt), &s)) + assert.Equal(t, JSONSignature{ + Type: "single_sender", + ED25519Signature: ED25519Signature{}, + MultiED25519Signature: MultiED25519Signature{ + PublicKeys: []string{ + "0x1b68747470733a2f2f6163636f756e74732e676f6f676c652e636f6d2073de33a2f41ce989a61922213b0de92a90b423aa36e781aa15e6d8910bf65826", + "0x1b68747470733a2f2f6163636f756e74732e676f6f676c652e636f6d2086b8bffaa6459d9a61a5f5c73b0ac4d8dfd33f2956b0652fbac35bda858eb71d", + }, + Signatures: []SignatureContent{{ + Index: 0, + Signature: "0x0000e3baf8dab67112e7ffd2f8c10ad5b81937dcc43d8102b7c0e851309e2515a2244aeb75f100b7d2cec2ab34024677d1b436b1fbcb09ea87e3d25ba86257a9c71dd2783876dbcfb84f11d5f39d9d97144f1bb56f53d127a71f6811d9a9eb0e62110ad8e83aa127cc9cb26670cebaf68c61abf39685ddf782b5ab7cd987b1be198d80969800000000000000010040e4f3f2cfc9c9b2976f9cd1556d28365bca89982953fcc406d839dfcf5e215605049b4dedd588bf8511732d73499e248574a8cd421403b48882fdc8112c01180e4c7b22616c67223a225253323536222c226b6964223a2239336234393531363261663063383763633761353136383632393430393730343064616633623433222c22747970223a224a5754227d3067b666000000000020d04ab232742bb4ab3a1368bd4615e4e6d0224ab71a016baf8520a332c97787370040a8f6dd3dd444765fe7cc10132156d947a61141b0261173f9b73aee05233ebde982a8170d9d08590f7ea8737864fb90ee69dda2ea7553ead8e32ab8fa01a72809", + }}, + }, + MultiAgentSignature: MultiAgentSignature{}, + }, s) +} + +func Test_unmarshalSignature3(t *testing.T) { + cnt := ` +{ + "sender": { + "public_key": "0xbee215385a76fad8117faac6e65f9934323e0f2a80ec9a219ae3399d09d74563", + "signature": "0x35afffb39a097f7e76f7ea40588a98ce96a324da02e1199f475928068f882766b70ee59d7d8a57cb3d0feec1a3865efbdd6c3b4230f4329de7beee83cf7aea0c", + "type": "ed25519_signature" + }, + "secondary_signer_addresses": [], + "secondary_signers": [], + "fee_payer_address": "0x587f66c999fed81c8872c5ddabed1e7049c790d750d2a796313a32702dcff962", + "fee_payer_signer": { + "public_key": "0x1fca2d4043456ce1edfb83d2aee3537a38a58b25c1ddaa84df024905ef7e0f14", + "signature": "0x3e1e22b2ef32bf0e068c821983d868934508feb80156e44ecc828f8bdbe49ebbb7dc67ef8b818374def2624c71b0ea8f4f08f9d3d7c1997c37a887acd202c909", + "type": "ed25519_signature" + }, + "type": "fee_payer_signature" +} +` + var s JSONSignature + assert.NoError(t, json.Unmarshal([]byte(cnt), &s)) + assert.Equal(t, JSONSignature{ + Type: "fee_payer_signature", + ED25519Signature: ED25519Signature{}, + MultiED25519Signature: MultiED25519Signature{}, + MultiAgentSignature: MultiAgentSignature{ + Sender: JSONSigner{ + Type: "ed25519_signature", + ED25519Signature: ED25519Signature{ + PublicKey: "0xbee215385a76fad8117faac6e65f9934323e0f2a80ec9a219ae3399d09d74563", + Signature: "0x35afffb39a097f7e76f7ea40588a98ce96a324da02e1199f475928068f882766b70ee59d7d8a57cb3d0feec1a3865efbdd6c3b4230f4329de7beee83cf7aea0c", + }, + }, + SecondarySignerAddresses: []string{}, + SecondarySigners: []JSONSigner{}, + }, + }, s) +}