diff --git a/smart-contract/assembly/__tests__/deweb-interface/ownership.spec.ts b/smart-contract/assembly/__tests__/deweb-interface/ownership.spec.ts new file mode 100644 index 0000000..3c2d6b4 --- /dev/null +++ b/smart-contract/assembly/__tests__/deweb-interface/ownership.spec.ts @@ -0,0 +1,44 @@ +import { Args } from '@massalabs/as-types'; +import { + Address, + get, + getKeys, + resetStorage, + setDeployContext, +} from '@massalabs/massa-as-sdk'; +import { + constructor, + transferOwnership, +} from '../../contracts/deweb-interface'; +import { OWNER_KEY } from '@massalabs/sc-standards/assembly/contracts/utils/ownership-internal'; + +const user1 = new Address( + 'AU12UBnqTHDQALpocVBnkPNy7y5CndUJQTLutaVDDFgMJcq5kQiKq', +); +const user2 = new Address( + 'AU1Sxv7v8KZtYQHvAq1V8fB5z3QJ1Q5t7ccDeASEgbLPmMn4SVO1P', +); + +describe('Ownership', () => { + beforeEach(() => { + resetStorage(); + setDeployContext(user1.toString()); + constructor(new Args().serialize()); + }); + + test('transfer ownership', () => { + transferOwnership(new Args().add(user2).serialize()); + + const owner = get(OWNER_KEY); + + expect(owner).toStrictEqual(user2.toString(), 'Owner should be user2'); + }); + + test('renounce ownership', () => { + transferOwnership(new Args().serialize()); + + const owner = get(OWNER_KEY); + + expect(owner).toHaveLength(0, 'Owner should be empty'); + }); +}); diff --git a/smart-contract/assembly/contracts/deweb-interface.ts b/smart-contract/assembly/contracts/deweb-interface.ts index 417f41c..068afbf 100644 --- a/smart-contract/assembly/contracts/deweb-interface.ts +++ b/smart-contract/assembly/contracts/deweb-interface.ts @@ -1,4 +1,5 @@ import { + Address, balance, Context, generateEvent, @@ -45,6 +46,24 @@ export function constructor(_: StaticArray): void { _setOwner(Context.caller().toString()); } +/** + * Change the owner of the smart contract to the address passed as an argument + * @param _binaryArgs - serialized arguments containing the new owner address + * @returns void + */ +export function transferOwnership(_binaryArgs: StaticArray): void { + _onlyOwner(); + + const args = new Args(_binaryArgs); + const newOwnerResult = args.nextSerializable
(); + + const newOwner = newOwnerResult.isOk() + ? newOwnerResult.unwrap().toString() + : ''; + + _setOwner(newOwner); +} + /* -------------------------------------------------------------------------- */ /* FILE INITIALIZATION */ /* -------------------------------------------------------------------------- */