Skip to content

Commit

Permalink
Merge pull request #7 from klever-io/support-old-extrinsic-type
Browse files Browse the repository at this point in the history
support old extrinsic type
  • Loading branch information
brunocampos-ssa authored Nov 8, 2024
2 parents 77eb08a + 569f189 commit 3dd06b4
Show file tree
Hide file tree
Showing 6 changed files with 490 additions and 0 deletions.
1 change: 1 addition & 0 deletions types/extrinsic/extensions/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ const (
PrevalidateAttestsSignedExtension SignedExtensionName = "PrevalidateAttests"
CheckNetworkMembershipSignedExtension SignedExtensionName = "CheckNetworkMembership"
SetEvmOriginSignedExtension SignedExtensionName = "SetEvmOrigin"
CheckAppIdSignedExtension SignedExtensionName = "CheckAppId"
)
6 changes: 6 additions & 0 deletions types/extrinsic/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ func WithGenesisHash(genesisHash types.Hash) SigningOption {
vals[GenesisHashSignedField] = genesisHash
}
}

func WithAppID(appID types.U32) SigningOption {
return func(vals SignedFieldValues) {
vals[AppIDSignedField] = appID
}
}
7 changes: 7 additions & 0 deletions types/extrinsic/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ const (
SpecVersionSignedField SignedFieldName = "spec_version"
TransactionVersionSignedField SignedFieldName = "transaction_version"
GenesisHashSignedField SignedFieldName = "genesis_hash"
AppIDSignedField SignedFieldName = "app_id"
)

// PayloadMutatorFn is the type used for mutating the Payload during creation.
Expand Down Expand Up @@ -218,6 +219,12 @@ var PayloadMutatorFns = map[extensions.SignedExtensionName]PayloadMutatorFn{
Value: &types.Hash{},
})
},
extensions.CheckAppIdSignedExtension: func(payload *Payload) {
payload.SignedExtraFields = append(payload.SignedFields, &SignedField{
Name: AppIDSignedField,
Value: types.U32(0),
})
},
// There's nothing that we can add in the payload or signature in the following cases, however, these are added to
// ensure that the extension is acknowledged and that the mutator check is passing.
extensions.CheckNonZeroSenderSignedExtension: func(payload *Payload) {},
Expand Down
162 changes: 162 additions & 0 deletions types/extrinsic_payload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
//
// Copyright 2019 Centrifuge GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package types

import (
"fmt"

"github.com/centrifuge/go-substrate-rpc-client/v4/scale"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

// ExtrinsicPayloadV3 is a signing payload for an Extrinsic. For the final encoding, it is variable length based on
// the contents included. Note that `BytesBare` is absolutely critical – we don't want the method (Bytes)
// to have the length prefix included. This means that the data-as-signed is un-decodable,
// but is also doesn't need the extra information, only the pure data (and is not decoded)
// ... The same applies to V1 & V1, if we have a V4, carry move this comment to latest
type ExtrinsicPayloadV3 struct {
Method BytesBare
Era ExtrinsicEra // extra via system::CheckEra
Nonce UCompact // extra via system::CheckNonce (Compact<Index> where Index is u32)
Tip UCompact // extra via balances::TakeFees (Compact<Balance> where Balance is u128)
SpecVersion U32 // additional via system::CheckVersion
GenesisHash Hash // additional via system::CheckGenesis
BlockHash Hash // additional via system::CheckEra
}

// Sign the extrinsic payload with the given derivation path
func (e ExtrinsicPayloadV3) Sign(signer signature.KeyringPair) (SignatureHash, error) {
b, err := codec.Encode(e)
if err != nil {
return SignatureHash{}, err
}

sig, err := signature.Sign(b, signer.URI)
return NewSignature(sig), err
}

// Encode implements encoding for ExtrinsicPayloadV3, which just unwraps the bytes of ExtrinsicPayloadV3 without
// adding a compact length prefix
func (e ExtrinsicPayloadV3) Encode(encoder scale.Encoder) error {
err := encoder.Encode(e.Method)
if err != nil {
return err
}

err = encoder.Encode(e.Era)
if err != nil {
return err
}

err = encoder.Encode(e.Nonce)
if err != nil {
return err
}

err = encoder.Encode(e.Tip)
if err != nil {
return err
}

err = encoder.Encode(e.SpecVersion)
if err != nil {
return err
}

err = encoder.Encode(e.GenesisHash)
if err != nil {
return err
}

err = encoder.Encode(e.BlockHash)
if err != nil {
return err
}

return nil
}

// Decode does nothing and always returns an error. ExtrinsicPayloadV3 is only used for encoding, not for decoding
func (e *ExtrinsicPayloadV3) Decode(decoder scale.Decoder) error {
return fmt.Errorf("decoding of ExtrinsicPayloadV3 is not supported")
}

type ExtrinsicPayloadV4 struct {
ExtrinsicPayloadV3
TransactionVersion U32
}

// Sign the extrinsic payload with the given derivation path
func (e ExtrinsicPayloadV4) Sign(signer signature.KeyringPair) (SignatureHash, error) {
b, err := codec.Encode(e)
if err != nil {
return SignatureHash{}, err
}

sig, err := signature.Sign(b, signer.URI)
return NewSignature(sig), err
}

func (e ExtrinsicPayloadV4) Encode(encoder scale.Encoder) error {
err := encoder.Encode(e.Method)
if err != nil {
return err
}

err = encoder.Encode(e.Era)
if err != nil {
return err
}

err = encoder.Encode(e.Nonce)
if err != nil {
return err
}

err = encoder.Encode(e.Tip)
if err != nil {
return err
}

err = encoder.Encode(e.SpecVersion)
if err != nil {
return err
}

err = encoder.Encode(e.TransactionVersion)
if err != nil {
return err
}

err = encoder.Encode(e.GenesisHash)
if err != nil {
return err
}

err = encoder.Encode(e.BlockHash)
if err != nil {
return err
}

return nil
}

// Decode does nothing and always returns an error. ExtrinsicPayloadV4 is only used for encoding, not for decoding
func (e *ExtrinsicPayloadV4) Decode(decoder scale.Decoder) error {
return fmt.Errorf("decoding of ExtrinsicPayloadV4 is not supported")
}
43 changes: 43 additions & 0 deletions types/extrinsic_signature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
//
// Copyright 2019 Centrifuge GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package types

type ExtrinsicSignatureV3 struct {
Signer Address
Signature SignatureHash
Era ExtrinsicEra // extra via system::CheckEra
Nonce UCompact // extra via system::CheckNonce (Compact<Index> where Index is u32))
Tip UCompact // extra via balances::TakeFees (Compact<Balance> where Balance is u128))
}

type ExtrinsicSignatureV4 struct {
Signer MultiAddress
Signature MultiSignature
Era ExtrinsicEra // extra via system::CheckEra
Nonce UCompact // extra via system::CheckNonce (Compact<Index> where Index is u32))
Tip UCompact // extra via balances::TakeFees (Compact<Balance> where Balance is u128))
}

type SignatureOptions struct {
Era ExtrinsicEra // extra via system::CheckEra
Nonce UCompact // extra via system::CheckNonce (Compact<Index> where Index is u32)
Tip UCompact // extra via balances::TakeFees (Compact<Balance> where Balance is u128)
SpecVersion U32 // additional via system::CheckSpecVersion
GenesisHash Hash // additional via system::CheckGenesis
BlockHash Hash // additional via system::CheckEra
TransactionVersion U32 // additional via system::CheckTxVersion
}
Loading

0 comments on commit 3dd06b4

Please sign in to comment.