Skip to content

Commit 396a25b

Browse files
authored
feat: added cmd to encrypt tss keyshare file (#1744)
* added cmd to encrypt tss keyshare file, allowing empty tss password for backward compatibility. * add changelog + make generate * update go-tss version * use positional args instead * remove unnecessary struct * fix gosec issue
1 parent eb791fb commit 396a25b

File tree

5 files changed

+74
-12
lines changed

5 files changed

+74
-12
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [1712](https://github.com/zeta-chain/node/issues/1712) - increase EVM outtx inclusion timeout to 20 minutes
1919
* [1733](https://github.com/zeta-chain/node/pull/1733)) - remove the unnecessary 2x multiplier in the convertGasToZeta RPC
2020
* [1721](https://github.com/zeta-chain/node/issues/1721) - zetaclient should provide bitcoin_chain_id when querying TSS address
21+
* [1744](https://github.com/zeta-chain/node/pull/1744) - added cmd to encrypt tss keyshare file, allowing empty tss password for backward compatibility.
2122

2223
### Tests
2324

cmd/zetaclientd/encrypt_tss.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"crypto/aes"
5+
"crypto/cipher"
6+
"crypto/rand"
7+
"crypto/sha256"
8+
"encoding/json"
9+
"errors"
10+
"io"
11+
"os"
12+
"path/filepath"
13+
14+
"github.com/spf13/cobra"
15+
)
16+
17+
var encTssCmd = &cobra.Command{
18+
Use: "tss-encrypt [file-path] [secret-key]",
19+
Short: "Utility command to encrypt existing tss key-share file",
20+
Args: cobra.ExactArgs(2),
21+
RunE: EncryptTSSFile,
22+
}
23+
24+
func init() {
25+
RootCmd.AddCommand(encTssCmd)
26+
}
27+
28+
func EncryptTSSFile(_ *cobra.Command, args []string) error {
29+
filePath := args[0]
30+
secretKey := args[1]
31+
32+
filePath = filepath.Clean(filePath)
33+
data, err := os.ReadFile(filePath)
34+
if err != nil {
35+
return err
36+
}
37+
38+
if !json.Valid(data) {
39+
return errors.New("file does not contain valid json, may already be encrypted")
40+
}
41+
42+
block, err := aes.NewCipher(getFragmentSeed(secretKey))
43+
if err != nil {
44+
return err
45+
}
46+
47+
// Creating GCM mode
48+
gcm, err := cipher.NewGCM(block)
49+
if err != nil {
50+
return err
51+
}
52+
// Generating random nonce
53+
nonce := make([]byte, gcm.NonceSize())
54+
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
55+
return err
56+
}
57+
58+
cipherText := gcm.Seal(nonce, nonce, data, nil)
59+
return os.WriteFile(filePath, cipherText, 0o600)
60+
}
61+
62+
func getFragmentSeed(password string) []byte {
63+
h := sha256.New()
64+
h.Write([]byte(password))
65+
seed := h.Sum(nil)
66+
return seed
67+
}

cmd/zetaclientd/start.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,9 @@ func promptPasswords() (string, string, error) {
333333
return "", "", err
334334
}
335335

336-
if TSSKeyPass == "" {
337-
return "", "", errors.New("tss password is required to start zetaclient")
338-
}
336+
//trim delimiters
337+
hotKeyPass = strings.TrimSuffix(hotKeyPass, "\n")
338+
TSSKeyPass = strings.TrimSuffix(TSSKeyPass, "\n")
339339

340340
return hotKeyPass, TSSKeyPass, err
341341
}

go.mod

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ require (
4242
github.com/pkg/errors v0.9.1
4343
github.com/rakyll/statik v0.1.7
4444
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15
45-
github.com/zeta-chain/go-tss v0.1.1-0.20240103170132-35850edf5dbd
45+
github.com/zeta-chain/go-tss v0.1.1-0.20240208222330-f3be0d4a0d98
4646
github.com/zeta-chain/keystone/keys v0.0.0-20231105174229-903bc9405da2
4747
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20230816152528-db7d2bf9144b
4848
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc
@@ -338,8 +338,6 @@ replace (
338338
// use cometbft
339339
github.com/tendermint/tendermint => github.com/cometbft/cometbft v0.34.28
340340
github.com/tendermint/tm-db => github.com/BlockPILabs/cosmos-db v0.0.3
341-
github.com/zeta-chain/go-tss => github.com/zeta-chain/go-tss v0.1.1-0.20240115203400-a5b80e5da933
342-
343341
)
344342

345343
replace github.com/cometbft/cometbft-db => github.com/notional-labs/cometbft-db v0.0.0-20230321185329-6dc7c0ca6345

go.sum

+2-6
Original file line numberDiff line numberDiff line change
@@ -1848,7 +1848,6 @@ github.com/ipfs/go-datastore v0.6.0/go.mod h1:rt5M3nNbSO/8q1t4LNkLyUwRs8HupMeN/8
18481848
github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk=
18491849
github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps=
18501850
github.com/ipfs/go-ipfs-util v0.0.2 h1:59Sswnk1MFaiq+VcaknX7aYEyGyGDAA73ilhEK2POp8=
1851-
github.com/ipfs/go-ipfs-util v0.0.2/go.mod h1:CbPtkWJzjLdEcezDns2XYaehFVNXG9zrdrtMecczcsQ=
18521851
github.com/ipfs/go-log v1.0.5 h1:2dOuUCB1Z7uoczMWgAyDck5JLb72zHzrMnGnCNNbvY8=
18531852
github.com/ipfs/go-log v1.0.5/go.mod h1:j0b8ZoR+7+R99LD9jZ6+AJsrzkPbSXbZfGakb5JPtIo=
18541853
github.com/ipfs/go-log/v2 v2.1.3/go.mod h1:/8d0SH3Su5Ooc31QlL1WysJhvyOTDCjcCZ9Axpmri6g=
@@ -2761,7 +2760,6 @@ github.com/sourcegraph/go-diff v0.5.1/go.mod h1:j2dHj3m8aZgQO8lMTcTnBcXkRRRqi34c
27612760
github.com/sourcegraph/go-diff v0.5.3/go.mod h1:v9JDtjCE4HHHCZGId75rg8gkKKa98RVjBcBGsVmMmak=
27622761
github.com/sourcegraph/go-diff v0.6.1/go.mod h1:iBszgVvyxdc8SFZ7gm69go2KDdt3ag071iBaWPF6cjs=
27632762
github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA=
2764-
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc=
27652763
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
27662764
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
27672765
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
@@ -3035,10 +3033,8 @@ github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQ
30353033
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=
30363034
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA=
30373035
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg=
3038-
github.com/zeta-chain/go-tss v0.1.1-0.20240103170132-35850edf5dbd h1:wv+VGLFX8IhPuoqAVQGAQjlEPWqYjowJgJVNReolJTM=
3039-
github.com/zeta-chain/go-tss v0.1.1-0.20240103170132-35850edf5dbd/go.mod h1:+lJfk/qqt+oxXeVuJV+PzpUoxftUfoTRf2eF3qlbyFI=
3040-
github.com/zeta-chain/go-tss v0.1.1-0.20240115203400-a5b80e5da933 h1:cx6ZXVmV9LpkYRQER7+sTgu56wdmaU1U5VJcx3rsCwc=
3041-
github.com/zeta-chain/go-tss v0.1.1-0.20240115203400-a5b80e5da933/go.mod h1:+lJfk/qqt+oxXeVuJV+PzpUoxftUfoTRf2eF3qlbyFI=
3036+
github.com/zeta-chain/go-tss v0.1.1-0.20240208222330-f3be0d4a0d98 h1:GCSRgszQbAR7h/qK0YKjlm1mcnZOaGMbztRLaAfoOx0=
3037+
github.com/zeta-chain/go-tss v0.1.1-0.20240208222330-f3be0d4a0d98/go.mod h1:+lJfk/qqt+oxXeVuJV+PzpUoxftUfoTRf2eF3qlbyFI=
30423038
github.com/zeta-chain/keystone/keys v0.0.0-20231105174229-903bc9405da2 h1:gd2uE0X+ZbdFJ8DubxNqLbOVlCB12EgWdzSNRAR82tM=
30433039
github.com/zeta-chain/keystone/keys v0.0.0-20231105174229-903bc9405da2/go.mod h1:x7Bkwbzt2W2lQfjOirnff0Dj+tykdbTG1FMJPVPZsvE=
30443040
github.com/zeta-chain/protocol-contracts v1.0.2-athens3.0.20230816152528-db7d2bf9144b h1:aZRt5BtXdoDdyrUKwcv3B7mS30m/B854cjKjmnXBE5A=

0 commit comments

Comments
 (0)