@consensys/bitcoin-snap
Advanced tools
Comparing version 0.3.7-dev-3831ac7-20220929 to 0.3.7-dev-5087529-20221116
{ | ||
"name": "@consensys/bitcoin-snap", | ||
"version": "0.3.7-dev-3831ac7-20220929", | ||
"version": "0.3.7-dev-5087529-20221116", | ||
"license": "(Apache-2.0 OR MIT)", | ||
@@ -19,5 +19,5 @@ "main": "src/index.ts", | ||
"prettier": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", | ||
"lint": "eslint . --max-warnings 0", | ||
"lint": "eslint . --max-warnings 12", | ||
"test": "yarn run test:unit", | ||
"test:unit": "nyc --check-coverage --statements 80 --branches 50 --functions 75 --lines 80 mocha --colors -r ts-node/register \"test/**/**.test.ts\"" | ||
"test:unit": "nyc --check-coverage --statements 64 --branches 44 --functions 75 --lines 64 mocha --colors -r ts-node/register \"test/**/**.test.ts\"" | ||
}, | ||
@@ -24,0 +24,0 @@ "keywords": [], |
{ | ||
"version": "0.3.7-dev-3831ac7-20220929", | ||
"version": "0.3.7-dev-5087529-20221116", | ||
"description": "Bitcoin Snap", | ||
@@ -10,3 +10,3 @@ "proposedName": "@consensys/bitcoin-snap", | ||
"source": { | ||
"shasum": "31/qEM85chUs+JvJA0AShZEjYrAjsvMXSl28KUqSXKc=", | ||
"shasum": "gvq70QZOjGM9NIKltDOjeHBzDY7fecax9WXoyhpI++c=", | ||
"location": { | ||
@@ -23,4 +23,10 @@ "npm": { | ||
"endowment:network-access": {}, | ||
"snap_getBip44Entropy_0": {}, | ||
"snap_getBip44Entropy_1": {}, | ||
"snap_getBip44Entropy": [ | ||
{ | ||
"coinType": 0 | ||
}, | ||
{ | ||
"coinType": 1 | ||
} | ||
], | ||
"snap_manageState": {} | ||
@@ -27,0 +33,0 @@ }, |
@@ -53,3 +53,2 @@ import Mutex from 'async-mutex/lib/Mutex'; | ||
export interface SendTransactionRequestParams extends BaseRequestParams { | ||
@@ -73,2 +72,2 @@ receiverAddress: string; | ||
export interface GetFeeRateParams extends BaseRequestParams {} | ||
export type GetFeeRateParams = BaseRequestParams; |
@@ -19,3 +19,6 @@ import { getNetwork } from './snapUtils'; | ||
const bip44Node = await wallet.request({ | ||
method: `snap_getBip44Entropy_${coinType}`, | ||
method: 'snap_getBip44Entropy', | ||
params: { | ||
coinType: coinType, | ||
}, | ||
}); | ||
@@ -43,5 +46,7 @@ | ||
const rootNode = bip32.fromPrivateKey(privateKeyBuffer, chainCodeBuffer, network); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
// ignore checking since no function to set depth for node | ||
rootNode.__DEPTH = keyDeriver.depth; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
@@ -48,0 +53,0 @@ // ignore checking since no function to set index for node |
@@ -28,2 +28,12 @@ import { JsonBIP44CoinTypeNode } from '@metamask/key-tree'; | ||
export const getBip44EntropyStub = async (...args: unknown[]) => { | ||
if (args?.[0]?.['coinType'] === 1) { | ||
return bip44Entropy; | ||
} else if (args?.[0]?.['coinType'] === 0) { | ||
return bip44EntropyMain; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
export const txn1: Transaction = { | ||
@@ -37,2 +47,4 @@ transactionHash: 'bb3c6df1cd39f66899e8ca8b75d0c0bbe53918a3e8dabb58c38677cb70d133db', | ||
value: 3000, | ||
block_id: -1, | ||
state: 'Pending', | ||
}; | ||
@@ -48,2 +60,4 @@ | ||
value: 5000, | ||
block_id: -1, | ||
state: 'Pending', | ||
}; | ||
@@ -59,2 +73,4 @@ | ||
value: 1000, | ||
block_id: -1, | ||
state: 'Pending', | ||
}; | ||
@@ -61,0 +77,0 @@ |
@@ -10,3 +10,3 @@ import chai, { expect } from 'chai'; | ||
import { ApiParams, CreateAccountRequestParams } from '../../src/types/snapApi'; | ||
import { account1, bip44Entropy } from '../constants.test'; | ||
import { account1, getBip44EntropyStub } from '../constants.test'; | ||
import { getAddressKeyDeriver } from '../../src/utils/keyPair'; | ||
@@ -37,3 +37,3 @@ import { Mutex } from 'async-mutex'; | ||
beforeEach(async function () { | ||
walletStub.rpcStubs.snap_getBip44Entropy_1.resolves(bip44Entropy); | ||
walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); | ||
apiParams.keyDeriver = await getAddressKeyDeriver(walletStub, BitcoinNetwork.Test); | ||
@@ -40,0 +40,0 @@ walletStub.rpcStubs.snap_manageState.resolves(state); |
@@ -6,3 +6,3 @@ import chai, { expect } from 'chai'; | ||
import { SnapState } from '../../src/types/snapState'; | ||
import { account1, account2, bip44Entropy, getFeeResp, getStatusResp } from '../constants.test'; | ||
import { account1, account2, getBip44EntropyStub, getFeeResp, getStatusResp } from '../constants.test'; | ||
import { getAddressKeyDeriver } from '../../src/utils/keyPair'; | ||
@@ -36,3 +36,3 @@ import { Mutex } from 'async-mutex'; | ||
beforeEach(async function () { | ||
walletStub.rpcStubs.snap_getBip44Entropy_1.resolves(bip44Entropy); | ||
walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); | ||
apiParams.keyDeriver = await getAddressKeyDeriver(walletStub, BitcoinNetwork.Test); | ||
@@ -39,0 +39,0 @@ sandbox.stub(explorer.BlockChair.prototype, 'getStatus').callsFake(async () => { |
@@ -9,3 +9,3 @@ import chai, { expect } from 'chai'; | ||
import { ApiParams } from '../../src/types/snapApi'; | ||
import { account1, unfoundUserAddress, bip44Entropy } from '../constants.test'; | ||
import { account1, unfoundUserAddress, getBip44EntropyStub } from '../constants.test'; | ||
import { getAddressKeyDeriver } from '../../src/utils/keyPair'; | ||
@@ -38,3 +38,3 @@ import { Mutex } from 'async-mutex'; | ||
beforeEach(async function () { | ||
walletStub.rpcStubs.snap_getBip44Entropy_1.resolves(bip44Entropy); | ||
walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); | ||
apiParams.keyDeriver = await getAddressKeyDeriver(walletStub, BitcoinNetwork.Test); | ||
@@ -41,0 +41,0 @@ walletStub.rpcStubs.snap_manageState.resolves(state); |
@@ -9,3 +9,3 @@ import chai, { expect } from 'chai'; | ||
import { Mutex } from 'async-mutex'; | ||
import { account1, bip44Entropy } from '../constants.test'; | ||
import { account1, getBip44EntropyStub } from '../constants.test'; | ||
import { getAddressKeyDeriver } from '../../src/utils/keyPair'; | ||
@@ -28,3 +28,3 @@ import { BitcoinNetwork } from '../../src/explorer/interface'; | ||
beforeEach(async function () { | ||
walletStub.rpcStubs.snap_getBip44Entropy_1.resolves(bip44Entropy); | ||
walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); | ||
apiParams.keyDeriver = await getAddressKeyDeriver(walletStub, BitcoinNetwork.Test); | ||
@@ -31,0 +31,0 @@ walletStub.rpcStubs.snap_manageState.resolves(state); |
@@ -7,3 +7,3 @@ import chai, { expect } from 'chai'; | ||
import { BITCOIN_TESTNET_TOKEN, BITCOIN_TESTNET_NETWORK } from '../../src/utils/constants'; | ||
import { account1, bip44Entropy, getTokenBalanceResponse, wrongAccount } from '../constants.test'; | ||
import { account1, getBip44EntropyStub, getTokenBalanceResponse, wrongAccount } from '../constants.test'; | ||
import { getTokenBalance } from '../../src/getTokenBalance'; | ||
@@ -41,3 +41,3 @@ import { Mutex } from 'async-mutex'; | ||
beforeEach(async function () { | ||
walletStub.rpcStubs.snap_getBip44Entropy_1.resolves(bip44Entropy); | ||
walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); | ||
apiParams.keyDeriver = await getAddressKeyDeriver(walletStub, BitcoinNetwork.Test); | ||
@@ -44,0 +44,0 @@ sandbox.stub(explorer.BlockChair.prototype, 'getTokenBalance').callsFake(async () => { |
@@ -7,3 +7,3 @@ import chai, { expect } from 'chai'; | ||
import { BITCOIN_TESTNET_TOKEN, BITCOIN_TESTNET_NETWORK } from '../../src/utils/constants'; | ||
import { bip44Entropy, txn1, txn2, txn3 } from '../constants.test'; | ||
import { getBip44EntropyStub, txn1, txn2, txn3 } from '../constants.test'; | ||
import { getTransactions } from '../../src/getTransactions'; | ||
@@ -37,3 +37,3 @@ import { Mutex } from 'async-mutex'; | ||
beforeEach(async function () { | ||
walletStub.rpcStubs.snap_getBip44Entropy_1.resolves(bip44Entropy); | ||
walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); | ||
apiParams.keyDeriver = await getAddressKeyDeriver(walletStub, BitcoinNetwork.Test); | ||
@@ -43,2 +43,5 @@ sandbox.stub(explorer.BlockChair.prototype, 'getTransactionsFromBlockChair').callsFake(async () => { | ||
}); | ||
sandbox.stub(explorer.BlockChair.prototype, 'getCurrentBlock').callsFake(async () => { | ||
return 1000000; | ||
}); | ||
walletStub.rpcStubs.snap_manageState.resolves(state); | ||
@@ -45,0 +48,0 @@ }); |
@@ -7,3 +7,3 @@ import chai, { expect } from 'chai'; | ||
import { sendTransaction } from '../../src/sendTransaction'; | ||
import { account1, bip44Entropy, broadcastTransactionResp, getStatusResp } from '../constants.test'; | ||
import { account1, broadcastTransactionResp, getBip44EntropyStub, getStatusResp } from '../constants.test'; | ||
import { getAddressKeyDeriver } from '../../src/utils/keyPair'; | ||
@@ -36,3 +36,3 @@ import { Mutex } from 'async-mutex'; | ||
beforeEach(async function () { | ||
walletStub.rpcStubs.snap_getBip44Entropy_1.resolves(bip44Entropy); | ||
walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); | ||
apiParams.keyDeriver = await getAddressKeyDeriver(walletStub, BitcoinNetwork.Test); | ||
@@ -39,0 +39,0 @@ sandbox.stub(explorer.BlockChair.prototype, 'getStatus').callsFake(async () => { |
import chai, { expect } from 'chai'; | ||
import sinonChai from 'sinon-chai'; | ||
import { bip44Entropy, bip44EntropyMain } from '../constants.test'; | ||
import { getBip44EntropyStub } from '../constants.test'; | ||
import { WalletMock } from '../wallet.mock.test'; | ||
@@ -22,4 +22,3 @@ import { SnapState } from '../../src/types/snapState'; | ||
beforeEach(async function () { | ||
walletStub.rpcStubs.snap_getBip44Entropy_0.resolves(bip44EntropyMain); | ||
walletStub.rpcStubs.snap_getBip44Entropy_1.resolves(bip44Entropy); | ||
walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); | ||
saveMutex = new Mutex(); | ||
@@ -26,0 +25,0 @@ walletStub.rpcStubs.snap_manageState.resolves(state); |
import chai, { expect } from 'chai'; | ||
import sinonChai from 'sinon-chai'; | ||
import { getKeyPair, getAddressKeyDeriver } from '../../src/utils/keyPair'; | ||
import { account0KeyPair, account1, bip44Entropy } from '../constants.test'; | ||
import { account0KeyPair, account1, getBip44EntropyStub } from '../constants.test'; | ||
import { WalletMock } from '../wallet.mock.test'; | ||
@@ -35,3 +35,3 @@ import { BitcoinNetwork } from '../../src/explorer/interface'; | ||
beforeEach(async function () { | ||
walletStub.rpcStubs.snap_getBip44Entropy_1.resolves(bip44Entropy); | ||
walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); | ||
apiParams.keyDeriver = await getAddressKeyDeriver(walletStub, BitcoinNetwork.Test); | ||
@@ -46,3 +46,3 @@ }); | ||
const result = await getKeyPair(apiParams.keyDeriver, 0, BitcoinNetwork.Test); | ||
expect(walletStub.rpcStubs.snap_getBip44Entropy_1).to.have.been.calledOnce; | ||
expect(walletStub.rpcStubs.snap_getBip44Entropy).to.have.been.calledOnce; | ||
expect(result.privateKey).to.be.eql(account0KeyPair.privateKey); | ||
@@ -54,3 +54,3 @@ expect(result.publicKey).to.be.eql(account0KeyPair.publicKey); | ||
const result = await getKeyPair(apiParams.keyDeriver, 1, BitcoinNetwork.Test); | ||
expect(walletStub.rpcStubs.snap_getBip44Entropy_1).to.have.been.calledOnce; | ||
expect(walletStub.rpcStubs.snap_getBip44Entropy).to.have.been.calledOnce; | ||
expect(result.privateKey).to.be.not.eql(account0KeyPair.privateKey); | ||
@@ -57,0 +57,0 @@ expect(result.publicKey).to.be.not.eql(account0KeyPair.publicKey); |
import sinon from 'sinon'; | ||
export interface Wallet { | ||
registerRpcMessageHandler: (fn) => unknown; | ||
request(options: { method: string; params?: unknown[] }): unknown; | ||
request(options: { method: string; params?: { [key: string]: unknown } | unknown[] }): unknown; | ||
} | ||
@@ -13,4 +13,3 @@ | ||
public readonly rpcStubs = { | ||
snap_getBip44Entropy_0: sinon.stub(), | ||
snap_getBip44Entropy_1: sinon.stub(), | ||
snap_getBip44Entropy: sinon.stub(), | ||
snap_confirm: sinon.stub(), | ||
@@ -24,6 +23,10 @@ snap_manageState: sinon.stub(), | ||
*/ | ||
public request(args: { method: string; params: unknown[] }): unknown { | ||
const { method, params = [] } = args; | ||
public request(args: { method: string; params: { [key: string]: unknown } | unknown[] }): unknown { | ||
const { method, params } = args; | ||
if (Object.hasOwnProperty.call(this.rpcStubs, method)) { | ||
return this.rpcStubs[method](...params); | ||
if (Array.isArray(params)) { | ||
return this.rpcStubs[method](...params); | ||
} else { | ||
return this.rpcStubs[method](params); | ||
} | ||
} | ||
@@ -30,0 +33,0 @@ return this.requestStub(args); |
Sorry, the diff of this file is too big to display
890088
26183