-
Notifications
You must be signed in to change notification settings - Fork 1
/
Auth.ts
165 lines (139 loc) · 4.17 KB
/
Auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {
Address,
Client,
CodeHash,
Contract,
ContractLink,
Deployment,
IntoLink,
Pagination,
Permit,
Signer,
VersionedDeployment,
ViewingKey,
linkStruct,
} from "./Core";
import { AuthProviderVersion, LatestAuthProviderVersion } from "./Versions";
export default class SiennaAuth extends VersionedDeployment<AuthProviderVersion> {
constructor (
options: object = {},
public readonly version: AuthProviderVersion = (options as any)?.version ?? LatestAuthProviderVersion,
/** Appended to the provider and oracle names. */
public readonly extraName: string|false = (options as any)?.extraName ?? false,
) {
super(options, version)
if (extraName) {
this.names.provider += `.${extraName}`
this.names.oracle = `${this.names.provider}.Oracle`
}
}
/** The names under which the provider and oracle are known in the deployment. */
names = {
provider: `Auth[${this.version}]`,
oracle: `Auth[${this.version}].Oracle`
}
/** The auth provider contract. */
provider = this.contract({
name: this.names.provider,
client: AuthProvider
})
/** The auth provider's RNG oracle. */
oracle = this.contract({
name: this.names.oracle
})
}
export type AuthStrategy =
| { type: "permit"; signer: Signer }
| { type: "vk"; viewing_key: { address: Address; key: ViewingKey } };
export type AuthMethod<T> =
| { permit: Permit<T> }
| { viewing_key: { address: Address; key: ViewingKey } };
export class MockAuthClient extends Client {
async update(second_contract: any) {
return this.execute({ update: { second_contract } });
}
}
export class AuthClient extends Client {
async setGroupKey (key: string, group_id: number) {
return await this.execute({ auth_client: { set_group_key: { key, group_id } } })
}
async changeProvider (provider: IntoLink) {
return await this.execute({ auth_client: { change_provider: { provider: linkStruct(provider) } } })
}
async getProvider (): Promise<AuthProvider> {
return await this.query({ auth_client: { provider: {} } })
}
}
export class AuthProvider extends Client {
async getOracle() {
return this.query({ oracle: {} });
}
// async getAdmin() {
// return this.query({ admin: { admin: {} } });
// }
async createGroup(name: string, members: ContractLink[], admin?: Address) {
return this.execute({ create_group: { name, members } });
}
async addMembers(groupId: number, members: ContractLink[]) {
return this.execute({ add_members: { groupId, members } });
}
async removeMembers(
groupId: number,
members: ContractLink[],
regenerateKey: boolean
) {}
async swapMembers(
groupId: number,
oldMember: ContractLink[],
newMember: ContractLink[],
regenerateKey: boolean
) {
return this.execute({
swap_members: {
group_id: groupId,
old_member: oldMember,
new_member: newMember,
regenerate_key: regenerateKey,
},
});
}
async removeGroup(groupId: number) {
return this.execute({ remove_group: { group_id: groupId } });
}
async getGroups(pagination: Pagination) {
return this.query({ groups: { pagination } });
}
async getGroup(id: number) {
return this.query({ group: id });
}
async findInGroups(target: ContractLink, groups: number[]) {
return this.query({ find_in_groups: { target, groups } });
}
async getLogs(pagination: Pagination) {
return this.query({ logs: { pagination } });
}
}
export class Auth {
static viewing_key(address: Address, key: ViewingKey): Auth {
return new this({ type: "vk", viewing_key: { address, key } });
}
static permit(signer: Signer): Auth {
return new this({ type: "permit", signer });
}
constructor(readonly strategy: AuthStrategy) {}
async createMethod<T>(
address: Address,
permission: T
): Promise<AuthMethod<T>> {
if (this.strategy.type === "permit") {
const permit = await this.strategy.signer.sign({
permit_name: `SiennaJS permit for ${address}`,
allowed_tokens: [address],
permissions: [permission],
});
return { permit };
} else {
return { viewing_key: this.strategy.viewing_key };
}
}
}