Some checks are pending
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
187 lines
5.1 KiB
Go
187 lines
5.1 KiB
Go
package legacytx
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"sigs.k8s.io/yaml"
|
|
|
|
"cosmossdk.io/errors"
|
|
|
|
"git.cw.tr/mukan-network/mukan-sdk/codec"
|
|
"git.cw.tr/mukan-network/mukan-sdk/codec/legacy"
|
|
codectypes "git.cw.tr/mukan-network/mukan-sdk/codec/types"
|
|
cryptotypes "git.cw.tr/mukan-network/mukan-sdk/crypto/types"
|
|
"git.cw.tr/mukan-network/mukan-sdk/crypto/types/multisig"
|
|
sdk "git.cw.tr/mukan-network/mukan-sdk/types"
|
|
"git.cw.tr/mukan-network/mukan-sdk/types/tx/signing"
|
|
)
|
|
|
|
// LegacyMsg defines the old interface a message must fulfill,
|
|
// containing Amino signing method.
|
|
//
|
|
// Deprecated: Please use `Msg` instead.
|
|
type LegacyMsg interface {
|
|
sdk.Msg
|
|
|
|
// Get the canonical byte representation of the Msg.
|
|
GetSignBytes() []byte
|
|
}
|
|
|
|
// StdSignDoc is replay-prevention structure.
|
|
// It includes the result of msg.GetSignBytes(),
|
|
// as well as the ChainID (prevent cross chain replay)
|
|
// and the Sequence numbers for each signature (prevent
|
|
// inchain replay and enforce tx ordering per account).
|
|
type StdSignDoc struct {
|
|
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
|
|
Sequence uint64 `json:"sequence" yaml:"sequence"`
|
|
TimeoutHeight uint64 `json:"timeout_height,omitempty" yaml:"timeout_height"`
|
|
ChainID string `json:"chain_id" yaml:"chain_id"`
|
|
Memo string `json:"memo" yaml:"memo"`
|
|
Fee json.RawMessage `json:"fee" yaml:"fee"`
|
|
Msgs []json.RawMessage `json:"msgs" yaml:"msgs"`
|
|
}
|
|
|
|
var RegressionTestingAminoCodec *codec.LegacyAmino
|
|
|
|
// Deprecated: please delete this code eventually.
|
|
func mustSortJSON(bz []byte) []byte {
|
|
var c any
|
|
err := json.Unmarshal(bz, &c)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
js, err := json.Marshal(c)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return js
|
|
}
|
|
|
|
// StdSignBytes returns the bytes to sign for a transaction.
|
|
//
|
|
// Deprecated: Please use x/tx/signing/aminojson instead.
|
|
func StdSignBytes(chainID string, accnum, sequence, timeout uint64, fee StdFee, msgs []sdk.Msg, memo string) []byte {
|
|
if RegressionTestingAminoCodec == nil {
|
|
panic(fmt.Errorf("must set RegressionTestingAminoCodec before calling StdSignBytes"))
|
|
}
|
|
msgsBytes := make([]json.RawMessage, 0, len(msgs))
|
|
for _, msg := range msgs {
|
|
bz := RegressionTestingAminoCodec.MustMarshalJSON(msg)
|
|
msgsBytes = append(msgsBytes, mustSortJSON(bz))
|
|
}
|
|
|
|
bz, err := legacy.Cdc.MarshalJSON(StdSignDoc{
|
|
AccountNumber: accnum,
|
|
ChainID: chainID,
|
|
Fee: json.RawMessage(fee.Bytes()),
|
|
Memo: memo,
|
|
Msgs: msgsBytes,
|
|
Sequence: sequence,
|
|
TimeoutHeight: timeout,
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return mustSortJSON(bz)
|
|
}
|
|
|
|
// Deprecated: StdSignature represents a sig
|
|
type StdSignature struct {
|
|
cryptotypes.PubKey `json:"pub_key" yaml:"pub_key"` // optional
|
|
Signature []byte `json:"signature" yaml:"signature"`
|
|
}
|
|
|
|
// Deprecated
|
|
func NewStdSignature(pk cryptotypes.PubKey, sig []byte) StdSignature {
|
|
return StdSignature{PubKey: pk, Signature: sig}
|
|
}
|
|
|
|
// GetSignature returns the raw signature bytes.
|
|
func (ss StdSignature) GetSignature() []byte {
|
|
return ss.Signature
|
|
}
|
|
|
|
// GetPubKey returns the public key of a signature as a cryptotypes.PubKey using the
|
|
// Amino codec.
|
|
func (ss StdSignature) GetPubKey() cryptotypes.PubKey {
|
|
return ss.PubKey
|
|
}
|
|
|
|
// MarshalYAML returns the YAML representation of the signature.
|
|
func (ss StdSignature) MarshalYAML() (any, error) {
|
|
pk := ""
|
|
if ss.PubKey != nil {
|
|
pk = ss.String()
|
|
}
|
|
|
|
bz, err := yaml.Marshal(struct {
|
|
PubKey string `json:"pub_key"`
|
|
Signature string `json:"signature"`
|
|
}{
|
|
pk,
|
|
fmt.Sprintf("%X", ss.Signature),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return string(bz), nil
|
|
}
|
|
|
|
func (ss StdSignature) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
|
|
return codectypes.UnpackInterfaces(ss.PubKey, unpacker)
|
|
}
|
|
|
|
// StdSignatureToSignatureV2 converts a StdSignature to a SignatureV2
|
|
func StdSignatureToSignatureV2(cdc *codec.LegacyAmino, sig StdSignature) (signing.SignatureV2, error) {
|
|
pk := sig.GetPubKey()
|
|
data, err := pubKeySigToSigData(cdc, pk, sig.Signature)
|
|
if err != nil {
|
|
return signing.SignatureV2{}, err
|
|
}
|
|
|
|
return signing.SignatureV2{
|
|
PubKey: pk,
|
|
Data: data,
|
|
}, nil
|
|
}
|
|
|
|
func pubKeySigToSigData(cdc *codec.LegacyAmino, key cryptotypes.PubKey, sig []byte) (signing.SignatureData, error) {
|
|
multiPK, ok := key.(multisig.PubKey)
|
|
if !ok {
|
|
return &signing.SingleSignatureData{
|
|
SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON,
|
|
Signature: sig,
|
|
}, nil
|
|
}
|
|
var multiSig multisig.AminoMultisignature
|
|
err := cdc.Unmarshal(sig, &multiSig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sigs := multiSig.Sigs
|
|
sigDatas := make([]signing.SignatureData, len(sigs))
|
|
pubKeys := multiPK.GetPubKeys()
|
|
bitArray := multiSig.BitArray
|
|
n := multiSig.BitArray.Count()
|
|
signatures := multisig.NewMultisig(n)
|
|
sigIdx := 0
|
|
for i := range n {
|
|
if bitArray.GetIndex(i) {
|
|
data, err := pubKeySigToSigData(cdc, pubKeys[i], multiSig.Sigs[sigIdx])
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "Unable to convert Signature to SigData %d", sigIdx)
|
|
}
|
|
|
|
sigDatas[sigIdx] = data
|
|
multisig.AddSignature(signatures, data, sigIdx)
|
|
sigIdx++
|
|
}
|
|
}
|
|
|
|
return signatures, nil
|
|
}
|