Skip to content

Commit

Permalink
added unit test for seed phrase related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhiminHeGit committed Aug 24, 2019
1 parent 96729a1 commit 28b4168
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 21 deletions.
50 changes: 50 additions & 0 deletions cmd/btfs/generatekey_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"testing"
)

// func generateKey(importKey string, keyType string, seedPhrase string, mnemonicLen int, mnemonic string ) error {
//record
//access
// aerobic
// glow
// retreat
// language
// distance
// stamp
// cattle
// arrive
// defy
// movie
// TSZKYA3bd4sJEmthPk1Z9hfD5ijpaDM1QE
// db6bf5da1c2225d76356df2f59ebd3207b91228bcf429f0c791f94e9095a1f8e

func TestSeedsPhrase(t *testing.T) {
const PRIVATE_KEY = "db6bf5da1c2225d76356df2f59ebd3207b91228bcf429f0c791f94e9095a1f8e"
finalImportKey, err := generateKey(PRIVATE_KEY, "",
"")
if err != nil || finalImportKey != PRIVATE_KEY {
t.Error("ImportKey generated not matching")
}

finalImportKey, err = generateKey("", "",
"record,access,aerobic,glow,retreat,language,distance,stamp,cattle,arrive,defy,movie")
if err != nil || finalImportKey != PRIVATE_KEY {
t.Error("Generating from seed phrases failed")
}

// one less word
finalImportKey, err = generateKey("", "",
"record,access,aerobic,glow,retreat,language,distance,stamp,cattle,arrive,defy")
if err.Error() != "The seed phrase required to generate TRON private key needs to contain 12 words. Provided mnemonic has 11 words." {
t.Error("Parameter check failed")
}
// word not in dictionary
finalImportKey, err = generateKey("", "",
"record,access,aerobic,glow,retreat,language,distance,stamp,cattle,arrive,defy,mov")
if err.Error() != "Entered seed phrase is not valid" {
t.Error("Parameter check failed")
}

}
52 changes: 31 additions & 21 deletions cmd/btfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,33 +129,43 @@ environment variable:
importKey, _ := req.Options[importKeyOptionName].(string)
keyType, _ := req.Options[keyTypeOptionName].(string)
seedPhrase, _ := req.Options[seedOptionName].(string)
mnemonicLen := len(strings.Split(seedPhrase, ","))
mnemonic := strings.ReplaceAll(seedPhrase, ",", " ")

if importKey != "" && keyType != "" {
return fmt.Errorf("cannot specify key type and import TRON private key at the same time")
} else if seedPhrase != "" {
if mnemonicLen != mnemonicLength {
return fmt.Errorf("The seed phrase required to generate TRON private key needs to contain 12 words. Provided mnemonic has %v words.", mnemonicLen)
}
if err := !bip39.IsMnemonicValid(mnemonic); err {
return fmt.Errorf("Entered seed phrase is not valid")
}
fmt.Println("Generating TRON key with BIP39 seed phrase...")
importKey = generatePrivKeyUsingBIP39(mnemonic)
}

if keyType == "" {
keyType = keyTypeDefault
} else if keyType == "BIP39" {
fmt.Println("Generating TRON key with BIP39 seed phrase...")
importKey = generatePrivKeyUsingBIP39("")
finalImportKey, error := generateKey(importKey, keyType, seedPhrase)
if error == nil {
return doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profiles, conf, keyType, finalImportKey, rmOnUnpin)
} else {
return error
}

return doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profiles, conf, keyType, importKey, rmOnUnpin)
},
}

func generateKey(importKey string, keyType string, seedPhrase string) (string, error) {
mnemonicLen := len(strings.Split(seedPhrase, ","))
mnemonic := strings.ReplaceAll(seedPhrase, ",", " ")

if importKey != "" && keyType != "" {
return "", fmt.Errorf("cannot specify key type and import TRON private key at the same time")
} else if seedPhrase != "" {
if mnemonicLen != mnemonicLength {
return "", fmt.Errorf("The seed phrase required to generate TRON private key needs to contain 12 words. Provided mnemonic has %v words.", mnemonicLen)
}
if err := !bip39.IsMnemonicValid(mnemonic); err {
return "", fmt.Errorf("Entered seed phrase is not valid")
}
fmt.Println("Generating TRON key with BIP39 seed phrase...")
importKey = generatePrivKeyUsingBIP39(mnemonic)
}

if keyType == "" {
keyType = keyTypeDefault
} else if keyType == "BIP39" {
fmt.Println("Generating TRON key with BIP39 seed phrase...")
importKey = generatePrivKeyUsingBIP39("")
}
return importKey, nil
}

var errRepoExists = errors.New(`btfs configuration file already exists!
Reinitializing would overwrite your keys.
`)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/ipfs/go-cid v0.0.2
github.com/ipfs/go-cidutil v0.0.2
github.com/ipfs/go-datastore v0.0.5
github.com/ipfs/go-detect-race v0.0.1
github.com/ipfs/go-ds-badger v0.0.5
github.com/ipfs/go-ds-flatfs v0.0.2
github.com/ipfs/go-ds-leveldb v0.0.2
Expand Down Expand Up @@ -62,6 +63,7 @@ require (
github.com/ipfs/interface-go-ipfs-core v0.1.0
github.com/ipfs/iptb v1.4.0
github.com/ipfs/iptb-plugins v0.1.0
github.com/jbenet/go-fuse-version v0.0.0-20160322195114-6d4c97bcf253 // indirect
github.com/jbenet/go-is-domain v1.0.2
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
github.com/jbenet/go-random-files v0.0.0-20190219210431-31b3f20ebded
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/TRON-US/go-btfs-cmds v0.1.2 h1:oAFOYHAfBlxPB+xiewDqOzbyPMmAbLQo63uu6g
github.com/TRON-US/go-btfs-cmds v0.1.2/go.mod h1:WxYDXVGY8q907Cz/45KsAwcBsAvkijclkIPbGyVpU9U=
github.com/TRON-US/go-btfs-config v0.1.2 h1:Ev2DEtZTl8TYJxNrPxf6jm5XYwDWd7R4+zyxh4DwP8g=
github.com/TRON-US/go-btfs-config v0.1.2/go.mod h1:GdZW7SmAY3rVqYwcbYkMmKo27IihzezwEQQMDTftlU0=
github.com/TRON-US/go-btfs-config v0.1.5 h1:H1+lAUw/CXs26TAf5oL3Kw00TYW549WJjD5wZBYwbyc=
github.com/TRON-US/go-btfs-config v0.1.5/go.mod h1:GdZW7SmAY3rVqYwcbYkMmKo27IihzezwEQQMDTftlU0=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down Expand Up @@ -344,6 +346,8 @@ github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+
github.com/jbenet/go-cienv v0.0.0-20150120210510-1bb1476777ec/go.mod h1:rGaEvXB4uRSZMmzKNLoXvTu1sfx+1kv/DojUlPrSZGs=
github.com/jbenet/go-cienv v0.1.0 h1:Vc/s0QbQtoxX8MwwSLWWh+xNNZvM3Lw7NsTcHrvvhMc=
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
github.com/jbenet/go-fuse-version v0.0.0-20160322195114-6d4c97bcf253 h1:+AUuGGAh/2X3wcomiZvjeTcx5OvGXsfdnIqk3KPM+HE=
github.com/jbenet/go-fuse-version v0.0.0-20160322195114-6d4c97bcf253/go.mod h1:gWtF+3u3zVe5/+I44niTEcU/KmVo2oMyLh0WhxpBT28=
github.com/jbenet/go-is-domain v1.0.2 h1:11r5MSptcNFZyBoqubBQnVMUKRWLuRjL1banaIk+iYo=
github.com/jbenet/go-is-domain v1.0.2/go.mod h1:xbRLRb0S7FgzDBTJlguhDVwLYM/5yNtvktxj2Ttfy7Q=
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c h1:uUx61FiAa1GI6ZmVd2wf2vULeQZIKG66eybjNXKYCz4=
Expand Down Expand Up @@ -735,6 +739,7 @@ github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFd
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e h1:T5PdfK/M1xyrHwynxMIVMWLS7f/qHwfslZphxtGnw7s=
github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e/go.mod h1:XDKHRm5ThF8YJjx001LtgelzsoaEcvnA7lVWz9EeX3g=
github.com/tron-us/go-btfs v0.4.20 h1:K6NoyTBw4FG3uX3sLvwX05owu1dhJJfTHe9M0OnP72k=
github.com/tyler-smith/go-bip32 v0.0.0-20170922074101-2c9cfd177564 h1:NXXyQVeRVLK8Xu27/hkkjwVOZLk5v4ZBEvvMtqMqznM=
github.com/tyler-smith/go-bip32 v0.0.0-20170922074101-2c9cfd177564/go.mod h1:0/YuQQF676+d4CMNclTqGUam1EDwz0B8o03K9pQqA3c=
github.com/tyler-smith/go-bip39 v1.0.0 h1:FOHg9gaQLeBBRbHE/QrTLfEiBHy5pQ/yXzf9JG5pYFM=
Expand Down

0 comments on commit 28b4168

Please sign in to comment.