forked from XDeFi-tech/chains-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from GridPlus/feat/move-reusable-parts-to-core
feat(core): move reusable logic from btc signer to core
- Loading branch information
Showing
11 changed files
with
452 additions
and
110 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
import { PrivateKeySigner } from './private-key.signer'; | ||
import LedgerSigner from './ledger.signer'; | ||
import TrezorSigner from './trezor.signer'; | ||
import LatticeSigner from './lattice.signer'; | ||
import { SeedPhraseSigner } from './seed-phrase.signer'; | ||
|
||
export { PrivateKeySigner, LedgerSigner, TrezorSigner, SeedPhraseSigner }; | ||
export { | ||
PrivateKeySigner, | ||
LedgerSigner, | ||
TrezorSigner, | ||
LatticeSigner, | ||
SeedPhraseSigner, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './signer.provider'; | ||
export * from './trezor.provider'; | ||
export * from './lattice.provider'; | ||
export * from './interfaces'; |
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,10 @@ | ||
import { LatticeProvider } from './lattice.provider'; | ||
|
||
const testDerivationPath = "m/84'/0'/0'/0/0"; | ||
|
||
describe('lattice.provider', () => { | ||
it('converts derivation string to int array', () => { | ||
const result = LatticeProvider.convertDerivationPathToArray(testDerivationPath); | ||
expect(result).toEqual([2147483732, 2147483648, 2147483648, 0, 0]); | ||
}); | ||
}); |
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,90 @@ | ||
import { setup } from 'gridplus-sdk'; | ||
|
||
import { Injectable } from '../../common'; | ||
import type { MsgData } from '../msg'; | ||
|
||
import { Provider } from './signer.provider'; | ||
|
||
const HARDENED_OFFSET = 0x80000000; // Hardened offset | ||
|
||
@Injectable() | ||
export class LatticeProvider extends Provider { | ||
private static instance: LatticeProvider; | ||
public initialized = false; | ||
private client!: string; | ||
public isPaired: boolean; | ||
|
||
constructor(client: string, isPaired: boolean) { | ||
super(); | ||
this.client = client; | ||
this.isPaired = isPaired; | ||
this.initialized = true; | ||
} | ||
|
||
static async create({ | ||
deviceId, | ||
password, | ||
name, | ||
}: { | ||
deviceId: string; | ||
password: string; | ||
name: string; | ||
}): Promise<LatticeProvider> { | ||
let clientData = ''; | ||
|
||
const getStoredClient = () => clientData; | ||
const setStoredClient = (newClientData: string | null) => { | ||
clientData = newClientData ?? ''; | ||
}; | ||
|
||
const isPaired = await setup({ | ||
deviceId, | ||
password, | ||
name, | ||
getStoredClient, | ||
setStoredClient, | ||
}); | ||
|
||
return new LatticeProvider(clientData, isPaired); | ||
} | ||
|
||
static convertDerivationPathToArray(path: string): number[] { | ||
// Validate input path | ||
if (!path.startsWith('m')) { | ||
throw new Error('Invalid derivation path. Must start with "m".'); | ||
} | ||
|
||
// Split the path into components, skipping the first element ('m') | ||
const components = path.split('/').slice(1); | ||
|
||
// Convert each component to the corresponding number | ||
const result = components.map((component) => { | ||
// Check if the component is hardened (ends with a single quote) | ||
const isHardened = component.endsWith("'"); | ||
|
||
// Parse the number, removing the single quote if it is hardened | ||
const number = Number.parseInt(isHardened ? component.slice(0, -1) : component); | ||
|
||
// Add the hardened offset if necessary | ||
return isHardened ? HARDENED_OFFSET + number : number; | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
async getAddress(derivation: string): Promise<string> { | ||
throw new Error('Should be implemented on specific signer level.'); | ||
} | ||
|
||
async sign(msg: MsgData, derivation: string) { | ||
throw new Error('Should be implemented on specific signer level.'); | ||
} | ||
|
||
verifyAddress(address: string): boolean { | ||
throw new Error('Should be implemented on specific signer level.'); | ||
} | ||
} | ||
|
||
export const LatticeConst = { | ||
HARDENED_OFFSET, | ||
}; |
Oops, something went wrong.