-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Polkadot cannot sign DynamicExtrinsic #401
Comments
Using the previous approach is also not feasible func Transfer(keyingPair signature.KeyringPair) {
// Instantiate the API
api, err := gsrpc.NewSubstrateAPI("wss://polkadot-rpc.dwellir.com")
if err != nil {
panic(err)
}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
panic(err)
}
bob, err := types.NewMultiAddressFromHexAccountID("0x1e635742bc09f1299c2934cbc46266a8c6b5756bc261fc0a970b6a51d1a4d053")
if err != nil {
panic(err)
}
// 0.1 unit of transfer
bal, ok := new(big.Int).SetString("1000000000", 10)
if !ok {
panic(fmt.Errorf("failed to convert balance"))
}
c, err := types.NewCall(meta, "Balances.transfer_keep_alive", bob, types.NewUCompact(bal))
if err != nil {
panic(err)
}
// Extrinsic
ext := types.NewExtrinsic(c)
genesisHash, err := api.RPC.Chain.GetBlockHash(0)
if err != nil {
panic(err)
}
rv, err := api.RPC.State.GetRuntimeVersionLatest()
if err != nil {
panic(err)
}
key, err := types.CreateStorageKey(meta, "System", "Account", keyingPair.PublicKey)
if err != nil {
panic(err)
}
var accountInfo types.AccountInfo
ok, err = api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil || !ok {
panic(err)
}
encodedExt, err := codec.EncodeToHex(ext)
if err != nil {
panic(err)
}
fmt.Printf("Ext - %s\n", encodedExt)
options := types.SignatureOptions{
BlockHash: genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: genesisHash,
Nonce: types.NewUCompactFromUInt(uint64(accountInfo.Nonce)),
SpecVersion: rv.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
TransactionVersion: rv.TransactionVersion,
}
err = ext.Sign(keyingPair, options)
if err != nil {
panic(err)
}
txHash, err := api.RPC.Author.SubmitExtrinsic(ext)
if err != nil {
panic(err)
}
fmt.Println(txHash.Hex())
} |
@YuXiaoCoder Please try using the latest version of dynamic extrinsic signing, and by using these extensions when signing in your first example:
|
Hello everyone. My code stopped to work here is an example. (westend network)
package main
import (
"bytes"
"errors"
"fmt"
"math/big"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/extrinsic"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/extrinsic/extensions"
"github.com/mr-tron/base58"
"github.com/shopspring/decimal"
"golang.org/x/crypto/blake2b"
)
func main() {
opts := types.SerDeOptions{NoPalletIndices: true}
types.SetSerDeOptions(opts)
a, err := gsrpc.NewSubstrateAPI("wss://westend-rpc.polkadot.io")
if err != nil {
panic(err)
}
fmt.Printf("api: %v\n", a)
to := "5D4vieYSoM8BARF3uD2dfuWqGtQTsh7X4DJBfsxxLPsvzCAT"
krFrom, err := signature.KeyringPairFromSecret("0xbe767cd48f3fda1535ca470506f1a5db5f8c12466951cd2164ef205bc2a628df", uint16(42))
if err != nil {
panic(err)
}
toNetwork, toAddrPublic, err := DecodeAddress(to)
if err != nil || toNetwork != uint8(42) {
panic(err)
}
// Decimals for mainnet - 10, testnet - 12
decimals := 12
coef := decimal.NewFromInt(10).Pow(decimal.NewFromInt32(int32(decimals)))
amount := decimal.RequireFromString("2.0")
amountAsBitInt := amount.Mul(coef).BigInt()
amountAsBitInt = big.NewInt(1000000000000)
meta, err := a.RPC.State.GetMetadataLatest()
if err != nil {
panic(err)
}
fmt.Println(meta.Version)
rv, err := a.RPC.State.GetRuntimeVersionLatest()
if err != nil {
panic(err)
}
toMultiAddress, err := types.NewMultiAddressFromAccountID(toAddrPublic)
if err != nil {
panic(err)
}
c, err := types.NewCall(meta,
"Balances.transfer_keep_alive", toMultiAddress, types.NewUCompact(amountAsBitInt))
if err != nil {
panic(err)
}
ext := extrinsic.NewDynamicExtrinsic(&c)
genesisHash, err := a.RPC.Chain.GetBlockHash(0)
if err != nil {
panic(err)
}
key, err := types.CreateStorageKey(meta, "System", "Account", krFrom.PublicKey)
if err != nil {
panic(err)
}
var accountInfo types.AccountInfo
ok, err := a.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil || !ok {
if err != nil {
panic(err)
} else {
panic(errors.New("no account"))
}
}
nonce := uint32(accountInfo.Nonce)
// Sign the transaction using Alice's default account
err = ext.Sign(
krFrom, meta,
extrinsic.WithGenesisHash(genesisHash),
extrinsic.WithNonce(types.NewUCompactFromUInt(uint64(nonce))),
extrinsic.WithTip(types.NewUCompactFromUInt(0)),
extrinsic.WithSpecVersion(rv.SpecVersion),
extrinsic.WithTransactionVersion(rv.TransactionVersion),
extrinsic.WithMetadataMode(extensions.CheckMetadataModeDisabled, extensions.CheckMetadataHash{Hash: types.NewEmptyOption[types.H256]()}),
extrinsic.WithAssetID(types.NewEmptyOption[types.AssetID]()),
extrinsic.WithEra(types.ExtrinsicEra{IsMortalEra: true}, genesisHash))
if err != nil {
panic(err)
}
encodedExt, err := codec.EncodeToHex(ext)
if err != nil {
panic(err)
}
fmt.Printf("Ext - %s\n", encodedExt)
sub, err := a.RPC.Author.SubmitAndWatchDynamicExtrinsic(ext)
if err != nil {
panic(err)
}
defer sub.Unsubscribe()
extInfo := <-sub.Chan()
tx, _ := extInfo.MarshalJSON()
fmt.Printf("submitted extrinsic: %s\n", tx)
}
func DecodeAddress(address string) (network byte, public []byte, err error) {
decoded, err := base58.Decode(address)
if err != nil {
return 0, nil, err
}
if len(decoded) < (1 + 32 + 2) {
return 0, nil, fmt.Errorf("wrong address")
}
networkWithPublic := decoded[:1+32]
//fmt.Println("networkWithPublic: ", hex.EncodeToString(networkWithPublic))
origCheckSum := decoded[1+32:]
//fmt.Println("origCheckSum: ", hex.EncodeToString(origCheckSum))
cs, err := ss58Checksum(networkWithPublic)
if err != nil {
return 0, nil, err
}
//fmt.Println("checsum:", hex.EncodeToString(cs))
checkSumToCompare := cs[:2]
//fmt.Println("checsumToCompare: ", hex.EncodeToString(checkSumToCompare))
if !bytes.Equal(origCheckSum, checkSumToCompare) {
return 0, nil, fmt.Errorf("wrong checksum")
}
return decoded[0], decoded[1 : 1+32], nil
}
func ss58Checksum(data []byte) ([]byte, error) {
hasher, err := blake2b.New(64, nil)
if err != nil {
return nil, err
}
_, err = hasher.Write([]byte(ss58Prefix))
if err != nil {
return nil, err
}
_, err = hasher.Write(data)
if err != nil {
return nil, err
}
return hasher.Sum(nil), nil
}
const (
ss58Prefix = "SS58PRE"
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error message:
panic: creating payload: signed extension 'PrevalidateAttests' is not supported
Here's the sample code
The text was updated successfully, but these errors were encountered: