-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Arweave wallet JSON file to gitignore
Here is a summary of the changes in the commit: - Update the Arweave and Bitcoin scripts with new features and optimizations - Implement private key generation and wallet creation - Add commands for generating a private key, showing balance, and creating a wallet - Update dependencies and import packages for Arweave and Bitcoin scripts
- Loading branch information
Showing
10 changed files
with
1,016 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
gcmd "github.com/Laisky/go-utils/v4/cmd" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCMD.PersistentFlags().Bool("debug", false, "run in debug mode") | ||
} | ||
|
||
var rootCMD = &cobra.Command{ | ||
Use: "", | ||
Short: "scripts", | ||
Args: gcmd.NoExtraArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
}, | ||
} | ||
|
||
func main() { | ||
rootCMD.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/sha256" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"encoding/json" | ||
"encoding/pem" | ||
"fmt" | ||
"math/big" | ||
"os" | ||
|
||
"github.com/Laisky/errors/v2" | ||
gcmd "github.com/Laisky/go-utils/v4/cmd" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ArweaveAPI = "https://arweave.net" | ||
|
||
func init() { | ||
genPrivkeyCMD.Flags().StringVarP(&genPrivkeyCMDArgs.keyfilePath, "keyfile", "k", "wallet.json", "path to save the generated wallet") | ||
rootCMD.AddCommand(genPrivkeyCMD) | ||
} | ||
|
||
var genPrivkeyCMDArgs = struct { | ||
keyfilePath string | ||
}{} | ||
|
||
var genPrivkeyCMD = &cobra.Command{ | ||
Use: "gen", | ||
Short: "Generate privkey and address", | ||
Args: gcmd.NoExtraArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
_, address, err := genPrivkeyAndAddress(genPrivkeyCMDArgs.keyfilePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("write wallet to %s, address: %s\n", genPrivkeyCMDArgs.keyfilePath, address) | ||
return nil | ||
}, | ||
} | ||
|
||
// Wallet arweave wallet | ||
type Wallet struct { | ||
Kty string `json:"kty"` | ||
N string `json:"n"` | ||
E string `json:"e"` | ||
D string `json:"d"` | ||
P string `json:"p"` | ||
Q string `json:"q"` | ||
Dp string `json:"dp"` | ||
Dq string `json:"dq"` | ||
Qi string `json:"qi"` | ||
} | ||
|
||
func generateKey() (*rsa.PrivateKey, error) { | ||
return rsa.GenerateKey(rand.Reader, 2048) | ||
} | ||
|
||
func exportRsaPrivateKeyAsPemStr(privkey *rsa.PrivateKey) string { | ||
privkeyBytes := x509.MarshalPKCS1PrivateKey(privkey) | ||
privkeyPem := &pem.Block{ | ||
Type: "RSA PRIVATE KEY", | ||
Bytes: privkeyBytes, | ||
} | ||
return string(pem.EncodeToMemory(privkeyPem)) | ||
} | ||
|
||
func toBase64URL(b []byte) string { | ||
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(b) | ||
} | ||
|
||
func createWallet(priv *rsa.PrivateKey) *Wallet { | ||
pub := priv.PublicKey | ||
e := big.NewInt(int64(pub.E)) | ||
|
||
w := &Wallet{ | ||
Kty: "RSA", | ||
N: toBase64URL(pub.N.Bytes()), | ||
E: toBase64URL(e.Bytes()), | ||
D: toBase64URL(priv.D.Bytes()), | ||
P: toBase64URL(priv.Primes[0].Bytes()), | ||
Q: toBase64URL(priv.Primes[1].Bytes()), | ||
Dp: toBase64URL(priv.Precomputed.Dp.Bytes()), | ||
Dq: toBase64URL(priv.Precomputed.Dq.Bytes()), | ||
Qi: toBase64URL(priv.Precomputed.Qinv.Bytes()), | ||
} | ||
|
||
return w | ||
} | ||
|
||
func genPrivkeyAndAddress(keyfilePath string) (*Wallet, string, error) { | ||
priv, err := generateKey() | ||
if err != nil { | ||
return nil, "", errors.Wrap(err, "generate key") | ||
} | ||
|
||
wallet := createWallet(priv) | ||
walletJson, err := json.MarshalIndent(wallet, "", " ") | ||
if err != nil { | ||
return nil, "", errors.Wrap(err, "marshal wallet") | ||
} | ||
|
||
err = os.WriteFile(keyfilePath, walletJson, 0644) | ||
if err != nil { | ||
return nil, "", errors.Wrap(err, "write wallet") | ||
} | ||
|
||
address := getWalletAddress(wallet) | ||
return wallet, address, nil | ||
} | ||
|
||
// getWalletAddress returns the wallet address | ||
func getWalletAddress(wallet *Wallet) string { | ||
nBytes, err := base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(wallet.N) | ||
if err != nil { | ||
panic("invalid base64 string for N") | ||
} | ||
hash := sha256.Sum256(nBytes) | ||
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(hash[:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/everFinance/goar" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGenPrivkeyAndAddress(t *testing.T) { | ||
// Create a temporary file for the wallet JSON | ||
tmpfile, err := os.CreateTemp("", "wallet.json") | ||
require.NoError(t, err) | ||
defer os.Remove(tmpfile.Name()) | ||
|
||
// Generate a new private key | ||
_, _, err = genPrivkeyAndAddress(tmpfile.Name()) | ||
require.NoError(t, err) | ||
|
||
wallet, err := goar.NewWalletFromPath(tmpfile.Name(), ArweaveAPI) | ||
require.NoError(t, err) | ||
|
||
bal, err := showBalance(wallet.Owner()) | ||
require.NoError(t, err) | ||
t.Logf("balance: %s", bal.String()) | ||
|
||
// t.Error() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
module scripts | ||
|
||
go 1.22.0 | ||
|
||
require ( | ||
github.com/Laisky/errors/v2 v2.0.1 | ||
github.com/Laisky/go-utils/v4 v4.9.1 | ||
github.com/everFinance/goar v1.6.3 | ||
github.com/spf13/cobra v1.8.0 | ||
github.com/stretchr/testify v1.8.4 | ||
) | ||
|
||
require ( | ||
filippo.io/edwards25519 v1.1.0 // indirect | ||
github.com/GoWebProd/gip v0.0.0-20230623090727-b60d41d5d320 // indirect | ||
github.com/GoWebProd/uuid7 v0.0.0-20231130161441-17ee54b097d4 // indirect | ||
github.com/Laisky/fast-skiplist/v2 v2.0.1 // indirect | ||
github.com/Laisky/go-chaining v0.0.0-20180507092046-43dcdc5a21be // indirect | ||
github.com/Laisky/graphql v1.0.6 // indirect | ||
github.com/Laisky/zap v1.27.0 // indirect | ||
github.com/bits-and-blooms/bitset v1.13.0 // indirect | ||
github.com/btcsuite/btcd/btcec/v2 v2.3.3 // indirect | ||
github.com/btcsuite/btcd/btcutil v1.1.5 // indirect | ||
github.com/cespare/xxhash v1.1.0 // indirect | ||
github.com/consensys/bavard v0.1.13 // indirect | ||
github.com/consensys/gnark-crypto v0.12.1 // indirect | ||
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect | ||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect | ||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect | ||
github.com/ethereum/c-kzg-4844 v1.0.2 // indirect | ||
github.com/ethereum/go-ethereum v1.14.3 // indirect | ||
github.com/everFinance/arseeding v1.2.5 // indirect | ||
github.com/everFinance/ethrpc v1.0.4 // indirect | ||
github.com/everFinance/goether v1.1.9 // indirect | ||
github.com/everFinance/gojwk v1.0.0 // indirect | ||
github.com/everFinance/ttcrsa v1.1.3 // indirect | ||
github.com/fsnotify/fsnotify v1.7.0 // indirect | ||
github.com/gammazero/deque v0.2.1 // indirect | ||
github.com/go-sql-driver/mysql v1.8.1 // indirect | ||
github.com/go-stack/stack v1.8.1 // indirect | ||
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect | ||
github.com/google/go-cpy v0.0.0-20211218193943-a9c933c06932 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/hamba/avro v1.8.0 // indirect | ||
github.com/holiman/uint256 v1.2.4 // indirect | ||
github.com/inconshreveable/log15 v2.16.0+incompatible // indirect | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.5 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/linkedin/goavro/v2 v2.13.0 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/mmcloughlin/addchain v0.4.0 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/monnand/dhkx v0.0.0-20180522003156-9e5b033f1ac4 // indirect | ||
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect | ||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect | ||
github.com/panjf2000/ants/v2 v2.9.1 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect | ||
github.com/rivo/duplo v0.0.0-20220703183130-751e882e6b83 // indirect | ||
github.com/shopspring/decimal v1.4.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/supranational/blst v0.3.11 // indirect | ||
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a // indirect | ||
github.com/tidwall/gjson v1.17.1 // indirect | ||
github.com/tidwall/match v1.1.1 // indirect | ||
github.com/tidwall/pretty v1.2.1 // indirect | ||
github.com/xlzd/gotp v0.1.0 // indirect | ||
go.dedis.ch/kyber/v3 v3.1.0 // indirect | ||
go.uber.org/automaxprocs v1.5.3 // indirect | ||
go.uber.org/multierr v1.11.0 // indirect | ||
golang.org/x/crypto v0.23.0 // indirect | ||
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect | ||
golang.org/x/net v0.25.0 // indirect | ||
golang.org/x/sync v0.7.0 // indirect | ||
golang.org/x/sys v0.20.0 // indirect | ||
golang.org/x/term v0.20.0 // indirect | ||
golang.org/x/tools v0.21.0 // indirect | ||
gopkg.in/h2non/gentleman.v2 v2.0.5 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
gorm.io/datatypes v1.2.0 // indirect | ||
gorm.io/driver/mysql v1.5.6 // indirect | ||
gorm.io/gorm v1.25.10 // indirect | ||
rsc.io/tmplfunc v0.0.3 // indirect | ||
) |
Oops, something went wrong.