github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/multi_signature.go (about)

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package types
    18  
    19  import "github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale"
    20  
    21  // MultiSignature
    22  type MultiSignature struct {
    23  	IsEd25519 bool      // 0:: Ed25519(Ed25519Signature)
    24  	AsEd25519 Signature // Ed25519Signature
    25  	IsSr25519 bool      // 1:: Sr25519(Sr25519Signature)
    26  	AsSr25519 Signature // Sr25519Signature
    27  	IsEcdsa   bool      // 2:: Ecdsa(EcdsaSignature)
    28  	AsEcdsa   Bytes     // EcdsaSignature
    29  }
    30  
    31  func (m *MultiSignature) Decode(decoder scale.Decoder) error {
    32  	b, err := decoder.ReadOneByte()
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	switch b {
    38  	case 0:
    39  		m.IsEd25519 = true
    40  		err = decoder.Decode(&m.AsEd25519)
    41  	case 1:
    42  		m.IsSr25519 = true
    43  		err = decoder.Decode(&m.AsSr25519)
    44  	case 2:
    45  		m.IsEcdsa = true
    46  		err = decoder.Decode(&m.AsEcdsa)
    47  	}
    48  
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func (m MultiSignature) Encode(encoder scale.Encoder) error {
    57  	var err1, err2 error
    58  	switch {
    59  	case m.IsEd25519:
    60  		err1 = encoder.PushByte(0)
    61  		err2 = encoder.Encode(m.AsEd25519)
    62  	case m.IsSr25519:
    63  		err1 = encoder.PushByte(1)
    64  		err2 = encoder.Encode(m.AsSr25519)
    65  	case m.IsEcdsa:
    66  		err1 = encoder.PushByte(2)
    67  		err2 = encoder.Encode(m.AsEcdsa)
    68  	}
    69  
    70  	if err1 != nil {
    71  		return err1
    72  	}
    73  	if err2 != nil {
    74  		return err2
    75  	}
    76  
    77  	return nil
    78  }