Skip to content

Commit

Permalink
signature.type can be single_sender in transaction #1015222711 in tes…
Browse files Browse the repository at this point in the history
…tnet
  • Loading branch information
lizixing committed Apr 18, 2024
1 parent 9a5201c commit 46d0c60
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
44 changes: 44 additions & 0 deletions models/signature.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package models

import (
"encoding/json"
"fmt"
"strings"
)

type JSONSignature struct {
Type string `json:"type"`
ED25519Signature
MultiED25519Signature
MultiAgentSignature
SingleSender
}

type ED25519Signature struct {
Expand All @@ -25,8 +32,45 @@ type MultiAgentSignature struct {
SecondarySigners []JSONSigner `json:"secondary_signers"`
}

type SingleSenderSignature struct {
Index uint64 `json:"index"`
Signature string `json:"signature"`
}
type SingleSender struct {
PublicKeys []string `json:"public_keys"`
Signatures []SingleSenderSignature `json:"signatures"`
SignaturesRequired uint8 `json:"signatures_required"`
}

type JSONSigner struct {
Type string `json:"type"`
ED25519Signature
MultiED25519Signature
}

func (s *JSONSignature) UnmarshalJSON(raw []byte) error {
var simple struct {
Type string `json:"type"`
}
err := json.Unmarshal(raw, &simple)
if err != nil {
return err
}
switch strings.ToLower(strings.ReplaceAll(simple.Type, "_", "")) {
case "ed25519signature":
err = json.Unmarshal(raw, &s.ED25519Signature)
case "multied25519signature":
err = json.Unmarshal(raw, &s.MultiED25519Signature)
case "multiagentsignature":
err = json.Unmarshal(raw, &s.MultiAgentSignature)
case "singlesender":
err = json.Unmarshal(raw, &s.SingleSender)
default:
return fmt.Errorf("unknown signature type %q", simple.Type)
}
if err != nil {
return err
}
s.Type = simple.Type
return nil
}
67 changes: 67 additions & 0 deletions models/signature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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{},
SingleSender: SingleSender{},
}, 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{},
MultiAgentSignature: MultiAgentSignature{},
SingleSender: SingleSender{
PublicKeys: []string{
"0x1b68747470733a2f2f6163636f756e74732e676f6f676c652e636f6d2073de33a2f41ce989a61922213b0de92a90b423aa36e781aa15e6d8910bf65826",
"0x1b68747470733a2f2f6163636f756e74732e676f6f676c652e636f6d2086b8bffaa6459d9a61a5f5c73b0ac4d8dfd33f2956b0652fbac35bda858eb71d",
},
Signatures: []SingleSenderSignature{{
Index: 0,
Signature: "0x0000e3baf8dab67112e7ffd2f8c10ad5b81937dcc43d8102b7c0e851309e2515a2244aeb75f100b7d2cec2ab34024677d1b436b1fbcb09ea87e3d25ba86257a9c71dd2783876dbcfb84f11d5f39d9d97144f1bb56f53d127a71f6811d9a9eb0e62110ad8e83aa127cc9cb26670cebaf68c61abf39685ddf782b5ab7cd987b1be198d80969800000000000000010040e4f3f2cfc9c9b2976f9cd1556d28365bca89982953fcc406d839dfcf5e215605049b4dedd588bf8511732d73499e248574a8cd421403b48882fdc8112c01180e4c7b22616c67223a225253323536222c226b6964223a2239336234393531363261663063383763633761353136383632393430393730343064616633623433222c22747970223a224a5754227d3067b666000000000020d04ab232742bb4ab3a1368bd4615e4e6d0224ab71a016baf8520a332c97787370040a8f6dd3dd444765fe7cc10132156d947a61141b0261173f9b73aee05233ebde982a8170d9d08590f7ea8737864fb90ee69dda2ea7553ead8e32ab8fa01a72809",
}},
SignaturesRequired: 1,
},
}, s)
}

0 comments on commit 46d0c60

Please sign in to comment.