Skip to content

Commit 4641d27

Browse files
committed
feat: github oracle
Co-authored-by: Miguel Victoria Villaquiran <[email protected]>
1 parent 3c661d8 commit 4641d27

File tree

7 files changed

+174
-23
lines changed

7 files changed

+174
-23
lines changed

examples/gno.land/r/demo/teritori/gh/account.gno

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package gh
22

3-
import "errors"
3+
4+
import (
5+
"std"
6+
"errors"
7+
"gno.land/p/demo/avl"
8+
)
49

510
// Account represents a GitHub user account or organization.
611
type Account struct {
@@ -27,7 +32,7 @@ func (a Account) Validate() error {
2732
if a.login == "" {
2833
return errors.New("empty login")
2934
}
30-
if a.kind == "" {
35+
if a.kind == "" || (a.kind != UserAccount && a.kind != OrgAccount){
3136
return errors.New("empty kind")
3237
}
3338
if a.name == "" {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package gh
2+
3+
import (
4+
"std"
5+
)
6+
7+
var(
8+
adminAddr std.Address = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"
9+
)
10+
11+
12+
func assertIsAdmin() {
13+
if std.GetOrigCaller() != adminAddr {
14+
panic("restricted area")
15+
}
16+
}
17+
18+
func setAdminAddress(address std.Address) {
19+
adminAddr = address
20+
}
21+
22+
func SetAdminAddress(address std.Address) {
23+
assertIsAdmin()
24+
setAdminAddress(address)
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package gh
2+
3+
import (
4+
"std"
5+
"testing"
6+
7+
"gno.land/p/demo/avl"
8+
"gno.land/p/demo/testutils"
9+
)
10+
11+
func TestAssertIsAdmin(t *testing.T) {
12+
adminAddr = "g1test1234"
13+
randomuser := testutils.TestAddress("g1unknown_player")
14+
defer func() {
15+
if r := recover(); r != nil {
16+
}
17+
}()
18+
std.TestSetOrigCaller(randomuser)
19+
assertIsAdmin()
20+
t.Fatalf("should fail because not admin")
21+
}
22+

examples/gno.land/r/demo/teritori/gh/oracle.gno

+3-11
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import (
1111
var (
1212
accounts avl.Tree // uri -> Account
1313
lastUpdateTime time.Time // used by the bot to only upload the diff
14-
oracleAddr std.Address = "g1eunnckcl6r8ncwj0lrpxu9g5062xcvwxqlrf29"
15-
adminAddr std.Address = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"
14+
oracleAddr std.Address
1615
)
1716

1817
func OracleLastUpdated() time.Time { return lastUpdateTime }
1918

20-
func OracleUpsertAccount(id, name, kind string) {
19+
func OracleUpsertAccount(id, login, name, kind string) {
2120
assertIsOracle()
2221
lastUpdateTime = time.Now()
2322

@@ -33,6 +32,7 @@ func OracleUpsertAccount(id, name, kind string) {
3332
// update fields
3433
account.name = name
3534
account.kind = kind
35+
account.login = login
3636

3737
if err := account.Validate(); err != nil {
3838
panic(err)
@@ -47,14 +47,6 @@ func AdminSetOracleAddr(new std.Address) {
4747
oracleAddr = new
4848
}
4949

50-
func AdminGetOracleAddr() std.Address { return oracleAddr }
51-
52-
func assertIsAdmin() {
53-
if std.GetOrigCaller() != adminAddr {
54-
panic("restricted area")
55-
}
56-
}
57-
5850
func assertIsOracle() {
5951
if std.GetOrigCaller() != oracleAddr {
6052
panic("restricted area")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package gh
2+
3+
import (
4+
"std"
5+
"testing"
6+
"time"
7+
8+
"gno.land/p/demo/avl"
9+
"gno.land/p/demo/testutils"
10+
)
11+
12+
func TestOracleUpsertUserIsNotOracle(t *testing.T) {
13+
oracleAddr = "g1test1234"
14+
var randomuser = "g1unknown_player"
15+
user := testutils.TestAddress("user")
16+
defer func() {
17+
if r := recover(); r != nil {
18+
}
19+
}()
20+
OracleUpsertAccount("acountID", "villaquiranm","john doe","user")
21+
t.Fatalf("should fail because not admin")
22+
}
23+
24+
func TestOracleUpsertUserOk(t *testing.T) {
25+
oracleAddr = "g1random"
26+
if accounts.Size() != 0 {
27+
t.Fatalf("Accounts is not empty")
28+
}
29+
now := time.Now()
30+
31+
std.TestSetOrigCaller(oracleAddr)
32+
33+
var randomuser = "g1unknown_player"
34+
OracleUpsertAccount("acountID", "villaquiranm","john doe","user")
35+
36+
if accounts.Size() != 1 {
37+
t.Fatalf("User was not created")
38+
}
39+
40+
OracleUpsertAccount("acountID", "villaquiranm","john doe","user")
41+
42+
if accounts.Size() != 1 {
43+
t.Fatalf("User was created more than once")
44+
}
45+
46+
if OracleLastUpdated().Unix() < now.Unix() {
47+
t.Fatalf("OracleLastUpdated was not changed")
48+
}
49+
}
50+
51+
func TestAssertIsOracle(t *testing.T) {
52+
std.TestSetOrigCaller(adminAddr)
53+
AdminSetOracleAddr("g1random123")
54+
defer func() {
55+
if r := recover(); r != nil {
56+
}
57+
}()
58+
assertIsOracle()
59+
t.Fatalf("should fail because user is not oracle")
60+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
package gh
2+
3+
import (
4+
"fmt"
5+
"std"
6+
"gno.land/p/demo/avl"
7+
)
28
var (
3-
addressesToAccount avl.Tree // address -> AccountLink
4-
adminAddr std.Address = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"
9+
addressToAccount avl.Tree // address -> AccountLink
10+
guardian std.Address // guardian address
511
)
6-
func LinkAccount(accountID string, address std.Address) {
12+
13+
func init() {
14+
setAdminAddress(std.GetOrigCaller())
15+
}
16+
17+
//Todo maybe we should gave multi guardian
18+
func SetGuardian(address std.Address) {
719
assertIsAdmin()
8-
account := AccountByID(id string)
20+
guardian = address
21+
}
22+
23+
func LinkAccount(accountID string, address std.Address, signature string) {
24+
verifySignature(fmt.Sprintf("%s %s", accountID, address.String()), signature)
25+
26+
account := AccountByID(accountID)
927
if account == nil {
1028
panic("account not found")
1129
}
1230

13-
addressesToAccount.Set(address, account)
31+
addressToAccount.Set(address.String(), account)
1432
}
1533

16-
func assertIsAdmin() {
17-
if std.GetOrigCaller() != adminAddr {
18-
panic("restricted area")
19-
}
20-
}
34+
func verifySignature(message, signature string) bool {
35+
return true
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package gh
2+
3+
import (
4+
"std"
5+
"testing"
6+
7+
"gno.land/p/demo/avl"
8+
"gno.land/p/demo/testutils"
9+
)
10+
11+
func TestLinkAccount(t *testing.T) {
12+
adminAddr = "g1test1234"
13+
14+
user := testutils.TestAddress("user")
15+
admin := testutils.TestAddress("admin")
16+
17+
std.TestSetOrigCaller(admin)
18+
setAdminAddress(admin)
19+
AdminSetOracleAddr(admin)
20+
21+
OracleUpsertAccount("123","user", "user", UserAccount)
22+
LinkAccount("123", user, "signature")
23+
accountInPublic, ok := addressToAccount.Get(user.String())
24+
if !ok {
25+
t.Fatalf("account not found")
26+
}
27+
account := AccountByID("123")
28+
if accountInPublic != account {
29+
t.Fatalf("account is not same")
30+
}
31+
}

0 commit comments

Comments
 (0)