What Are the Licensing Methods in the Solidity Smart Contract

Introduction

When writing smart contracts, I tend to take a guided approach. Even if they are intended for production environments, I make them as easy to understand as possible. The smart contracts I write are reusable, but they are usually rewritten for each specific business case.

In this article, I will discuss three licensing methods in solid smart contracts. Discuss the complexity of these methods from high to low, which is the order you should consider in your project. I provide code that can be used for each method.

This article assumes that you can easily write smart contracts and use functions such as inheriting and passing contract addresses as parameters.

Simple method - ownable.sol

Openzeppelin's ownable.sol contract must be one of the most reusable contracts. It is implemented in 77 lines:

1. The logic of asserting that someone is the owner of a smart contract.

2. Restrict function calls to inherit the logic of the owner of the smart contract.

3. Logic for transferring ownership to other addresses.

When writing smart contracts, you often inherit from ownable. Let's look at an example of how to use ownable. Imagine that you want to keep an address list in the smart contract, but you want to be the only list that can add more addresses. Think of it as a registry for people you trust. You can do the following:

contract Whitelist is Ownable

mapping (address =》 bool) members;

constructor() public Ownable()

funcTIon addMember(address _member)

public

onlyOwner

membersï¼»_memberï¼½ = true;

Inheriting from ownable and calling its constructor on your ownable ensures that the address where the smart contract is deployed is registered as the owner. The onlyowner modifier restores the function if it is not called from an address registered as the owner.

After deploying this smart contract, only you or the person you specify can add new members to its list.

Although useful, many times ownable is not enough. Only one address can become the owner at a given time. Only the owner can decide who can become the new owner. You can only check whether you are the owner, not others.

Intermediate complex method - whitelist.sol

Whitelist.sol keeps a list of addresses that can then be used to restrict functionality or for any other purpose. It is functionally very similar to openzeppelin's roles. Sol, although there are some important differences.

Whitelist.sol has only three functions:

funcTIon isMember(address _member) public view returns(bool);

funcTIon addMember(address _member) public onlyOwner;

funcTIon removeMember(address _member) public onlyOwner;

For example, with this smart contract, you can keep a list of approved stakeholders who may be the only recipients of token transfer. You can do the following:

pragma solidity ^0.5.0;

import “@openzeppelin/contracts/token/ERC20/ERC20.sol”;

import “。./access/Whitelist.sol”;

contract ERC20Whitelisted is ERC20

Whitelist whitelist;

constructor(address _whitelistAddress) public

whitelist = Whitelist(_whitelistAddress);

function transfer(address account, uint256 amount) public

require(whitelist.isMember(account), “Account not whitelisted.”);

super._transfer(account, amount);

In the above example, you can also make erc20 whitelist inherit from erc20 and whitelist. I'd be happy to discuss some compromises.

A simple whitelist can be very powerful. Openzeppelin uses them to implement many erc20 and erc721 variants and tries to provide more functionality than most of us need. At techhq, we also implemented cementdao only using the white list.

But sometimes the white list fails. You may need more than one owner to have a whitelist. Or you may need to manage many overlapping whitelists. For these cases, we have hierarchical role contracts.

Advanced complex method - rbac.sol

We have developed rbac.sol to provide the same multi-user functions as modern sharing systems.

1. Some roles are just address groups.

2. Group membership can only be modified by members of some administrator roles.

3. You can create new roles at run time.

4. Role membership can be verified.

At the lower level, we use the bytes32 parameter selected by the user to identify the role. Typically, these are recognizable short strings, but you can also use encrypted values or addresses.

The role itself is a set of member addresses and identifiers for the admin role. Interestingly, we don't need to store the role's identifier in its own structure.

struct Role

bytes32 adminRoleId;

mapping (address =》 bool) members;

There are two ways to add a new role and verify that the role exists:

function roleExists(bytes32 _roleId) public view returns(bool);

function addRole(bytes32 _roleId, bytes32 _adminRoleId) public;

And the functions of managing members are the same, except that relevant roles must be specified now:

function isMember(address _member, bytes32 _roleId) public view returns(bool);

function addMember(address _member, bytes32 _roleId) public;

function removeMember(address _member, bytes32 _roleId) public;

Addmember and removemember will succeed only if the caller belongs to the administrator role of the role we want to add members to.

Addrole succeeds only if the caller belongs to a role that will manage the created role.

These simple rules will allow you to create a hierarchy of roles that can then be used to implement complex multi-user platforms with different permission levels or regions.

Advanced learning

In order to go further into the rabbit hole, I suggest starting with the problem of openzeppelin. Their code base is no different from ours, and even if we choose to adopt other methods, you will find that most design decisions are thoroughly reasoned. Their use of roles in contracts such as erc20mintable is a good example, which can replace whitelist.

Another resource for the brave is the aragonos ACL contract. At a glance, they decided to go further:

function hasPermission(address who, address where, bytes32 what,

bytes how) public view returns (bool);

For the examples in our own @ hq20 / contracts package, we use the three levels of access control described in this article, so you should also be aware of this.

Conclusion

For the implementation of smart contract, it is best to realize only the required complexity without any complexity. There are three different levels of complexity in licensing:

·Single user

·User group

·Hierarchy of user groups

You can use ownable.sol for systems that are allowed by a single user. You can use @ openzeppelin / roles.sol or @ hq20 / whitelist.sol for systems that require users with permissions in the group. For systems that require a group hierarchy, we have successfully used @ hq20 / rbac.sol in the past.

You will have your own requirements and need to weigh the trade-offs. Understanding the design decisions behind each implementation will enable you to use existing contracts or modify them for your own use.

get in touch with us
Articoli raccomandati
Apple Smart Ring Patent, with Gesture Input Function, also Supports Siri
According to patentlyapple, the U.S. patent and Trademark Office recently granted apple a patent related to Apple's future smart ring.It is reported that Apple began brewing patents related to smart rings as early as 2015 and obtained patents related to smart rings in October this year. According to the Patent Description, this is a ring device with a touch panel or display, and the hardware part includes touch panels, cameras, buttons, etc. In addition to the hardware, the patent also reveals that it supports functions such as speakers and microphones. It is worth noting that Apple's smart ring will be able to support Siri and can be used to control computers, TVs, TV boxes and other devices. In addition, the ring can also be used to control some functions of home lighting or vehicles.In this continuation patent, apple mainly focuses on the micro touch screen on the ring. However, the focus this time is still the gesture input function. According to the Patent Description, the smart ring will include an application that controls an external electronic device using a predetermined gesture, and the device will execute instructions according to the predetermined gesture and output relevant results. The predetermined gesture will also include a pointing gesture.
Human Machine Experience Becomes the Decisive Point of Intelligent Machinery and Brings Optimal User
What Are Some Ways to Keep My Hair Really Healthy?
How Was the First Sale of Vivo X60 Series?
Huami Technology Held a Press Conference on August 27, and Amazfit Smart Sports Watch 3 Officially A
Connect to Nonencrypted Wireless Network Using Ubuntu Commands
Is Time Travel Possible? Can We Travel Back in Time?
Which Is a Better Design to Put on a Beer Pong Table?
What Else to Do on PS3?
How to Protect Data on IOT Devices
related searches
Apple Smart Ring Patent, with Gesture Input Function, also Supports Siri
Human Machine Experience Becomes the Decisive Point of Intelligent Machinery and Brings Optimal User
What Are Some Ways to Keep My Hair Really Healthy?
How Was the First Sale of Vivo X60 Series?
Huami Technology Held a Press Conference on August 27, and Amazfit Smart Sports Watch 3 Officially A
Connect to Nonencrypted Wireless Network Using Ubuntu Commands
Is Time Travel Possible? Can We Travel Back in Time?
Which Is a Better Design to Put on a Beer Pong Table?
What Else to Do on PS3?

Copyright © 2020 Coffee bag - Guangzhou tianci packaging industry Co,. Ltd. | Sitemap