forked from ArweaveTeam/SmartWeave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
name-system.js
85 lines (71 loc) · 2.74 KB
/
name-system.js
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
// A simple name system:
// Supports just one level of names.
// Supports transferring a name.
// Supports associating a string with a name.
// Supports giving up a name.
// Note: this is untested example atm.
// TODO: require a minimum reward or burn of Ar to register a name.
export function handle (state, action) {
if (action.input.function === 'register') {
if (typeof action.input.name !== 'string' || action.input.name.length < 3) {
throw new ContractError(`Invalid name provided: ${action.input.name}`)
}
if (typeof action.input.data !== 'string') {
throw new ContractError('Must provide data to be associated with the name')
}
if (state.names[action.input.name]) {
throw new ContractError('Name already registered')
}
state.names[action.input.name] = {
ownedBy: action.caller,
data: action.input.data
}
return { state }
}
if (action.input.function === 'update') {
if (typeof action.input.name !== 'string' || action.input.name.length < 3) {
throw new ContractError(`Invalid name provided: ${action.input.name}`)
}
if (typeof action.input.data !== 'string') {
throw new ContractError('Must provide data to be associated with the name')
}
if (!state.names[action.input.name]) {
throw new ContractError('Name not registered')
}
if (state.names[action.input.name].ownedBy !== action.caller) {
throw new ContractError('Name not owned by caller')
}
state.names[action.input.name].data = action.input.data
return { state }
}
if (action.input.function === 'transfer') {
if (typeof action.input.name !== 'string' || action.input.name.length < 3) {
throw new ContractError(`Invalid name provided: ${action.input.name}`)
}
if (typeof action.input.target !== 'string') {
throw new ContractError('Must provide a target to transfer the name to')
}
if (!state.names[action.input.name]) {
throw new ContractError('Name not registered')
}
if (state.names[action.input.name].ownedBy !== action.caller) {
throw new ContractError('Name not owned by caller')
}
state.names[action.input.name].ownedBy = action.input.target
return { state }
}
if (action.input.function === 'giveup') {
if (typeof action.input.name !== 'string' || action.input.name.length < 3) {
throw new ContractError(`Invalid name provided: ${action.input.name}`)
}
if (!state.names[action.input.name]) {
throw new ContractError('Name not registered')
}
if (state.names[action.input.name].ownedBy !== action.caller) {
throw new ContractError('Name not owned by caller')
}
delete state.names[action.input.name]
return { state }
}
throw new ContractError('Invalid input')
}