forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Miguel Victoria Villaquiran <[email protected]>
- Loading branch information
Showing
8 changed files
with
258 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
gnokey maketx addpkg \ | ||
-deposit="1ugnot" \ | ||
-gas-fee="1ugnot" \ | ||
-gas-wanted="5000000" \ | ||
-broadcast="true" \ | ||
-remote="https://rpc.gno.land:443" \ | ||
-chainid="portal-loop" \ | ||
-pkgdir="." \ | ||
-pkgpath="gno.land/r/mikecito/gh_test_4" \ | ||
mykey2 | ||
|
||
gnokey maketx call \ | ||
-gas-fee="1ugnot" \ | ||
-gas-wanted="5000000" \ | ||
-broadcast="true" \ | ||
-remote="https://rpc.gno.land:443" \ | ||
-chainid="portal-loop" \ | ||
-pkgpath="gno.land/r/mikecito/gh_test_4" \ | ||
-func="AdminSetOracleAddr" \ | ||
-args="g1d46t4el0dduffs5j56t2razaeyvnmkxlduduuw" \ | ||
mykey2 | ||
|
||
gnokey maketx call \ | ||
-gas-fee="1ugnot" \ | ||
-gas-wanted="5000000" \ | ||
-broadcast="true" \ | ||
-remote="https://rpc.gno.land:443" \ | ||
-chainid="portal-loop" \ | ||
-pkgpath="gno.land/r/mikecito/gh_test_4" \ | ||
-func="OracleUpsertAccount" \ | ||
-args="15034695" \ | ||
-args="omarsy" \ | ||
-args="6h057" \ | ||
-args="user" \ | ||
mykey2 | ||
|
||
gnokey query "vm/qeval" -data='gno.land/r/mikecito/gh_test_4 | ||
AccountByID("15034695")' -remote="https://rpc.gno.land:443" | ||
|
||
gnokey query "vm/qeval" -data='gno.land/r/mikecito/gh_test_4 | ||
RenderAccount("g14mfv59k38r8k5vkevpu0lpqlqra0e9trwp3d32")' -remote="https://rpc.gno.land:443" | ||
|
||
gnokey query "vm/qeval" -data='gno.land/r/mikecito/gh_test_4 | ||
Render()' -remote="https://rpc.gno.land:443" |
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,25 @@ | ||
package gh | ||
|
||
import ( | ||
"std" | ||
) | ||
|
||
var( | ||
adminAddr std.Address = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq" | ||
) | ||
|
||
|
||
func assertIsAdmin() { | ||
if std.GetOrigCaller() != adminAddr { | ||
panic("restricted area") | ||
} | ||
} | ||
|
||
func setAdminAddress(address std.Address) { | ||
adminAddr = address | ||
} | ||
|
||
func SetAdminAddress(address std.Address) { | ||
assertIsAdmin() | ||
setAdminAddress(address) | ||
} |
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,22 @@ | ||
package gh | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/testutils" | ||
) | ||
|
||
func TestAssertIsAdmin(t *testing.T) { | ||
adminAddr = "g1test1234" | ||
randomuser := testutils.TestAddress("g1unknown_player") | ||
defer func() { | ||
if r := recover(); r != nil { | ||
} | ||
}() | ||
std.TestSetOrigCaller(randomuser) | ||
assertIsAdmin() | ||
t.Fatalf("should fail because not admin") | ||
} | ||
|
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,60 @@ | ||
package gh | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
"time" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/testutils" | ||
) | ||
|
||
func TestOracleUpsertUserIsNotOracle(t *testing.T) { | ||
oracleAddr = "g1test1234" | ||
var randomuser = "g1unknown_player" | ||
user := testutils.TestAddress("user") | ||
defer func() { | ||
if r := recover(); r != nil { | ||
} | ||
}() | ||
OracleUpsertAccount("acountID", "villaquiranm","john doe","user") | ||
t.Fatalf("should fail because not admin") | ||
} | ||
|
||
func TestOracleUpsertUserOk(t *testing.T) { | ||
oracleAddr = "g1random" | ||
if accounts.Size() != 0 { | ||
t.Fatalf("Accounts is not empty") | ||
} | ||
now := time.Now() | ||
|
||
std.TestSetOrigCaller(oracleAddr) | ||
|
||
var randomuser = "g1unknown_player" | ||
OracleUpsertAccount("acountID", "villaquiranm","john doe","user") | ||
|
||
if accounts.Size() != 1 { | ||
t.Fatalf("User was not created") | ||
} | ||
|
||
OracleUpsertAccount("acountID", "villaquiranm","john doe","user") | ||
|
||
if accounts.Size() != 1 { | ||
t.Fatalf("User was created more than once") | ||
} | ||
|
||
if OracleLastUpdated().Unix() < now.Unix() { | ||
t.Fatalf("OracleLastUpdated was not changed") | ||
} | ||
} | ||
|
||
func TestAssertIsOracle(t *testing.T) { | ||
std.TestSetOrigCaller(adminAddr) | ||
AdminSetOracleAddr("g1random123") | ||
defer func() { | ||
if r := recover(); r != nil { | ||
} | ||
}() | ||
assertIsOracle() | ||
t.Fatalf("should fail because user is not oracle") | ||
} |
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 |
---|---|---|
@@ -1,20 +1,63 @@ | ||
package gh | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/p/demo/avl" | ||
) | ||
|
||
var ( | ||
addressesToAccount avl.Tree // address -> AccountLink | ||
adminAddr std.Address = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq" | ||
) | ||
func LinkAccount(accountID string, address std.Address) { | ||
addressToAccount avl.Tree // address -> Account | ||
guardian std.Address // guardian address | ||
) | ||
|
||
func init() { | ||
addressToAccount = avl.Tree{} | ||
setAdminAddress(std.GetOrigCaller()) | ||
} | ||
|
||
// Todo maybe we should gave multi guardian | ||
func SetGuardian(address std.Address) { | ||
assertIsAdmin() | ||
account := AccountByID(id string) | ||
guardian = address | ||
} | ||
|
||
func LinkAccount(accountID string, address std.Address, signature string) { | ||
verifySignature(accountID+" "+address.String(), signature) | ||
|
||
account := AccountByID(accountID) | ||
if account == nil { | ||
panic("account not found") | ||
} | ||
addressesToAccount.Set(address, account) | ||
|
||
addressToAccount.Set(address.String(), account) | ||
} | ||
|
||
func assertIsAdmin() { | ||
if std.GetOrigCaller() != adminAddr { | ||
panic("restricted area") | ||
func RenderAccount(address std.Address) string { | ||
account, ok := addressToAccount.Get(address.String()) | ||
if !ok { | ||
panic("account not found") | ||
} | ||
|
||
return account.(*Account).Render() | ||
} | ||
|
||
func Render(address string) string { | ||
if address != "" { | ||
return RenderAccount(std.Address(address)) | ||
} | ||
} | ||
str := "[" | ||
addressToAccount.Iterate("", "", func(key string, value interface{}) bool { | ||
account := value.(*Account) | ||
str += account.Render() | ||
str += "," | ||
return false | ||
}) | ||
str += "]" | ||
|
||
return str | ||
} | ||
|
||
func verifySignature(message, signature string) bool { | ||
return true | ||
} |
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,31 @@ | ||
package gh | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/testutils" | ||
) | ||
|
||
func TestLinkAccount(t *testing.T) { | ||
adminAddr = "g1test1234" | ||
|
||
user := testutils.TestAddress("user") | ||
admin := testutils.TestAddress("admin") | ||
|
||
std.TestSetOrigCaller(admin) | ||
setAdminAddress(admin) | ||
AdminSetOracleAddr(admin) | ||
|
||
OracleUpsertAccount("123","user", "user", UserAccount) | ||
LinkAccount("123", user, "signature") | ||
accountInPublic, ok := addressToAccount.Get(user.String()) | ||
if !ok { | ||
t.Fatalf("account not found") | ||
} | ||
account := AccountByID("123") | ||
if accountInPublic != account { | ||
t.Fatalf("account is not same") | ||
} | ||
} |