-
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.
feat(XDEFI-5747): Add Tron ledger signer (#111)
- Loading branch information
Showing
8 changed files
with
178 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Msg } from '@xdefi-tech/chains-core'; | ||
|
||
import { TronProvider } from '../chain.provider'; | ||
import { ChainDataSource } from '../datasource'; | ||
import { TRON_MANIFEST } from '../manifests'; | ||
import { ChainMsg, MsgBody } from '../msg'; | ||
|
||
import LedgerSigner from './ledger.signer'; | ||
jest.mock('@ledgerhq/hw-transport-webhid', () => ({ | ||
create: jest.fn().mockResolvedValue({ | ||
close: jest.fn().mockImplementation(), | ||
}), | ||
})); | ||
|
||
jest.mock('@ledgerhq/hw-app-trx', () => { | ||
return jest.fn().mockImplementation(() => ({ | ||
getAddress: jest.fn().mockResolvedValue({ | ||
address: 'TSDmgg8m3AfNniTzz4dyWN44fkGd7otZ4C', | ||
}), | ||
signTransactionHash: jest.fn().mockResolvedValue('SIGNEDTX'), | ||
})); | ||
}); | ||
|
||
describe('ledger.signer', () => { | ||
let signer: LedgerSigner; | ||
let derivationPath: string; | ||
let provider: TronProvider; | ||
let txInput: MsgBody; | ||
let message: Msg; | ||
|
||
beforeEach(() => { | ||
signer = new LedgerSigner(); | ||
|
||
provider = new TronProvider(new ChainDataSource(TRON_MANIFEST)); | ||
derivationPath = "m/44'/195'/0'/0/0"; | ||
|
||
txInput = { | ||
from: 'TSDmgg8m3AfNniTzz4dyWN44fkGd7otZ4C', | ||
to: 'TN4JsVEuLVBG9Ru7YSjDxkTdoRTychnJkH', | ||
amount: '0.000001', | ||
}; | ||
|
||
message = provider.createMsg(txInput); | ||
}); | ||
|
||
it('should get an address from the ledger device', async () => { | ||
expect(await signer.getAddress(derivationPath)).toBe(txInput.from); | ||
}); | ||
|
||
it('should sign a transaction using a ledger device', async () => { | ||
await signer.sign(message as ChainMsg, derivationPath); | ||
|
||
expect(message.signedTransaction).toEqual('SIGNEDTX'); | ||
}); | ||
|
||
it('should return true for a valid address', () => { | ||
expect(signer.verifyAddress(txInput.from, TRON_MANIFEST)).toBe(true); | ||
}); | ||
|
||
it('should return false for an invalid address', () => { | ||
expect(signer.verifyAddress('invalid-address', TRON_MANIFEST)).toBe(false); | ||
}); | ||
|
||
it('should fail if private key is requested', async () => { | ||
expect(signer.getPrivateKey(derivationPath)).rejects.toThrowError(); | ||
}); | ||
}); |
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,53 @@ | ||
import Transport from '@ledgerhq/hw-transport-webhid'; | ||
import Trx from '@ledgerhq/hw-app-trx'; | ||
import { Chain, Signer, SignerDecorator } from '@xdefi-tech/chains-core'; | ||
import TronWeb from 'tronweb'; | ||
|
||
import { ChainMsg } from '../msg'; | ||
|
||
@SignerDecorator(Signer.SignerType.LEDGER) | ||
export class LedgerSigner extends Signer.Provider { | ||
verifyAddress(address: string, manifest: Chain.Manifest): boolean { | ||
const tronWeb = new TronWeb({ | ||
fullHost: manifest.rpcURL, | ||
}); | ||
|
||
return tronWeb.isAddress(address); | ||
} | ||
|
||
async getAddress(derivation: string): Promise<string> { | ||
const transport = await Transport.create(); | ||
try { | ||
const trx = new Trx(transport); | ||
|
||
const address = await trx.getAddress(derivation); | ||
|
||
return address.address; | ||
} catch (e) { | ||
throw e; | ||
} finally { | ||
await transport.close(); | ||
} | ||
} | ||
|
||
async sign(msg: ChainMsg, derivation: string) { | ||
const transport = await Transport.create(); | ||
try { | ||
const trx = new Trx(transport); | ||
const tx = await msg.buildTx(); | ||
|
||
const signedTx = await trx.signTransactionHash( | ||
derivation, | ||
tx.raw_data_hex | ||
); | ||
|
||
msg.sign(signedTx); | ||
} catch (e) { | ||
throw e; | ||
} finally { | ||
await transport.close(); | ||
} | ||
} | ||
} | ||
|
||
export default LedgerSigner; |
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
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