-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
175 lines (138 loc) · 4.98 KB
/
main.go
File metadata and controls
175 lines (138 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"log"
routerTypes "github.com/0xElder/elder/x/router/types"
"github.com/cosmos/btcutil/bech32"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/gogoproto/proto"
"github.com/ethereum/go-ethereum/crypto"
"golang.org/x/crypto/ripemd160"
)
// {
// "rollId": "1",
// "txs": {
// "txs": [
// "Cn8KfQodL2VsZGVyLnJvdXRlci5Nc2dTdWJtaXRSb2xsVHgSXAotY29zbW9zMXYzcnQ5NTN5cHpuY2hwazBnemRsYzJ4cDJ0d20zbnVwemphNDMzEAEYoMIeIiXkgIUCVAvkAIJSCJTt8GGZ+NofLieiFfliULZnm6DZvWSAgICAElgKUApGCh8vY29zbW9zLmNyeXB0by5zZWNwMjU2azEuUHViS2V5EiMKIQMRo66jA2y5C4wS1V+Ko1vDHJ3YSrk/TF+GgwqikeGTUBIECgIIAhgDEgQQwJoMGkD5htuz/l4yPnMVHKBKaB9Bb0r5ovb12CCk1upOPX1wYAJRz1otuJPYVajL3GPbdIQfMRcPBTm0ccLE2+7uZHQU"
// ],
// "block": "30"
// }
// }
type GetRollTxsResponse struct {
RollID string `json:"roll_id"`
Txs TxList `json:"txs"`
}
type TxList []string
func EthPubKeyToEthAddr(pubKey string) string {
// Decode the public key from hex
pubKeyBytes, err := hex.DecodeString(pubKey)
if err != nil {
log.Fatalf("Failed to decode hex string: %v", err)
}
// Generate the public key object
publicKey, err := crypto.UnmarshalPubkey(pubKeyBytes)
if err != nil {
log.Fatalf("Failed to generate public key: %v", err)
}
// Generate the address
address := crypto.PubkeyToAddress(*publicKey)
return address.Hex()
}
func CosmosPubKeyToEthPubkey(pubKey string) (string, error) {
// Decode the public key from hex
pubKeyBytes, err := hex.DecodeString(pubKey)
if err != nil {
log.Fatalf("Failed to decode hex string: %v", err)
}
fmt.Println("pubKeyBytes", len(pubKeyBytes))
// Generate the public key object
publicKey, err := crypto.DecompressPubkey(pubKeyBytes)
if err != nil {
log.Fatalf("Failed to decompress public key: %v", err)
}
return hex.EncodeToString(crypto.FromECDSAPub(publicKey)), nil
}
func bytesToCosmosTx(rawBytes string) (*tx.Tx, error) {
txBytes, err := base64.StdEncoding.DecodeString(rawBytes)
if err != nil {
return nil, err
}
var tx tx.Tx
err = tx.Unmarshal(txBytes)
if err != nil {
return nil, err
}
return &tx, nil
}
// PublicKeyToAddress converts secp256k1 public key to a bech32 Tendermint/Cosmos based address
func CosmosPublicKeyToCosmosAddress(addressPrefix, publicKeyString string) string {
// Decode public key string
pubKeyBytes, err := hex.DecodeString(publicKeyString)
if err != nil {
log.Fatalf("Failed to decode public key hex: %v", err)
}
// Hash pubKeyBytes as: RIPEMD160(SHA256(public_key_bytes))
pubKeySha256Hash := sha256.Sum256(pubKeyBytes)
ripemd160hash := ripemd160.New()
ripemd160hash.Write(pubKeySha256Hash[:])
addressBytes := ripemd160hash.Sum(nil)
// Convert addressBytes into a bech32 string
address := toBech32(addressPrefix, addressBytes)
return address
}
// Code courtesy: https://github.com/cosmos/cosmos-sdk/blob/90c9c9a9eb4676d05d3f4b89d9a907bd3db8194f/types/bech32/bech32.go#L10
func toBech32(addrPrefix string, addrBytes []byte) string {
converted, err := bech32.ConvertBits(addrBytes, 8, 5, true)
if err != nil {
panic(err)
}
addr, err := bech32.Encode(addrPrefix, converted)
if err != nil {
panic(err)
}
return addr
}
func main() {
rollTxResp := &GetRollTxsResponse{
RollID: "1",
Txs: TxList{
"Cn8KfQodL2VsZGVyLnJvdXRlci5Nc2dTdWJtaXRSb2xsVHgSXAotY29zbW9zMXYzcnQ5NTN5cHpuY2hwazBnemRsYzJ4cDJ0d20zbnVwemphNDMzEAEYoMIeIiXkgIUCVAvkAIJSCJTt8GGZ+NofLieiFfliULZnm6DZvWSAgICAElgKUApGCh8vY29zbW9zLmNyeXB0by5zZWNwMjU2azEuUHViS2V5EiMKIQMRo66jA2y5C4wS1V+Ko1vDHJ3YSrk/TF+GgwqikeGTUBIECgIIAhgDEgQQwJoMGkD5htuz/l4yPnMVHKBKaB9Bb0r5ovb12CCk1upOPX1wYAJRz1otuJPYVajL3GPbdIQfMRcPBTm0ccLE2+7uZHQU",
},
}
for _, txBytes := range rollTxResp.Txs {
tx, err := bytesToCosmosTx(txBytes)
if err != nil {
panic(err)
}
signerCosmos := tx.GetAuthInfo()
fmt.Printf("\nSigner: %v\n", signerCosmos.SignerInfos[0].PublicKey)
cosmosPubKey := &secp256k1.PubKey{}
err = proto.Unmarshal(signerCosmos.SignerInfos[0].PublicKey.Value, cosmosPubKey)
if err != nil {
panic(err)
}
fmt.Printf("\nPubKey: %+v\n", cosmosPubKey.Key)
cosmosPubKeyStr := hex.EncodeToString(cosmosPubKey.Key)
fmt.Printf("\nPubKeyStr: %v\n", cosmosPubKeyStr)
cosmosAddr := CosmosPublicKeyToCosmosAddress("cosmos", cosmosPubKeyStr)
fmt.Printf("\nElderAddr: %v\n", cosmosAddr)
ethPubKey, err := CosmosPubKeyToEthPubkey(cosmosPubKeyStr)
if err != nil {
panic(err)
}
fmt.Printf("\nEthPubKey: %v\n", ethPubKey)
ethAddr := EthPubKeyToEthAddr(ethPubKey)
fmt.Printf("\nEthAddr: %v\n", ethAddr)
cosmMessage := &routerTypes.MsgSubmitRollTx{}
proto.Unmarshal(tx.Body.Messages[0].Value, cosmMessage)
sigs := base64.StdEncoding.EncodeToString(tx.Signatures[0])
// innerTxBytes := base64.StdEncoding.EncodeToString(cosmMessage.TxData)
fmt.Printf("\ntx.Signatures: %+v\n", sigs)
fmt.Printf("\nCosmosMessage: %+v\n", cosmMessage)
fmt.Printf("\nCosmosMessage.InnerTxData: %x\n", cosmMessage.TxData)
}
}