centrifuge / go-substrate-rpc-client

Substrate RPC client for go aka GSRPC
Apache License 2.0
204 stars 178 forks source link

Transaction signature seems to be incorrectly formatted #363

Open ashishkhuraishy opened 1 year ago

ashishkhuraishy commented 1 year ago

I have created an example pallet from the tutorials and i want to interact with that pallet programatically.

#[pallet::call]
impl<T: Config> Pallet<T> {
  #[pallet::call_index(1)]
  #[pallet::weight((T::WeightInfo::create_claim(), Pays::No))]
  pub fn create_claim(origin: OriginFor<T>, claim: T::Hash) -> DispatchResult {
      // Check that the extrinsic was signed and get the signer.
      // This function will return an error if the extrinsic is not signed.
      let sender = ensure_signed(origin)?;

      // let sender = "157VKbUv5RAucscCSXL8AgLdKtjB5gm7JTGQsbq8MZcQpXwA".to_string();
      // Verify that the specified claim has not already been stored.
      ensure!(!Claims::<T>::contains_key(&claim), Error::<T>::AlreadyClaimed);

      // Store the claim with the sender and block number.
      Claims::<T>::insert(&claim, &sender);

      // Emit an event that the claim was created.
      Self::deposit_event(Event::CreateClaim { who: sender, claim });

      Ok(())
  }
}

using the package i am able to create the call and invoke the submitExtrinsic

rpcUrl := "ws://127.0.0.1:9944"

api, err := gsrpc.NewSubstrateAPI(rpcUrl)

meta, err := api.RPC.State.GetMetadataLatest()
genesisHash, err := api.RPC.Chain.GetBlockHash(0)

rv, err := api.RPC.State.GetRuntimeVersionLatest()
keyRing, err := GetKeyPair()

key, err := types.CreateStorageKey(meta, "System", "Account", keyRing.PublicKey)

var accountInfo types.AccountInfo
ok, err := api.RPC.State.GetStorageLatest(key, &accountInfo)
latestBlock, _ := api.RPC.Chain.GetBlockHashLatest()

nonce := uint32(accountInfo.Nonce)
fmt.Println("nonce", nonce)
o := types.SignatureOptions{
  BlockHash:          latestBlock,
  Era:                types.ExtrinsicEra{IsImmortalEra: true},
  GenesisHash:        genesisHash,
  Nonce:              types.NewUCompactFromUInt(uint64(nonce)),
  Tip:                types.NewUCompactFromUInt(10000000000),
  SpecVersion:        rv.SpecVersion,
  TransactionVersion: rv.TransactionVersion,
}

parameters := []interface{}{"a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447"}
call, err := types.NewCall(
  meta,
  "TemplateModule.create_claim",
  parameters,
)

ext := types.NewExtrinsic(call)
val, err := codec.EncodeToHex(ext)
fmt.Println(val)

err = ext.Sign(keyRing, o)

val, _ = codec.EncodeToHex(ext)
fmt.Println(val)

subscription, err := api.RPC.Author.SubmitAndWatchExtrinsic(ext)
if err != nil {
  fmt.Println("error while executing")
  log.Fatalln(err)
}

The output of the code is throwing an error in the last stage which is not giving out much info on why its happening

error while executing
2023/10/20 12:04:23 Verification Error: Runtime error: Execution failed: Execution aborted due to trap: wasm trap: wasm `unreachable` instruction executed
WASM backtrace:
error while executing at wasm backtrace:
    0: 0x7f221 - <unknown>!rust_begin_unwind
    1: 0x24aa - <unknown>!core::panicking::panic_fmt::h0393390f71e5218b
    2: 0x36f7d - <unknown>!TaggedTransactionQueue_validate_transaction
exit status 1

but when i used the hex encoded values on the frontend template and invoked submitExtrinsic i got image (when i used the ext hex value pre-signing)

image (when using the ext hex value after signing)

the error seems to suggest bad signature and i followed everything from the *_test.go files. Any help would be appreciated

cdamian commented 11 months ago

Hey,

This could be due to the fact that your chain has different SignedExtra defined. This might require a different kind of SignatureOptions that use something else instead of the normal Tip that we provide.

Please refer to the example provided here - https://github.com/centrifuge/go-substrate-rpc-client/blob/master/registry/retriever/extrinsic_retriever_live_test.go#L42, and check whether you need the non-default option found there.

You can then adjust the SignatureOptions on your end and try to submit an extrinsic using that.