Skip to content

Commit 1a491e6

Browse files
committed
Fix tests
1 parent ac60219 commit 1a491e6

File tree

2 files changed

+64
-22
lines changed

2 files changed

+64
-22
lines changed

js/test/utils.ts

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,19 @@ const NanospClickTable = new Map([
5353
])
5454

5555
const StaxClickTable = new Map([
56-
[OutputType.Base, 2],
57-
[OutputType.Multisig, 3],
58-
[OutputType.Token, 3],
59-
[OutputType.BaseAndToken, 3],
60-
[OutputType.MultisigAndToken, 4],
56+
[OutputType.Base, 1],
57+
[OutputType.Multisig, 2],
58+
[OutputType.Token, 2],
59+
[OutputType.BaseAndToken, 2],
60+
[OutputType.MultisigAndToken, 2],
61+
])
62+
63+
const FlexClickTable = new Map([
64+
[OutputType.Base, 1],
65+
[OutputType.Multisig, 2],
66+
[OutputType.Token, 2],
67+
[OutputType.BaseAndToken, 2],
68+
[OutputType.MultisigAndToken, 3],
6169
])
6270

6371
function getOutputClickSize(outputType: OutputType) {
@@ -66,8 +74,8 @@ function getOutputClickSize(outputType: OutputType) {
6674
case 'nanos': return NanosClickTable.get(outputType)!
6775
case 'nanosp':
6876
case 'nanox': return NanospClickTable.get(outputType)!
69-
case 'stax':
70-
case 'flex': return StaxClickTable.get(outputType)!
77+
case 'stax': return StaxClickTable.get(outputType)!
78+
case 'flex': return FlexClickTable.get(outputType)!
7179
default: throw new Error(`Unknown model ${model}`)
7280
}
7381
}
@@ -95,12 +103,16 @@ const STAX_APPROVE_POSITION = { x: 200, y: 515 }
95103
const STAX_REJECT_POSITION = { x: 36, y: 606 }
96104
const STAX_SETTINGS_POSITION = { x: 342, y: 55 }
97105
const STAX_BLIND_SETTING_POSITION = { x: 342, y: 90 }
106+
const STAX_GO_TO_SETTINGS = { x: 36, y: 606 }
107+
const STAX_ACCEPT_RISK_POSITION = { x: 36, y: 606 }
98108

99109
const FLEX_CONTINUE_POSITION = { x: 430, y: 550 }
100110
const FLEX_APPROVE_POSITION = { x: 240, y: 435 }
101111
const FLEX_REJECT_POSITION = { x: 55, y: 530 }
102112
const FLEX_SETTINGS_POSITION = { x: 405, y: 75 }
103113
const FLEX_BLIND_SETTING_POSITION = { x: 405, y: 96 }
114+
const FLEX_GO_TO_SETTINGS = { x: 55, y: 530 }
115+
const FLEX_ACCEPT_RISK_POSITION = { x: 55, y: 530 }
104116

105117
async function touchPosition(pos: Position) {
106118
await sleep(1000)
@@ -110,14 +122,24 @@ async function touchPosition(pos: Position) {
110122
})
111123
}
112124

113-
async function _touch(times: number) {
125+
async function longPress(pos: Position) {
126+
await sleep(1000)
127+
return fetch(`http://localhost:25000/finger`, {
128+
method: 'POST',
129+
body: JSON.stringify({ action: 'press-and-release', x: pos.x, y: pos.y, delay: 3 })
130+
})
131+
}
132+
133+
async function _touch(times: number, approve: boolean = false) {
114134
const model = getModel()
115135
const continuePos = model === 'stax' ? STAX_CONTINUE_POSITION : FLEX_CONTINUE_POSITION
116136
for (let i = 0; i < times; i += 1) {
117137
await touchPosition(continuePos)
118138
}
119-
const approvePos = model === 'stax' ? STAX_APPROVE_POSITION : FLEX_APPROVE_POSITION
120-
await touchPosition(approvePos)
139+
if (approve) {
140+
const approvePos = model === 'stax' ? STAX_APPROVE_POSITION : FLEX_APPROVE_POSITION
141+
await longPress(approvePos)
142+
}
121143
}
122144

123145
export async function staxFlexApproveOnce() {
@@ -129,16 +151,19 @@ export async function staxFlexApproveOnce() {
129151
}
130152

131153
async function touch(outputs: OutputType[], hasExternalInputs: boolean) {
132-
await sleep(1000);
154+
await sleep(3000);
133155
if (hasExternalInputs) {
134156
await staxFlexApproveOnce()
135157
}
136158

159+
_touch(1) // the first review page
160+
await sleep(1000)
161+
137162
for (let index = 0; index < outputs.length; index += 1) {
138163
await _touch(getOutputClickSize(outputs[index]))
139164
}
140165

141-
await _touch(2) // fees
166+
await _touch(1, true) // fees
142167
}
143168

144169
export async function approveTx(outputs: OutputType[], hasExternalInputs: boolean = false) {
@@ -147,7 +172,7 @@ export async function approveTx(outputs: OutputType[], hasExternalInputs: boolea
147172
const isSelfTransfer = outputs.length === 0 && !hasExternalInputs
148173
if (isSelfTransfer) {
149174
if (isStaxOrFlex()) {
150-
await _touch(2)
175+
await _touch(2, true)
151176
} else {
152177
await clickAndApprove(2)
153178
}
@@ -164,7 +189,7 @@ export async function approveTx(outputs: OutputType[], hasExternalInputs: boolea
164189
export async function approveHash() {
165190
if (!needToAutoApprove()) return
166191
if (isStaxOrFlex()) {
167-
return await _touch(3)
192+
return await _touch(2, true)
168193
}
169194
if (getModel() === 'nanos') {
170195
await clickAndApprove(5)
@@ -176,7 +201,9 @@ export async function approveHash() {
176201
export async function approveAddress() {
177202
if (!needToAutoApprove()) return
178203
if (isStaxOrFlex()) {
179-
return await _touch(2)
204+
await _touch(1)
205+
await staxFlexApproveOnce()
206+
return
180207
}
181208
if (getModel() === 'nanos') {
182209
await clickAndApprove(4)
@@ -185,21 +212,32 @@ export async function approveAddress() {
185212
}
186213
}
187214

188-
function isStaxOrFlex(): boolean {
215+
export function isStaxOrFlex(): boolean {
189216
return !getModel().startsWith('nano')
190217
}
191218

192219
export function isNanos(): boolean {
193220
return getModel() === 'nanos'
194221
}
195222

196-
export function skipBlindSigningWarning() {
223+
export async function skipBlindSigningWarning() {
197224
if (!needToAutoApprove()) return
198225
if (isStaxOrFlex()) {
199-
const rejectPos = getModel() === 'stax' ? STAX_REJECT_POSITION : FLEX_REJECT_POSITION
200-
touchPosition(rejectPos)
226+
await sleep(3000)
227+
const goToSettings = getModel() === 'stax' ? STAX_GO_TO_SETTINGS : FLEX_GO_TO_SETTINGS
228+
await touchPosition(goToSettings)
229+
} else {
230+
await clickAndApprove(3)
231+
}
232+
}
233+
234+
export async function staxFlexAcceptRisk() {
235+
if (!needToAutoApprove()) return
236+
await sleep(3000)
237+
if (getModel() === 'stax') {
238+
await touchPosition(STAX_ACCEPT_RISK_POSITION)
201239
} else {
202-
clickAndApprove(3)
240+
await touchPosition(FLEX_ACCEPT_RISK_POSITION)
203241
}
204242
}
205243

js/test/wallet.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ALPH_TOKEN_ID, Address, DUST_AMOUNT, NodeProvider, ONE_ALPH, binToHex,
33
import { getSigner, mintToken, transfer } from '@alephium/web3-test'
44
import { PrivateKeyWallet } from '@alephium/web3-wallet'
55
import blake from 'blakejs'
6-
import { approveAddress, approveHash, approveTx, createTransport, enableBlindSigning, getRandomInt, isNanos, needToAutoApprove, OutputType, skipBlindSigningWarning, staxFlexApproveOnce } from './utils'
6+
import { approveAddress, approveHash, approveTx, createTransport, enableBlindSigning, getRandomInt, isNanos, isStaxOrFlex, needToAutoApprove, OutputType, skipBlindSigningWarning, staxFlexAcceptRisk, staxFlexApproveOnce } from './utils'
77
import { TokenMetadata } from '../src/types'
88
import { randomBytes } from 'crypto'
99
import { merkleTokens, tokenMerkleProofs } from '../src/merkle'
@@ -547,7 +547,11 @@ describe('ledger wallet', () => {
547547

548548
await enableBlindSigning()
549549
if (needToAutoApprove()) {
550-
staxFlexApproveOnce().then(() => approveTx([]))
550+
if (isStaxOrFlex()) {
551+
staxFlexAcceptRisk().then(() => approveTx([]))
552+
} else {
553+
approveTx([])
554+
}
551555
} else {
552556
// waiting for blind signing setting to be enabled
553557
await sleep(20000)

0 commit comments

Comments
 (0)