Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zauth support (wasm/clis) #1460

Open
wants to merge 68 commits into
base: sprint-1.14
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
c62ca58
Update sprint 1.12 (#1341)
Hitenjain14 Dec 22, 2023
3a57362
fix trailing whitespace (#1343)
Hitenjain14 Dec 23, 2023
ad762e4
Merge staging changes (#1346)
peterlimg Dec 27, 2023
9b9fdd7
fix upload select (#1351)
Hitenjain14 Jan 4, 2024
f3ec2cc
fix workdir in mobile sdk (#1345)
Hitenjain14 Jan 4, 2024
5569b3d
set option to download to disk in wasm (#1348)
Hitenjain14 Jan 4, 2024
199dfd0
fix panic in hash chan (#1352)
Hitenjain14 Jan 4, 2024
2e55c3f
Fix merge conflict in sprint-1.12 (#1354)
Hitenjain14 Jan 4, 2024
4ed3f42
repair in batches (#1347)
Hitenjain14 Jan 4, 2024
edf037a
Merge staging (#1365)
Hitenjain14 Jan 12, 2024
e1a614f
Fix stake pool stats (#1356)
Jayashsatolia403 Jan 21, 2024
0056303
list pagination (#1368)
Hitenjain14 Jan 21, 2024
97b6a5b
Fix sync in windows (#1370)
Roshan-Mehta Jan 21, 2024
09c3757
Add option for mimeType (#1372)
Hitenjain14 Jan 21, 2024
9bc91c6
fix shutdown blobber (#1374)
boddumanohar Jan 21, 2024
6f48750
Merge remote-tracking branch 'origin/staging' into sprint-1.12
dabasov Jan 21, 2024
e7db14c
Cancel upload (#1332)
Hitenjain14 Jan 22, 2024
716e99b
Fix auth response on wasm (#1359)
peterlimg Jan 22, 2024
de8ef8c
Download buffer (#1363)
Hitenjain14 Jan 22, 2024
703cbd0
fix req complete cb (#1381)
Hitenjain14 Jan 29, 2024
312b5cf
get hardfork round by name (#1385)
Hitenjain14 Feb 2, 2024
5cae7a4
made GetFreeAllocationBlobbers public
dabasov Feb 2, 2024
e3d651b
Add miner/sharder stake and unstake support
peterlimg Feb 3, 2024
7deed8a
Add collect rewards for miner/sharder
peterlimg Feb 3, 2024
36e5b10
fixed resp format
dabasov Feb 3, 2024
ccda3cd
fixed resp format
dabasov Feb 3, 2024
b9512d3
Merge branch 'sprint-1.12' into feature/wasm-sc-txn
peterlimg Feb 4, 2024
004db9d
[wasm] Expose minersc - stakePool/unstakePool collect rewards (#1386)
peterlimg Feb 4, 2024
30b02db
Use zcncore transaction for storagesc to support 2fa
peterlimg Feb 5, 2024
86956f1
Remove duplicate Execute transaction in packages
peterlimg Feb 5, 2024
077bcf7
Expose claim rewards to wasm
peterlimg Feb 6, 2024
7e1e0a0
Fix collect rewards
peterlimg Feb 6, 2024
293689e
Merge branch 'staging' into sprint-1.12
dabasov Feb 8, 2024
c713e1b
Merge branch 'sprint-1.12' into feature/wasm-sc-txn
peterlimg Feb 9, 2024
9d83189
Fix transaction output
peterlimg Feb 9, 2024
0736880
Fix send token
peterlimg Feb 14, 2024
0c0f5e4
2FA file operation support
peterlimg Mar 2, 2024
9b0727d
Fix 2FA copy/rename operations
peterlimg Mar 11, 2024
67c92b1
Differ 2FA Sign and normal Sign
peterlimg Mar 12, 2024
ccc75a1
Add SplitKeysWallet to split keys and return wallet
peterlimg Apr 9, 2024
02dc138
Add methods to set split keys and persist
peterlimg Apr 9, 2024
928e2fe
Add isSplit field to wallet to indicate whether the wallet is split o…
peterlimg Apr 9, 2024
8d3abd1
Fix send with zauth
peterlimg Apr 9, 2024
2ab99c2
Add zauth functions
peterlimg Apr 11, 2024
7e4ef8e
Add zauth config for zboxcli
peterlimg Apr 12, 2024
cf1a8b6
Merge branch 'sprint-1.14' into feature/wasm-sc-txn
peterlimg Apr 12, 2024
f8dadd0
Resolve conflicts for setClientInfoWithSign
peterlimg Apr 12, 2024
3ebb76d
Merge branch 'zauth' into zauth-sc-zbox
peterlimg Apr 12, 2024
abc4aac
Update to support zauth server signing
peterlimg Apr 12, 2024
bab6137
Add zauth wasm
peterlimg Apr 16, 2024
67f00a8
Merge branch 'sprint-1.14' into zauth
peterlimg Apr 16, 2024
de58136
Update CallZauthSetup func
peterlimg Apr 25, 2024
b4c6cb1
Add wasm methods to support zvault and zauth
peterlimg Apr 26, 2024
91c0b7d
Merge branch 'sprint-1.14' into zauth
peterlimg Apr 26, 2024
66e47d0
Adjust zvault callback funcs
peterlimg Apr 27, 2024
6ad2c68
Update setWallet to introduce isSplit and peerPublicKey
peterlimg Apr 27, 2024
14206b9
Register zauth server properly
peterlimg Apr 27, 2024
3cbafb7
Init ZCNSDK with split option
peterlimg Apr 27, 2024
8256289
Clean up debug logs
peterlimg Apr 27, 2024
bb21816
Fix write marker signature verify
peterlimg Apr 28, 2024
e90ddfb
Merge branch 'sprint-1.14' into zauth
peterlimg May 8, 2024
8a35d81
Adjust wasm auth sign
peterlimg May 11, 2024
b59695a
Adjust wasm auth common sign
peterlimg May 12, 2024
9a9c771
ScryptEncrypt key does not have to be 32, it's the derivate key from …
peterlimg May 12, 2024
525999d
Add passphrase requirement for auth keys store and generate
peterlimg May 12, 2024
e044811
Add multiple users support zauth server
peterlimg May 12, 2024
50398c2
Fix wasm panic on auth error
peterlimg May 13, 2024
183e980
Fix invalid signature after updating allocation
peterlimg May 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions core/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ type Config struct {
// ZboxAppType app type name
ZboxAppType string `json:"zbox_app_type"`
// SharderConsensous is consensous for when quering for SCRestAPI calls
SharderConsensous int `json:"sharder_consensous"`
SharderConsensous int `json:"sharder_consensous"`
ZauthServer string `json:"zauth_server"`
V *viper.Viper `json:"-"`
}

// LoadConfigFile load and parse Config from file
Expand All @@ -99,7 +101,14 @@ func LoadConfigFile(file string) (Config, error) {
return cfg, thrown.Throw(ErrBadParsing, err.Error())
}

return LoadConfig(v)
cfg, err = LoadConfig(v)
if err != nil {
return cfg, err
}

cfg.V = v

return cfg, nil
}

// LoadConfig load and parse config
Expand Down Expand Up @@ -165,9 +174,9 @@ func LoadConfig(v Reader) (Config, error) {

cfg.SignatureScheme = v.GetString("signature_scheme")
cfg.ChainID = v.GetString("chain_id")
cfg.ZauthServer = v.GetString("zauth.server")

return cfg, nil

}

func isURL(s string) bool {
Expand Down
14 changes: 13 additions & 1 deletion core/sys/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var (
Sleep = time.Sleep

// Sign sign method. it should be initialized on different platform.
Sign SignFunc
Sign SignFunc
SignWithAuth SignFunc

// Verify verify method. it should be initialized on different platform.
Verify VerifyFunc
Expand All @@ -23,4 +24,15 @@ var (
VerifyWith VerifyWithFunc

Authorize AuthorizeFunc

AuthCommon AuthorizeFunc
)

// SetAuthorize sets the authorize callback function
func SetAuthorize(auth AuthorizeFunc) {
Authorize = auth
}

func SetAuthCommon(auth AuthorizeFunc) {
AuthCommon = auth
}
5 changes: 3 additions & 2 deletions core/transaction/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,15 @@ func NewTransactionReceipt(t *Transaction) *TxnReceipt {
return &TxnReceipt{Transaction: t}
}

func (t *Transaction) VerifyTransaction(verifyHandler VerifyFunc) (bool, error) {
// VerifySigWith verify the signature with the given public key and handler
func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bool, error) {
// Store the hash
hash := t.Hash
t.ComputeHashData()
if t.Hash != hash {
return false, errors.New("verify_transaction", fmt.Sprintf(`{"error":"hash_mismatch", "expected":"%v", "actual":%v"}`, t.Hash, hash))
}
return verifyHandler(t.PublicKey, t.Signature, t.Hash)
return verifyHandler(pubkey, t.Signature, t.Hash)
}

func SendTransactionSync(txn *Transaction, miners []string) error {
Expand Down
2 changes: 1 addition & 1 deletion core/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
//====== THIS IS AUTOGENERATED FILE. DO NOT MODIFY ========

package version
const VERSIONSTR = "v1.12.1-3-gbc68f654"
const VERSIONSTR = "v1.14.0-RC7-76-g8a35d81e"

7 changes: 6 additions & 1 deletion core/zcncrypto/bls0chain_herumi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ func TestCombinedSignAndVerify(t *testing.T) {
}

func TestSplitKey(t *testing.T) {
primaryKeyStr := `c36f2f92b673cf057a32e8bd0ca88888e7ace40337b737e9c7459fdc4c521918`
primaryKeyStr := `872eac6370c72093535fa395ad41a08ee90c9d0d46df9461eb2515451f389d1b`
// primaryKeyStr := `c36f2f92b673cf057a32e8bd0ca88888e7ace40337b737e9c7459fdc4c521918`
sig0 := NewSignatureScheme("bls0chain")
err := sig0.SetPrivateKey(primaryKeyStr)
if err != nil {
t.Fatalf("Set private key failed - %s", errors.Top(err))
}
data = "823bb3dc0b80a6c86922a884e63908cb9e963ef488688b41e32cbf4d84471a1f"
hash := Sha3Sum256(data)
signature, err := sig0.Sign(hash)
if err != nil {
Expand All @@ -170,15 +172,18 @@ func TestSplitKey(t *testing.T) {
for i := 0; i < numSplitKeys; i++ {
sigAggScheme[i] = NewSignatureScheme("bls0chain")
err = sigAggScheme[i].SetPrivateKey(w.Keys[i].PrivateKey)
fmt.Println("seckey:", sigAggScheme[i].GetPrivateKey())

require.NoError(t, err)
}
var aggrSig string
for i := 1; i < numSplitKeys; i++ {
tmpSig, _ := sigAggScheme[i].Sign(hash)
fmt.Println("tmpSig:", tmpSig)
aggrSig, _ = sigAggScheme[0].Add(tmpSig, hash)
}
if aggrSig != signature {
t.Fatalf("split key signature failed")
}
fmt.Println("aggrSig:", aggrSig)
}
34 changes: 27 additions & 7 deletions core/zcncrypto/signature_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package zcncrypto

import (
"encoding/json"
"fmt"
"os"

"github.com/0chain/errors"
"github.com/0chain/gosdk/core/encryption"
Expand All @@ -18,13 +20,15 @@ type KeyPair struct {

// Wallet structure
type Wallet struct {
ClientID string `json:"client_id"`
ClientKey string `json:"client_key"`
Keys []KeyPair `json:"keys"`
Mnemonic string `json:"mnemonics"`
Version string `json:"version"`
DateCreated string `json:"date_created"`
Nonce int64 `json:"nonce"`
ClientID string `json:"client_id"`
ClientKey string `json:"client_key"`
PeerPublicKey string `json:"peer_public_key"` // Peer public key exists only in split wallet
Keys []KeyPair `json:"keys"`
Mnemonic string `json:"mnemonics"`
Version string `json:"version"`
DateCreated string `json:"date_created"`
Nonce int64 `json:"nonce"`
IsSplit bool `json:"is_split"`
}

// SignatureScheme - an encryption scheme for signing and verifying messages
Expand Down Expand Up @@ -81,6 +85,22 @@ func (w *Wallet) Sign(hash, scheme string) (string, error) {
return sigScheme.Sign(hash)
}

// SetSplitKeys sets split keys and wipes out mnemonic and original primary keys
func (w *Wallet) SetSplitKeys(sw *Wallet) {
*w = *sw
}

func (w *Wallet) SaveTo(file string) error {
d, err := json.Marshal(w)
if err != nil {
return err
}

fmt.Println("Saving wallet to file: ", string(d))

return os.WriteFile(file, d, 0644)
}

func IsMnemonicValid(mnemonic string) bool {
return bip39.IsMnemonicValid(mnemonic)
}
Expand Down
5 changes: 5 additions & 0 deletions wasmsdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ func unlockStakePool(providerType, fee uint64, providerID string) (int64, error)
return unstake, err
}

func collectRewards(providerType int, providerID string) (string, error) {
hash, _, err := sdk.CollectRewards(providerID, sdk.ProviderType(providerType))
return hash, err
}

func getSkatePoolInfo(providerType int, providerID string) (*sdk.StakePoolInfo, error) {

info, err := sdk.GetStakePoolInfo(sdk.ProviderType(providerType), providerID)
Expand Down
31 changes: 31 additions & 0 deletions wasmsdk/auth_txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"syscall/js"

"github.com/0chain/gosdk/core/sys"
"github.com/0chain/gosdk/zcncore"
)

type AuthCallbackFunc func(msg string) string
Expand All @@ -28,6 +29,36 @@ func registerAuthorizer(this js.Value, args []js.Value) interface{} {
return nil
}

func registerZauthServer(serverAddr, splitPublicKey string) {
sys.SetAuthorize(zcncore.ZauthSignTxn(serverAddr, splitPublicKey))
sys.SetAuthCommon(zcncore.ZauthAuthCommon(serverAddr, splitPublicKey))
}

// zvaultNewWallet generates new split wallet
func zvaultNewWallet(serverAddr, token, passphrase string) (string, error) {
return zcncore.CallZvaultNewWalletString(serverAddr, token, "", passphrase)
}

// zvaultNewSplit generates new split wallet from existing clientID
func zvaultNewSplit(clientID, serverAddr, token, passphrase string) (string, error) {
return zcncore.CallZvaultNewWalletString(serverAddr, token, clientID, passphrase)
}

func zvaultStoreKey(serverAddr, token, privateKey, passphrase string) (string, error) {
return zcncore.CallZvaultStoreKeyString(serverAddr, token, privateKey, passphrase)
}

func registerAuthCommon(this js.Value, args []js.Value) interface{} {
authCallback = parseAuthorizerCallback(args[0])
authResponseC = make(chan string, 1)

sys.AuthCommon = func(msg string) (string, error) {
authCallback(msg)
return <-authResponseC, nil
}
return nil
}

func authResponse(response string) {
authResponseC <- response
}
Expand Down
3 changes: 1 addition & 2 deletions wasmsdk/jsbridge/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func BindFunc(global js.Value, jsFuncName string, fn interface{}) error {
func BindAsyncFuncs(global js.Value, fnList map[string]interface{}) {

for jsFuncName, fn := range fnList {
if jsFuncName == "registerAuthorizer" || jsFuncName == "callAuth" {
if jsFuncName == "registerAuthorizer" || jsFuncName == "callAuth" || jsFuncName == "registerAuthCommon" {
global.Set(jsFuncName, fn)
} else {
jsFunc, err := promise(fn)
Expand All @@ -45,7 +45,6 @@ func BindAsyncFuncs(global js.Value, fnList map[string]interface{}) {
}
}


func BindFuncs(global js.Value, fnList map[string]interface{}) {

for jsFuncName, fn := range fnList {
Expand Down
1 change: 1 addition & 0 deletions wasmsdk/jsbridge/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (b *InputBuilder) Build() (InputBinder, error) {
// Bind bind js inputs to reflect values
func (b *InputBuilder) Bind(args []js.Value) ([]reflect.Value, error) {
if len(args) != b.numIn {
fmt.Println("args:", args)
return nil, ErrMismatchedInputLength
}

Expand Down
52 changes: 52 additions & 0 deletions wasmsdk/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"sync"
Expand Down Expand Up @@ -62,6 +63,46 @@ func main() {
// js already has signatureScheme and keys
return signFunc(hash)
}

sys.SignWithAuth = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) {
sig, err := sys.Sign(hash, signatureScheme, keys)
if err != nil {
return "", fmt.Errorf("failed to sign with split key: %v", err)
}

data, err := json.Marshal(struct {
Hash string `json:"hash"`
Signature string `json:"signature"`
ClientID string `json:"client_id"`
}{
Hash: hash,
Signature: sig,
ClientID: client.GetClient().ClientID,
})
if err != nil {
return "", err
}

if sys.AuthCommon == nil {
return "", errors.New("authCommon is not set")
}

rsp, err := sys.AuthCommon(string(data))
if err != nil {
return "", err
}

var sigpk struct {
Sig string `json:"sig"`
}

err = json.Unmarshal([]byte(rsp), &sigpk)
if err != nil {
return "", err
}

return sigpk.Sig, nil
}
} else {
PrintError("__zcn_wasm__.jsProxy.sign is not installed yet")
}
Expand Down Expand Up @@ -217,6 +258,9 @@ func main() {
"unLockReadPool": unLockReadPool,
"createReadPool": createReadPool,

// claim rewards
"collectRewards": collectRewards,

// stakepool
"getSkatePoolInfo": getSkatePoolInfo,
"lockStakePool": lockStakePool,
Expand Down Expand Up @@ -258,8 +302,16 @@ func main() {
"setAuthUrl": setAuthUrl,

"registerAuthorizer": js.FuncOf(registerAuthorizer),
"registerAuthCommon": js.FuncOf(registerAuthCommon),
"callAuth": js.FuncOf(callAuth),
"authResponse": authResponse,

// zauth
"registerZauthServer": registerZauthServer,
// zvault
"zvaultNewWallet": zvaultNewWallet,
"zvaultNewSplit": zvaultNewSplit,
"zvaultStoreKey": zvaultStoreKey,
})

fmt.Println("__wasm_initialized__ = true;")
Expand Down