Skip to content

Commit

Permalink
types and package.json fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bergarces committed Mar 7, 2023
1 parent 304ec9e commit 62ca07a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
20 changes: 9 additions & 11 deletions ledger-iframe-bridge.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const { BRIDGE_URL } = require('./ledger-keyring')

const CONNECTION_EVENT = 'ledger-connection-change'

class LedgerIframeBridge {
init () {
init (bridgeUrl) {
this.iframeLoaded = false
this._setupIframe()
this._setupIframe(bridgeUrl)

this.currentMessageId = 0
this.messageCallbacks = {}
this._setupListener()
this._setupListener(bridgeUrl)

return Promise.resolve()
}
Expand Down Expand Up @@ -100,9 +98,9 @@ class LedgerIframeBridge {
})
}

_setupIframe () {
_setupIframe (bridgeUrl) {
this.iframe = document.createElement('iframe')
this.iframe.src = BRIDGE_URL
this.iframe.src = bridgeUrl
this.iframe.allow = `hid 'src'`
this.iframe.onload = async () => {
// If the ledger live preference was set before the iframe is loaded,
Expand All @@ -124,15 +122,15 @@ class LedgerIframeBridge {
document.head.appendChild(this.iframe)
}

_getOrigin () {
const tmp = this.bridgeUrl.split('/')
_getOrigin (bridgeUrl) {
const tmp = bridgeUrl.split('/')
tmp.splice(-1, 1)
return tmp.join('/')
}

_setupListener () {
_setupListener (bridgeUrl) {
this._eventListener = ({ origin, data }) => {
if (origin !== this._getOrigin()) {
if (origin !== this._getOrigin(bridgeUrl)) {
return false
}

Expand Down
5 changes: 4 additions & 1 deletion ledger-keyring.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class LedgerKeyring extends EventEmitter {
constructor ({ bridge } = {}) {
super()
this.accountDetails = {}
this.bridgeUrl = null
this.type = type
this.page = 0
this.perPage = 5
Expand All @@ -44,7 +45,7 @@ class LedgerKeyring extends EventEmitter {
* @returns {Promise<void>}
*/
init () {
return this.bridge.init()
return this.bridge.init(this.bridgeUrl || BRIDGE_URL)
}

/**
Expand All @@ -61,12 +62,14 @@ class LedgerKeyring extends EventEmitter {
hdPath: this.hdPath,
accounts: this.accounts,
accountDetails: this.accountDetails,
bridgeUrl: this.bridgeUrl,
implementFullBIP44: false,
})
}

deserialize (opts = {}) {
this.hdPath = opts.hdPath || hdPathString
this.bridgeUrl = opts.bridgeUrl || BRIDGE_URL
this.accounts = opts.accounts || []
this.accountDetails = opts.accountDetails || {}
if (!opts.accountDetails) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"files": [
"index.js",
"ledger-keyring.js",
"ledger-bridge-iframe.js",
"ledger-iframe-bridge.js",
"types/index.d.ts"
],
"engines": {
Expand Down
2 changes: 2 additions & 0 deletions test/ledger-keyring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ describe('LedgerKeyring', function () {
it('serializes an instance', function (done) {
keyring.serialize()
.then((output) => {
assert.equal(output.bridgeUrl, 'https://metamask.github.io/eth-ledger-bridge-keyring')
assert.equal(output.hdPath, `m/44'/60'/0'`)
assert.equal(Array.isArray(output.accounts), true)
assert.equal(output.accounts.length, 0)
Expand Down Expand Up @@ -173,6 +174,7 @@ describe('LedgerKeyring', function () {
return keyring.serialize()
}).then((serialized) => {
assert.equal(serialized.accounts.length, 1, 'restores 1 account')
assert.equal(serialized.bridgeUrl, 'https://metamask.github.io/eth-ledger-bridge-keyring', 'restores bridgeUrl')
assert.equal(serialized.hdPath, someHdPath, 'restores hdPath')
assert.deepEqual(serialized.accountDetails, accountDetails, 'restores accountDetails')
})
Expand Down
20 changes: 10 additions & 10 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
export type GetPublicKeyPayload = { hdPath: string };
export type GetPublicKeyParams = { hdPath: string };
export type GetPublicKeyResponse = {
publicKey: string;
address: string;
chainCode?: string;
};

export type LedgerSignTransactionPayload = { hdPath: string; rawTxHex: string };
export type LedgerSignTransactionParams = { hdPath: string; tx: string };
export type LedgerSignTransactionResponse = {
s: string;
v: string;
r: string;
};

export type LedgerSignMessagePayload = { hdPath: string; message: string };
export type LedgerSignMessageParams = { hdPath: string; message: string };
export type LedgerSignMessageResponse = {
v: number;
s: string;
r: string;
};

export type LedgerSignTypedDataPayload = {
export type LedgerSignTypedDataParams = {
hdPath: string;
domainSeparatorHex: string;
hashStructMessageHex: string;
Expand All @@ -37,19 +37,19 @@ export interface LedgerBridge {

attemptMakeApp(): Promise<boolean>;

getPublicKey(
payload: GetPublicKeyPayload
): Promise<GetPublicKeyResponse>;
updateTransportMethod(transportType: string): Promise<boolean>;

getPublicKey(params: GetPublicKeyParams): Promise<GetPublicKeyResponse>;

deviceSignTransaction(
payload: LedgerSignTransactionPayload
params: LedgerSignTransactionParams
): Promise<LedgerSignTransactionResponse>;

deviceSignMessage(
payload: LedgerSignMessagePayload
params: LedgerSignMessageParams
): Promise<LedgerSignMessageResponse>;

deviceSignTypedData(
payload: LedgerSignTypedDataPayload
params: LedgerSignTypedDataParams
): Promise<LedgerSignTypedDataResponse>;
}

0 comments on commit 62ca07a

Please sign in to comment.