-
Notifications
You must be signed in to change notification settings - Fork 75
/
ownership.ts
58 lines (52 loc) · 1.3 KB
/
ownership.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
import { Storage } from '@massalabs/massa-as-sdk';
import { Args, boolToByte, stringToBytes } from '@massalabs/as-types';
import {
OWNER_KEY,
_isOwner,
_onlyOwner,
_setOwner,
} from './ownership-internal';
/**
* Set the contract owner
*
* @param binaryArgs - byte string with the following format:
* - the address of the new contract owner (address).
*/
export function setOwner(binaryArgs: StaticArray<u8>): void {
const args = new Args(binaryArgs);
const newOwner = args
.nextString()
.expect('newOwnerAddress argument is missing or invalid');
_setOwner(newOwner);
}
/**
* Returns the contract owner
*
* @returns owner address in bytes
*/
export function ownerAddress(_: StaticArray<u8>): StaticArray<u8> {
if (!Storage.has(OWNER_KEY)) {
return [];
}
return stringToBytes(Storage.get(OWNER_KEY));
}
/**
* Returns true if address is the owner of the contract.
*
* @param address -
*/
export function isOwner(binaryArgs: StaticArray<u8>): StaticArray<u8> {
if (!Storage.has(OWNER_KEY)) {
return [0]; // false
}
const address = new Args(binaryArgs)
.nextString()
.expect('address argument is missing or invalid');
return boolToByte(_isOwner(address));
}
/**
* Throws if the caller is not the owner.
*/
export function onlyOwner(): void {
_onlyOwner();
}