Skip to content

Commit

Permalink
Merge pull request #1524 from aeternity/feature/test-tx-deeplink
Browse files Browse the repository at this point in the history
Fix and test sign transaction deeplink
  • Loading branch information
davidyuk authored Jan 11, 2024
2 parents fe886ab + 11919d8 commit 5f586b1
Show file tree
Hide file tree
Showing 18 changed files with 699 additions and 11 deletions.
543 changes: 537 additions & 6 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"workbox-precaching": "^7.0.0"
},
"devDependencies": {
"@aeternity/aepp-sdk-next": "npm:@aeternity/aepp-sdk@^13.2.2",
"@babel/eslint-parser": "^7.23.3",
"@frsource/cypress-plugin-visual-regression-diff": "^3.3.10",
"@intlify/eslint-plugin-vue-i18n": "^2.0.0",
Expand Down
12 changes: 7 additions & 5 deletions src/store/plugins/ui/urlRequestHandler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { times } from 'lodash-es';
import { ensureLoggedIn, mergeEnterHandlers } from '../../../router/utils';
import { ROUTE_MOBILE_LOGGED_IN } from '../../../lib/constants';

const urlRequestMethods = ['address', 'addressAndNetworkUrl', 'sign', 'signTransaction'];

Expand All @@ -9,12 +8,12 @@ export default (store) => {
const method = url.path.replace('/', '');
const callbackUrl = new URL(url.query.callback);
const lastParamIdx = Math.max(
0,
-1,
...Array.from(Object.keys(url.query))
.map((key) => key.startsWith('param') && +key.replace('param', '')),
);
const params = times(
lastParamIdx,
lastParamIdx + 1,
(idx) => JSON.parse(decodeURIComponent(url.query[`param${idx}`])),
);
const reply = ({ result, error }) => {
Expand All @@ -30,7 +29,10 @@ export default (store) => {
}
};

if (!['http:', 'https:'].includes(callbackUrl.protocol)) {
if (
!['http:', 'https:'].includes(callbackUrl.protocol)
&& !callbackUrl.href.startsWith('about:blank')
) {
reply({ error: new Error(`Unknown protocol: ${callbackUrl.protocol}`) });
return;
}
Expand Down Expand Up @@ -58,7 +60,7 @@ export default (store) => {
ensureLoggedIn,
(to, from, next) => {
handleUrlRequest(to);
next(ROUTE_MOBILE_LOGGED_IN);
next(false);
},
),
}));
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
138 changes: 138 additions & 0 deletions tests/e2e/specs/transfer/sign-transaction.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import {
buildTx, unpackTx, Tag, encode, decode, commitmentHash, Encoding, ORACLE_TTL_TYPES, oracleQueryId,
} from '@aeternity/aepp-sdk-next';

describe('Sign transaction', () => {
const nonce = 1;
const nameTtl = 1;
const clientTtl = 1;
const amount = 12.34e18;
const accountId = 'ak_2iBPH7HUz3cSDVEUWiHg76MZJ6tZooVNBmmxcgVK6VV8KAE688';
const recipientId = 'ak_2iBPH7HUz3cSDVEUWiHg76MZJ6tZooVNBmmxcgVK6VV8KAE688';
const name = 'test123test.chain';
const nameId = 'nm_2sFnPHi5ziAqhdApSpRBsYdomCahtmk3YGNZKYUTtUNpVSMccC';
const nameFee = '1000000000000000000000';
const contractId = 'ct_TCQVoset7Y4qEyV5tgEAJAqa2Foz8J1EXqoGpq3fB6dWH5roe';
const pointers = [
{ key: 'account_pubkey', id: accountId },
{ key: 'test key', id: accountId },
{ key: 'contract_pubkey', id: contractId },
];
const payload = encode(Buffer.from('test'), Encoding.Bytearray);
const queryFormat = '{\'city\': str}';
const responseFormat = '{\'tmp\': num}';
const queryFee = 30000;
const oracleTtl = { oracleTtlType: ORACLE_TTL_TYPES.delta, oracleTtlValue: 500 };
const responseTtl = { responseTtlType: ORACLE_TTL_TYPES.delta, responseTtlValue: 100 };
const queryTtl = { queryTtlType: ORACLE_TTL_TYPES.delta, queryTtlValue: 100 };
const query = '{\'city\': \'Berlin\'}';
const queryResponse = '{\'tmp\': 101}';
const callData = 'cb_KxFs8lcLG2+HEPb2FOjjZ2DqRd4=';
const gasLimit = 5e6;
const nameSalt = 4204563566073083;
const commitmentId = commitmentHash(name, nameSalt);
const oracleId = encode(decode(accountId), Encoding.OracleAddress);

const transactions = [{
tag: Tag.SpendTx, senderId: accountId, recipientId: accountId, nonce, amount, payload,
}, {
tag: Tag.NamePreclaimTx, accountId, nonce, commitmentId,
}, {
tag: Tag.NameClaimTx, accountId, nonce, name, nameSalt, nameFee,
}, {
tag: Tag.NameUpdateTx, accountId, nonce, nameId, nameTtl, pointers, clientTtl,
}, {
tag: Tag.NameRevokeTx, accountId, nonce, nameId,
}, {
tag: Tag.NameTransferTx, accountId, nonce, nameId, recipientId,
}, {
tag: Tag.ContractCreateTx,
nonce,
ownerId: accountId,
code: 'cb_+ExGA6Dh3797nquCHCSm088avgOiqgjjarRQviEYAXq+YlegWcCgjf6AeCCSADcBBwcBAQCOLwERgHggkhlnZXRBcmeCLwCFNy4wLjEALb9eTg==',
amount,
gasLimit,
callData,
}, {
tag: Tag.ContractCallTx,
nonce,
callerId: accountId,
contractId,
amount,
gasLimit,
callData,
}, {
tag: Tag.OracleRegisterTx,
nonce,
accountId,
queryFormat,
responseFormat,
queryFee,
...oracleTtl,
}, {
tag: Tag.OracleExtendTx, nonce, oracleId, ...oracleTtl,
}, {
tag: Tag.OracleQueryTx,
nonce,
oracleId,
...responseTtl,
query,
...queryTtl,
queryFee,
senderId: accountId,
}, {
tag: Tag.OracleResponseTx,
nonce,
oracleId,
...responseTtl,
queryId: oracleQueryId(accountId, nonce, oracleId),
response: queryResponse,
}];

transactions.forEach((txParams) => {
it(`shows ${Tag[txParams.tag]} details`, () => {
const url = new URL('http://localhost/signTransaction');
url.searchParams.append('param0', `"${buildTx(txParams)}"`);
url.searchParams.append('callback', 'about:blank');
cy
.viewport('iphone-se2')
.visit(url.href.replace('http://localhost', ''), { login: true });
cy.get('.page.primary').should('be.visible').then(($el) => {
if ($el.text().includes('Sign raw transaction')) return;
cy.get('.details-fee-input .turtle-rabbit').should('be.visible');
});
cy.matchImage();
});
});

it('signs transaction', () => {
const url = new URL('http://localhost/signTransaction');
url.searchParams.append('param0', `"${buildTx(transactions[0])}"`);
url.searchParams.append('callback', 'about:blank');
cy
.viewport('iphone-se2')
.visit(url.href.replace('http://localhost', ''), { login: true });
cy.get('button').contains('Confirm').click();
const skipButton = cy.get('.modal-plain .ae-button.primary');
cy.matchImage();
skipButton.click();

cy.url().should('contain', 'about:blank').then((u) => {
const resultUrl = new URL(u);
const signedTx = resultUrl.searchParams.get('result');
const txParams = unpackTx(JSON.parse(decodeURIComponent(signedTx)), Tag.SignedTx);
expect(txParams.signatures).to.have.length(1);
});
});

it('cancel transaction signing', () => {
const url = new URL('http://localhost/signTransaction');
url.searchParams.append('param0', `"${buildTx(transactions[0])}"`);
url.searchParams.append('callback', 'about:blank');
cy
.viewport('iphone-se2')
.visit(url.href.replace('http://localhost', ''), { login: true });
cy.get('button').contains('Cancel').click();
cy.url().should('equal', 'about:blank?error=Rejected+by+user');
});
});
16 changes: 16 additions & 0 deletions tests/e2e/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,19 @@ import './commands';

// Alternatively you can use CommonJS syntax:
// require('./commands')

beforeEach(() => {
cy.intercept({
method: 'GET',
url: 'https://api.coingecko.com/api/v3/simple/price*',
}, {
aeternity: {
usd: 0.0200868,
eur: 0.01835634,
btc: 4.44868e-07,
gbp: 0.01578517,
aud: 0.02998522,
cny: 0.142835,
},
});
});

0 comments on commit 5f586b1

Please sign in to comment.