github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/extrinsic_payload.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 (
    20  	"fmt"
    21  
    22  	"github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale"
    23  	"github.com/stafiprotocol/go-substrate-rpc-client/signature"
    24  )
    25  
    26  // ExtrinsicPayloadV3 is a signing payload for an Extrinsic. For the final encoding, it is variable length based on
    27  // the contents included. Note that `BytesBare` is absolutely critical – we don't want the method (Bytes)
    28  // to have the length prefix included. This means that the data-as-signed is un-decodable,
    29  // but is also doesn't need the extra information, only the pure data (and is not decoded)
    30  // ... The same applies to V1 & V1, if we have a V4, carry move this comment to latest
    31  type ExtrinsicPayloadV3 struct {
    32  	Method      BytesBare
    33  	Era         ExtrinsicEra // extra via system::CheckEra
    34  	Nonce       UCompact     // extra via system::CheckNonce (Compact<Index> where Index is u32)
    35  	Tip         UCompact     // extra via balances::TakeFees (Compact<Balance> where Balance is u128)
    36  	SpecVersion U32          // additional via system::CheckVersion
    37  	GenesisHash Hash         // additional via system::CheckGenesis
    38  	BlockHash   Hash         // additional via system::CheckEra
    39  }
    40  
    41  // Sign the extrinsic payload with the given derivation path
    42  func (e ExtrinsicPayloadV3) Sign(signer signature.KeyringPair) (Signature, error) {
    43  	b, err := EncodeToBytes(e)
    44  	if err != nil {
    45  		return Signature{}, err
    46  	}
    47  
    48  	sig, err := signature.Sign(b, signer.URI)
    49  	return NewSignature(sig), err
    50  }
    51  
    52  // Encode implements encoding for ExtrinsicPayloadV3, which just unwraps the bytes of ExtrinsicPayloadV3 without
    53  // adding a compact length prefix
    54  func (e ExtrinsicPayloadV3) Encode(encoder scale.Encoder) error {
    55  	err := encoder.Encode(e.Method)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	err = encoder.Encode(e.Era)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	err = encoder.Encode(e.Nonce)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	err = encoder.Encode(e.Tip)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	err = encoder.Encode(e.SpecVersion)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	err = encoder.Encode(e.GenesisHash)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	err = encoder.Encode(e.BlockHash)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  // Decode does nothing and always returns an error. ExtrinsicPayloadV3 is only used for encoding, not for decoding
    94  func (e *ExtrinsicPayloadV3) Decode(decoder scale.Decoder) error {
    95  	return fmt.Errorf("decoding of ExtrinsicPayloadV3 is not supported")
    96  }
    97  
    98  type ExtrinsicPayloadV4 struct {
    99  	ExtrinsicPayloadV3
   100  	TransactionVersion U32
   101  }
   102  
   103  // Sign the extrinsic payload with the given derivation path
   104  func (e ExtrinsicPayloadV4) Sign(signer signature.KeyringPair) (Signature, error) {
   105  	b, err := EncodeToBytes(e)
   106  	if err != nil {
   107  		return Signature{}, err
   108  	}
   109  
   110  	sig, err := signature.Sign(b, signer.URI)
   111  	return NewSignature(sig), err
   112  }
   113  
   114  func (e ExtrinsicPayloadV4) Encode(encoder scale.Encoder) error {
   115  	err := encoder.Encode(e.Method)
   116  	if err != nil {
   117  		return err
   118  	}
   119  
   120  	err = encoder.Encode(e.Era)
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	err = encoder.Encode(e.Nonce)
   126  	if err != nil {
   127  		return err
   128  	}
   129  
   130  	err = encoder.Encode(e.Tip)
   131  	if err != nil {
   132  		return err
   133  	}
   134  
   135  	err = encoder.Encode(e.SpecVersion)
   136  	if err != nil {
   137  		return err
   138  	}
   139  
   140  	err = encoder.Encode(e.TransactionVersion)
   141  	if err != nil {
   142  		return err
   143  	}
   144  
   145  	err = encoder.Encode(e.GenesisHash)
   146  	if err != nil {
   147  		return err
   148  	}
   149  
   150  	err = encoder.Encode(e.BlockHash)
   151  	if err != nil {
   152  		return err
   153  	}
   154  
   155  	return nil
   156  }
   157  
   158  // Decode does nothing and always returns an error. ExtrinsicPayloadV4 is only used for encoding, not for decoding
   159  func (e *ExtrinsicPayloadV4) Decode(decoder scale.Decoder) error {
   160  	return fmt.Errorf("decoding of ExtrinsicPayloadV4 is not supported")
   161  }