go-substrate-rpc-client icon indicating copy to clipboard operation
go-substrate-rpc-client copied to clipboard

use ecdsa signature and submit extrinsic.but got 'Transaction has a bad signature'

Open litterGuy opened this issue 3 years ago • 6 comments

Due to the problem of public key derivation, I considered using ecdsa to generate an account. But after ecdsa signs, there will be signature errors. Where should I go to troubleshoot and solve this problem?

in the westend network,i use code like that:

scheme := ecdsa.Scheme{}
pari, err := scheme.FromSeed(secert)
if err != nil {
     return nil, nil, err
}

sig, err := signECDSA(data, hex.EncodeToString(signer.Seed()))
if err != nil {
    return e, err
}

multiSig := types.MultiSignature{IsEcdsa: true, AsEcdsa: types.NewBytes(sig)}
func signECDSA(data []byte, privateKeyURI string) ([]byte, error) {
	// if data is longer than 256 bytes, hash it first
	if len(data) > 256 {
		h := blake2b.Sum256(data)
		data = h[:]
	}
	scheme := ecdsa.Scheme{}
	kyr, err := subkey.DeriveKeyPair(scheme, privateKeyURI)
	if err != nil {
		return nil, err
	}

	signature, err := kyr.Sign(data)
	if err != nil {
		return nil, err
	}
	return signature, nil
}

litterGuy avatar Apr 01 '21 02:04 litterGuy

And in the ecdsa,the public key is 33 byte, but 'MultiAddress'

type MultiAddress struct {
	AccountId AccountID
	Index     UCompact
	Raw       Bytes
	Address32 H256
	Address20 H160
	types     int
}

accountid is 32 byte. I think it was caused by an error in the public key transmission,but i can`t fix this by change MultiAddress.

code : sign_ecdsa.go

plz help

litterGuy avatar Apr 06 '21 06:04 litterGuy

@litterGuy have you tried the RawBytes?

vedhavyas avatar Apr 15 '21 08:04 vedhavyas

@litterGuy have you tried the RawBytes?

Also tried. Set RawBytes as public key ( 33 byte ), and result the error is

{"Code":1011,"Message":"Unknown Transaction Validity","Data":"CannotLookup"}

litterGuy avatar Apr 15 '21 09:04 litterGuy

@litterGuy i just looked at the substrate code. looks like you have to do a blake2b hash of compressed 33 byte pub key to get 32 byte key which will be the public key - sp_io::hashing::blake2_256(&who.as_ref()[..]).into() https://github.com/paritytech/substrate/blob/master/primitives/runtime/src/lib.rs#L292.

You can give it a shot if you want

vedhavyas avatar Apr 15 '21 09:04 vedhavyas

Thanks for the help, i will try later

litterGuy avatar Apr 16 '21 00:04 litterGuy

accountId:= blake2b.Sum256(kr.Public())

and set accountId(32 byte) to raw, also get the same error is :

{"Code":1011,"Message":"Unknown Transaction Validity","Data":"CannotLookup"}

when set "AsID" = accountId , the error is 'Transaction has a bad signature'.

are there any other suggestions?

litterGuy avatar Apr 16 '21 08:04 litterGuy

This issue seems stale, closing it for now. Please let us know if you still need help.

cdamian avatar Dec 05 '23 11:12 cdamian