-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Start experimenting with GridPlus * Work on pairing process * Fix address scanning * Hexlify signing params * Fixes to signature logic * Add basic SDK typing and other improvements * Add function to get credentials for saving * Run formatting * Fix issue with empty buffer being returned for v * Fix message signing * Improve error handling * Move condition * Add some tests and mock * Improve some typing and add more tests * Attempt to simplify client management * Add more tests * Add timeout to popup * Add test for timeout * Revert popup timeout and detect closing the popup instead * Fix test * Fix some PR comments * Fix more PR comments * Simplify slightly * Add missing test * Small PR comments * Bump dep and version
- Loading branch information
1 parent
6124175
commit a6bff71
Showing
15 changed files
with
1,107 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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
101 changes: 101 additions & 0 deletions
101
src/implementations/deterministic/__mocks__/gridplus-sdk.ts
This file contains 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,101 @@ | ||
import { arrayify, splitSignature } from '@ethersproject/bytes'; | ||
import { HDNode } from '@ethersproject/hdnode'; | ||
import type { Transaction } from '@ethersproject/transactions'; | ||
import { parse } from '@ethersproject/transactions'; | ||
import { Wallet } from '@ethersproject/wallet'; | ||
import type { | ||
AddressesOpts, | ||
SignMessageOpts, | ||
SignOpts, | ||
SignResult, | ||
SignTxOpts | ||
} from 'gridplus-sdk'; | ||
|
||
import { fMnemonicPhrase } from '../../../../.jest/__fixtures__'; | ||
import { getPathPrefix, stripHexPrefix, toChecksumAddress } from '../../../utils'; | ||
|
||
const hdNode = HDNode.fromMnemonic(fMnemonicPhrase); | ||
|
||
const convertPathToString = (path: number[]): string => | ||
path | ||
.map((p) => { | ||
const withoutHardening = p - 0x80000000; | ||
const index = withoutHardening >= 0 ? Math.min(p, withoutHardening) : p; | ||
const isHardened = index === withoutHardening; | ||
return `${index}${isHardened ? "'" : ''}`; | ||
}) | ||
.join('/'); | ||
|
||
export class Client { | ||
isPaired = false; | ||
hasActiveWallet = jest.fn().mockReturnValue(true); | ||
connect = jest | ||
.fn() | ||
.mockImplementation( | ||
(_deviceID: string, callback: (err: Error | null, isPaired: boolean) => void) => { | ||
this.isPaired = true; | ||
callback(null, true); | ||
} | ||
); | ||
sign = jest | ||
.fn() | ||
.mockImplementation( | ||
async (opts: SignOpts, callback: (err: Error | null, data: SignResult) => void) => { | ||
const path = convertPathToString(opts.data.signerPath); | ||
const childNode = hdNode.derivePath(path); | ||
const wallet = new Wallet(childNode.privateKey); | ||
if (opts.currency === 'ETH') { | ||
const { signerPath, chainId, ...transaction } = opts.data as SignTxOpts; | ||
|
||
const isEIP1559 = | ||
transaction.maxFeePerGas !== undefined && | ||
transaction.maxPriorityFeePerGas !== undefined; | ||
|
||
const signedTransaction = await wallet.signTransaction({ | ||
...transaction, | ||
chainId: parseInt((chainId as unknown) as string, 16), | ||
type: isEIP1559 ? 2 : 0 | ||
}); | ||
const { v, r, s } = parse(signedTransaction) as Required<Transaction>; | ||
callback(null, { | ||
sig: { | ||
// eslint-disable-next-line no-restricted-globals | ||
v: v === 0 ? Buffer.from([]) : Buffer.from([v]), | ||
r: stripHexPrefix(r), | ||
s: stripHexPrefix(s) | ||
} | ||
}); | ||
} else if (opts.currency === 'ETH_MSG') { | ||
const signMessageOpts = opts.data as SignMessageOpts; | ||
const signature = await wallet.signMessage(arrayify(signMessageOpts.payload)); | ||
const { v, r, s } = splitSignature(signature); | ||
|
||
callback(null, { | ||
sig: { | ||
// eslint-disable-next-line no-restricted-globals | ||
v: v === 0 ? Buffer.from([]) : Buffer.from([v]), | ||
r: stripHexPrefix(r), | ||
s: stripHexPrefix(s) | ||
} | ||
}); | ||
} | ||
} | ||
); | ||
getAddresses = jest | ||
.fn() | ||
.mockImplementation( | ||
(opts: AddressesOpts, callback: (err: Error | null, data: string[]) => void) => { | ||
const path = convertPathToString(opts.startPath); | ||
const indices = path.split('/'); | ||
const offset = parseInt(indices[indices.length - 1]); | ||
|
||
const masterNode = hdNode.derivePath(getPathPrefix(path)); | ||
const result = new Array(opts.n).fill(undefined).map((_, i) => { | ||
const index = offset + i; | ||
const node = masterNode.derivePath(index.toString(10)); | ||
return toChecksumAddress(node.address); | ||
}); | ||
callback(null, result); | ||
} | ||
); | ||
} |
This file contains 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 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.