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

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale"
     7  )
     8  
     9  type MultiAddress struct {
    10  	IsID        bool
    11  	AsID        AccountID
    12  	IsIndex     bool
    13  	AsIndex     AccountIndex
    14  	IsRaw       bool
    15  	AsRaw       []byte
    16  	IsAddress32 bool
    17  	AsAddress32 [32]byte
    18  	IsAddress20 bool
    19  	AsAddress20 [20]byte
    20  }
    21  
    22  // NewMultiAddressFromAccountID creates an Address from the given AccountID (public key)
    23  func NewMultiAddressFromAccountID(b []byte) MultiAddress {
    24  	return MultiAddress{
    25  		IsID: true,
    26  		AsID: NewAccountID(b),
    27  	}
    28  }
    29  
    30  // NewMultiAddressFromHexAccountID creates an Address from the given hex string that contains an AccountID (public key)
    31  func NewMultiAddressFromHexAccountID(str string) (MultiAddress, error) {
    32  	b, err := HexDecodeString(str)
    33  	if err != nil {
    34  		return MultiAddress{}, err
    35  	}
    36  	return NewMultiAddressFromAccountID(b), nil
    37  }
    38  
    39  func (m MultiAddress) Encode(encoder scale.Encoder) error {
    40  	var err error
    41  	switch {
    42  	case m.IsID:
    43  		err = encoder.PushByte(0)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		err = encoder.Encode(m.AsID)
    48  		if err != nil {
    49  			return err
    50  		}
    51  	case m.IsIndex:
    52  		err = encoder.PushByte(1)
    53  		if err != nil {
    54  			return err
    55  		}
    56  		err = encoder.Encode(m.AsIndex)
    57  		if err != nil {
    58  			return err
    59  		}
    60  	case m.IsRaw:
    61  		err = encoder.PushByte(2)
    62  		if err != nil {
    63  			return err
    64  		}
    65  		err = encoder.Encode(m.AsRaw)
    66  		if err != nil {
    67  			return err
    68  		}
    69  	case m.IsAddress32:
    70  		err = encoder.PushByte(3)
    71  		if err != nil {
    72  			return err
    73  		}
    74  		err = encoder.Encode(m.AsAddress32)
    75  		if err != nil {
    76  			return err
    77  		}
    78  	case m.IsAddress20:
    79  		err = encoder.PushByte(4)
    80  		if err != nil {
    81  			return err
    82  		}
    83  		err = encoder.Encode(m.AsAddress20)
    84  		if err != nil {
    85  			return err
    86  		}
    87  	default:
    88  		return fmt.Errorf("Invalid variant for MultiAddress")
    89  	}
    90  
    91  	return nil
    92  }
    93  
    94  func (m MultiAddress) Decode(decoder scale.Decoder) error {
    95  	tag, err := decoder.ReadOneByte()
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	switch tag {
   101  	case 0:
   102  		m.IsID = true
   103  		err = decoder.Decode(&m.AsID)
   104  	case 1:
   105  		m.IsIndex = true
   106  		err = decoder.Decode(&m.AsIndex)
   107  	case 2:
   108  		m.IsRaw = true
   109  		err = decoder.Decode(&m.AsRaw)
   110  	case 3:
   111  		m.IsAddress32 = true
   112  		err = decoder.Decode(&m.AsAddress32)
   113  	case 4:
   114  		m.IsAddress20 = true
   115  		err = decoder.Decode(&m.AsAddress20)
   116  	default:
   117  		return fmt.Errorf("Invalid variant for MultiAddress")
   118  	}
   119  
   120  	if err != nil {
   121  		return err
   122  	}
   123  
   124  	return nil
   125  }