Guest User

Untitled

a guest
Apr 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import { Transaction, Keypair, xdr, Network } from 'stellar-sdk'
  2.  
  3. // import Transport from "@ledgerhq/hw-transport-node-hid"
  4. import Transport from '@ledgerhq/hw-transport-u2f'
  5. import Str from '@ledgerhq/hw-app-str'
  6.  
  7. export class StellarUtils {
  8. constructor() {
  9. Network.use(new Network(environment.stellar.stellarPassphrase))
  10. }
  11.  
  12. async getStrPublicKey(): Promise<string> {
  13. const isSupported = await this.isSupported()
  14. if (isSupported) {
  15. const transport = await Transport.create()
  16. const str = new Str(transport)
  17. const result = await str.getPublicKey('44'/148'/0'')
  18. transport.close()
  19. return result.publicKey
  20. } else { throw new Error('Not supported') }
  21. }
  22.  
  23. async signTransaction(transactionXDR: string) {
  24. const isSupported = await this.isSupported()
  25. if (isSupported) {
  26. const transport = await Transport.create()
  27. const str = new Str(transport)
  28. const tx = new Transaction(transactionXDR)
  29. const result = await str.signTransaction('44'/148'/0'', tx.signatureBase())
  30. const keyPair = Keypair.fromPublicKey(await this.getStrPublicKey())
  31. const hint = (keyPair as any).signatureHint()
  32. const decorated = new (xdr as any).DecoratedSignature({ hint: hint, signature: result.signature });
  33. (tx as any).signatures.push(decorated)
  34. return tx
  35. } else { throw new Error('Not supported') }
  36. }
  37. }
Add Comment
Please, Sign In to add comment