-
Notifications
You must be signed in to change notification settings - Fork 105
Implement account permission delegation - XLS-74d and XLS-75d amendments #840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d2ee762
Transaction model, unit and integ tests for Delegate XLS-74d amendment
ckeshava 63f7dd3
fix linter errors
ckeshava e42af9f
Update xrpl/models/transactions/transaction.py
ckeshava 4b1491b
address PR comments from Phu and Rabbit
ckeshava 1d4b3d8
fix linter errors
ckeshava cf31824
add DelegatableTransaction and GranularPermission string enums
Patel-Raj11 b8deada
Merge branch 'main' of github.com:XRPLF/xrpl-py into feature/account-…
Patel-Raj11 190b666
handle PermissionValue serialization
Patel-Raj11 9d15f54
add unit test for DelegateSet serialization
Patel-Raj11 5b16990
fix integration tests
Patel-Raj11 f2a496d
refactor PermissionValue binary codec
Patel-Raj11 3626419
remove DelegatableTransaction enum
Patel-Raj11 d1ebbaa
refactor based on pr comments
Patel-Raj11 c36fa12
refactor definitions.py
Patel-Raj11 0c900a9
fix lint issue
Patel-Raj11 c536ef5
refactor integration test case
Patel-Raj11 b70dbfd
refactor nit comment
Patel-Raj11 0a85fa1
add TransactionType and GranularPermission for types
Patel-Raj11 88d4b8a
pr review refactors
Patel-Raj11 13f3eaa
pr review comments
Patel-Raj11 30cc5fc
Merge branch 'main' of github.com:XRPLF/xrpl-py into feature/account-…
Patel-Raj11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
from tests.integration.integration_test_case import IntegrationTestCase | ||
from tests.integration.it_utils import ( | ||
fund_wallet_async, | ||
sign_and_reliable_submission_async, | ||
test_async_and_sync, | ||
) | ||
from xrpl.models.requests import AccountObjects, AccountObjectType, LedgerEntry | ||
from xrpl.models.requests.ledger_entry import Delegate | ||
from xrpl.models.response import ResponseStatus | ||
from xrpl.models.transactions import ( | ||
AccountSet, | ||
DelegateSet, | ||
GranularPermission, | ||
Payment, | ||
) | ||
from xrpl.models.transactions.delegate_set import Permission | ||
from xrpl.models.transactions.types import TransactionType | ||
from xrpl.utils import xrp_to_drops | ||
from xrpl.wallet.main import Wallet | ||
|
||
|
||
class TestDelegateSet(IntegrationTestCase): | ||
@test_async_and_sync(globals()) | ||
async def test_delegation_with_no_permission(self, client): | ||
# Note: Using WALLET, DESTINATION accounts could pollute the test results | ||
alice = Wallet.create() | ||
await fund_wallet_async(alice) | ||
bob = Wallet.create() | ||
await fund_wallet_async(bob) | ||
carol = Wallet.create() | ||
await fund_wallet_async(carol) | ||
|
||
# Use bob's account to execute a transaction on behalf of alice | ||
payment = Payment( | ||
account=alice.address, | ||
amount=xrp_to_drops(1), | ||
destination=carol.address, | ||
delegate=bob.address, | ||
) | ||
response = await sign_and_reliable_submission_async( | ||
payment, bob, client, check_fee=False | ||
) | ||
self.assertEqual(response.status, ResponseStatus.SUCCESS) | ||
|
||
# The lack of AccountPermissionSet transaction will result in a tecNO_PERMISSION | ||
self.assertEqual(response.result["engine_result"], "tecNO_PERMISSION") | ||
|
||
@test_async_and_sync(globals()) | ||
async def test_delegate_set_workflow(self, client): | ||
# Note: Using WALLET, DESTINATION accounts could pollute the test results | ||
alice = Wallet.create() | ||
await fund_wallet_async(alice) | ||
bob = Wallet.create() | ||
await fund_wallet_async(bob) | ||
carol = Wallet.create() | ||
await fund_wallet_async(carol) | ||
|
||
delegate_set = DelegateSet( | ||
account=alice.address, | ||
authorize=bob.address, | ||
# Authorize bob account to execute Payment transactions and | ||
# modify the domain of an account behalf of alice's account. | ||
permissions=[ | ||
Permission(permission_value=TransactionType.PAYMENT), | ||
Permission(permission_value=GranularPermission.ACCOUNT_DOMAIN_SET), | ||
], | ||
) | ||
response = await sign_and_reliable_submission_async( | ||
delegate_set, alice, client, check_fee=False | ||
) | ||
self.assertEqual(response.status, ResponseStatus.SUCCESS) | ||
self.assertEqual(response.result["engine_result"], "tesSUCCESS") | ||
|
||
# Use the bob's account to execute a transaction on behalf of alice | ||
payment = Payment( | ||
account=alice.address, | ||
amount=xrp_to_drops(1), | ||
destination=carol.address, | ||
delegate=bob.address, | ||
) | ||
response = await sign_and_reliable_submission_async( | ||
payment, bob, client, check_fee=False | ||
) | ||
self.assertEqual(response.status, ResponseStatus.SUCCESS) | ||
self.assertEqual(response.result["engine_result"], "tesSUCCESS") | ||
|
||
# Validate that the transaction was signed by bob | ||
self.assertEqual(response.result["tx_json"]["Account"], alice.address) | ||
self.assertEqual(response.result["tx_json"]["Delegate"], bob.address) | ||
self.assertEqual(response.result["tx_json"]["SigningPubKey"], bob.public_key) | ||
|
||
# Use the bob's account to execute a transaction on behalf of alice | ||
account_set = AccountSet( | ||
account=alice.address, | ||
delegate=bob.address, | ||
email_hash="10000000002000000000300000000012", | ||
) | ||
response = await sign_and_reliable_submission_async( | ||
account_set, bob, client, check_fee=False | ||
) | ||
self.assertEqual(response.status, ResponseStatus.SUCCESS) | ||
self.assertEqual(response.result["engine_result"], "tecNO_PERMISSION") | ||
|
||
# test ledger entry | ||
ledger_entry_response = await client.request( | ||
LedgerEntry( | ||
delegate=Delegate( | ||
account=alice.address, | ||
authorize=bob.address, | ||
), | ||
) | ||
) | ||
self.assertTrue(ledger_entry_response.is_successful()) | ||
self.assertEqual( | ||
ledger_entry_response.result["node"]["LedgerEntryType"], | ||
"Delegate", | ||
) | ||
self.assertEqual(ledger_entry_response.result["node"]["Account"], alice.address) | ||
self.assertEqual(ledger_entry_response.result["node"]["Authorize"], bob.address) | ||
self.assertEqual(len(ledger_entry_response.result["node"]["Permissions"]), 2) | ||
|
||
@test_async_and_sync(globals()) | ||
async def test_fetch_delegate_account_objects(self, client): | ||
# Note: Using WALLET, DESTINATION accounts could pollute the test results | ||
alice = Wallet.create() | ||
await fund_wallet_async(alice) | ||
bob = Wallet.create() | ||
await fund_wallet_async(bob) | ||
|
||
delegate_set = DelegateSet( | ||
account=alice.address, | ||
authorize=bob.address, | ||
# Authorize bob's account to execute Payment transactions | ||
# and authorize a trustline on behalf of alice's account. | ||
permissions=[ | ||
Permission(permission_value=TransactionType.PAYMENT), | ||
Permission(permission_value=GranularPermission.TRUSTLINE_AUTHORIZE), | ||
], | ||
) | ||
response = await sign_and_reliable_submission_async( | ||
delegate_set, alice, client, check_fee=False | ||
) | ||
|
||
self.assertEqual(response.status, ResponseStatus.SUCCESS) | ||
self.assertEqual(response.result["engine_result"], "tesSUCCESS") | ||
|
||
account_objects_response = await client.request( | ||
AccountObjects(account=alice.address, type=AccountObjectType.DELEGATE) | ||
) | ||
|
||
granted_permission = { | ||
obj["Permission"]["PermissionValue"] | ||
for obj in account_objects_response.result["account_objects"][0][ | ||
"Permissions" | ||
] | ||
} | ||
|
||
self.assertEqual(len(granted_permission), 2) | ||
self.assertTrue(TransactionType.PAYMENT.value in granted_permission) | ||
self.assertTrue( | ||
GranularPermission.TRUSTLINE_AUTHORIZE.value in granted_permission | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
from unittest import TestCase | ||
|
||
from xrpl.models.exceptions import XRPLModelException | ||
from xrpl.models.transactions import DelegateSet | ||
from xrpl.models.transactions.delegate_set import ( | ||
PERMISSIONS_MAX_LENGTH, | ||
GranularPermission, | ||
Permission, | ||
) | ||
from xrpl.models.transactions.types import TransactionType | ||
|
||
_ACCOUNT = "r9LqNeG6qHxjeUocjvVki2XR35weJ9mZgQ" | ||
_DELEGATED_ACCOUNT = "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW" | ||
_MORE_THAN_10_PERMISSIONS = [ | ||
GranularPermission.PAYMENT_MINT, | ||
GranularPermission.ACCOUNT_MESSAGE_KEY_SET, | ||
GranularPermission.ACCOUNT_TICK_SIZE_SET, | ||
GranularPermission.ACCOUNT_DOMAIN_SET, | ||
TransactionType.PAYMENT, | ||
TransactionType.AMM_CLAWBACK, | ||
TransactionType.AMM_BID, | ||
TransactionType.ORACLE_DELETE, | ||
TransactionType.MPTOKEN_AUTHORIZE, | ||
TransactionType.MPTOKEN_ISSUANCE_DESTROY, | ||
TransactionType.CREDENTIAL_ACCEPT, | ||
] | ||
|
||
|
||
class TestDelegateSet(TestCase): | ||
def test_delegate_set(self): | ||
tx = DelegateSet( | ||
account=_ACCOUNT, | ||
authorize=_DELEGATED_ACCOUNT, | ||
permissions=[ | ||
Permission(permission_value=GranularPermission.TRUSTLINE_AUTHORIZE), | ||
Permission(permission_value=TransactionType.PAYMENT), | ||
], | ||
) | ||
self.assertTrue(tx.is_valid()) | ||
|
||
def test_delegate_set_granular_permission(self): | ||
tx = DelegateSet( | ||
account=_ACCOUNT, | ||
authorize=_DELEGATED_ACCOUNT, | ||
permissions=[Permission(permission_value=GranularPermission.PAYMENT_MINT)], | ||
) | ||
self.assertTrue(tx.is_valid()) | ||
|
||
def test_long_permissions_list(self): | ||
with self.assertRaises(XRPLModelException) as error: | ||
DelegateSet( | ||
account=_ACCOUNT, | ||
authorize=_DELEGATED_ACCOUNT, | ||
permissions=[ | ||
Permission(permission_value=_MORE_THAN_10_PERMISSIONS[i]) | ||
for i in range(len(_MORE_THAN_10_PERMISSIONS)) | ||
], | ||
) | ||
self.assertEqual( | ||
error.exception.args[0], | ||
"{'permissions': 'Length of `permissions` list is greater than " | ||
+ str(PERMISSIONS_MAX_LENGTH) | ||
+ ".'}", | ||
) | ||
|
||
def test_duplicate_permission_value(self): | ||
with self.assertRaises(XRPLModelException) as error: | ||
DelegateSet( | ||
account=_ACCOUNT, | ||
authorize=_DELEGATED_ACCOUNT, | ||
permissions=[ | ||
Permission(permission_value=TransactionType.ORACLE_DELETE), | ||
Permission(permission_value=TransactionType.ORACLE_DELETE), | ||
], | ||
) | ||
self.assertEqual( | ||
error.exception.args[0], | ||
"{'permissions': 'Duplicate permission value in `permissions` list.'}", | ||
) | ||
|
||
def test_account_and_delegate_are_the_same(self): | ||
with self.assertRaises(XRPLModelException) as error: | ||
DelegateSet( | ||
account=_ACCOUNT, | ||
authorize=_ACCOUNT, | ||
permissions=[ | ||
Permission( | ||
permission_value=GranularPermission.MPTOKEN_ISSUANCE_LOCK | ||
), | ||
], | ||
) | ||
self.assertEqual( | ||
error.exception.args[0], | ||
"{'account_addresses': 'Field `authorize` and `account` must be different." | ||
+ "'}", | ||
) | ||
|
||
def test_non_delegatable_transactions(self): | ||
with self.assertRaises(XRPLModelException) as error: | ||
DelegateSet( | ||
account=_ACCOUNT, | ||
authorize=_DELEGATED_ACCOUNT, | ||
permissions=[ | ||
Permission( | ||
permission_value=GranularPermission.MPTOKEN_ISSUANCE_LOCK | ||
), | ||
Permission(permission_value=TransactionType.ACCOUNT_DELETE), | ||
], | ||
) | ||
self.assertEqual( | ||
error.exception.args[0], | ||
"{'permissions': \"Non-delegatable transactions found in `permissions` " | ||
"list: {<TransactionType.ACCOUNT_DELETE: 'AccountDelete'>}.\"}", | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.