-
Notifications
You must be signed in to change notification settings - Fork 54
/
HederaAccountService.sol
66 lines (59 loc) · 4.04 KB
/
HederaAccountService.sol
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
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.5.0 <0.9.0;
pragma experimental ABIEncoderV2;
import "../HederaResponseCodes.sol";
import "./IHederaAccountService.sol";
abstract contract HederaAccountService {
address constant HASPrecompileAddress = address(0x16a);
/// Returns the amount of hbars that the spender has been authorized to spend on behalf of the owner.
/// @param owner The account that has authorized the spender
/// @param spender The account that has been authorized by the owner
/// @return responseCode The response code for the status of the request. SUCCESS is 22.
/// @return amount The amount of hbar that the spender has been authorized to spend on behalf of the owner.
function hbarAllowance(address owner, address spender) internal returns (int64 responseCode, int256 amount)
{
(bool success, bytes memory result) = HASPrecompileAddress.call(
abi.encodeWithSelector(IHederaAccountService.hbarAllowance.selector,
owner, spender));
(responseCode, amount) = success ? abi.decode(result, (int32, int256)) : (HederaResponseCodes.UNKNOWN, (int256)(0));
}
/// Allows spender to withdraw hbars from the owner account multiple times, up to the value amount. If this function is called
/// again it overwrites the current allowance with the new amount.
/// @param owner The owner of the hbars
/// @param spender the account address authorized to spend
/// @param amount the amount of hbars authorized to spend.
/// @return responseCode The response code for the status of the request. SUCCESS is 22.
function hbarApprove(address owner, address spender, int256 amount) internal returns (int64 responseCode)
{
(bool success, bytes memory result) = HASPrecompileAddress.call(
abi.encodeWithSelector(IHederaAccountService.hbarApprove.selector,
owner, spender, amount));
responseCode = success ? abi.decode(result, (int32)) : HederaResponseCodes.UNKNOWN;
}
/// Determines if the signature is valid for the given message hash and account.
/// It is assumed that the signature is composed of a single EDCSA or ED25519 key.
/// @param account The account to check the signature against
/// @param messageHash The hash of the message to check the signature against
/// @param signature The signature to check
/// @return responseCode The response code for the status of the request. SUCCESS is 22.
/// @return authorized True if the signature is valid, false otherwise
function isAuthorizedRaw(address account, bytes memory messageHash, bytes memory signature) internal returns (int64 responseCode, bool authorized) {
(bool success, bytes memory result) = HASPrecompileAddress.call(
abi.encodeWithSelector(IHederaAccountService.isAuthorizedRaw.selector,
account, messageHash, signature));
(responseCode, authorized) = success ? (HederaResponseCodes.SUCCESS, abi.decode(result, (bool))) : (HederaResponseCodes.UNKNOWN, false);
}
/// Determines if the signature is valid for the given message hash and account.
/// It is assumed that the signature is composed of a single EDCSA or ED25519 key.
/// @param account The account to check the signature against
/// @param messageHash The hash of the message to check the signature against
/// @param signature The signature to check
/// @return responseCode The response code for the status of the request. SUCCESS is 22.
/// @return authorized True if the signature is valid, false otherwise
function isAuthorized(address account, bytes memory messageHash, bytes memory signature) internal returns (int64 responseCode, bool authorized) {
(bool success, bytes memory result) = HASPrecompileAddress.call(
abi.encodeWithSelector(IHederaAccountService.isAuthorized.selector,
account, messageHash, signature));
(responseCode, authorized) = success ? (HederaResponseCodes.SUCCESS, abi.decode(result, (bool))) : (HederaResponseCodes.UNKNOWN, false);
}
}