Skip to content

Commit

Permalink
Merge pull request #403 from enkryptcom/develop
Browse files Browse the repository at this point in the history
Release: v1.34.0
  • Loading branch information
kvhnuke authored Feb 28, 2024
2 parents ba254ad + c55e290 commit 75347fd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 66 deletions.
3 changes: 1 addition & 2 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@enkryptcom/extension",
"version": "1.33.0",
"version": "1.34.0",
"private": true,
"scripts": {
"zip": "cd dist; zip -r release.zip *;",
Expand Down Expand Up @@ -42,7 +42,6 @@
"bignumber.js": "^9.1.2",
"bip39": "^3.1.0",
"bitcoinjs-lib": "^6.1.5",
"bitcoinjs-message": "^2.2.0",
"chai": "^4.4.1",
"concurrently": "^8.2.2",
"core-js": "^3.35.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { sha256 } from "ethereumjs-util";

const MAGIC_BYTES = Buffer.from("Bitcoin Signed Message:\n");

const varintBufNum = (n: number) => {
let buf;
if (n < 253) {
buf = Buffer.alloc(1);
buf.writeUInt8(n, 0);
} else if (n < 0x10000) {
buf = Buffer.alloc(1 + 2);
buf.writeUInt8(253, 0);
buf.writeUInt16LE(n, 1);
} else if (n < 0x100000000) {
buf = Buffer.alloc(1 + 4);
buf.writeUInt8(254, 0);
buf.writeUInt32LE(n, 1);
} else {
buf = Buffer.alloc(1 + 8);
buf.writeUInt8(255, 0);
buf.writeInt32LE(n & -1, 1);
buf.writeUInt32LE(Math.floor(n / 0x100000000), 5);
}
return buf;
};

export const magicHash = (messageBuffer: Buffer) => {
const prefix1 = varintBufNum(MAGIC_BYTES.length);
const prefix2 = varintBufNum(messageBuffer.length);
const buf = Buffer.concat([prefix1, MAGIC_BYTES, prefix2, messageBuffer]);
return sha256(sha256(buf));
};

export const toCompact = (
i: number,
signature: Uint8Array,
compressed: boolean
) => {
if (!(i === 0 || i === 1 || i === 2 || i === 3)) {
throw new Error("i must be equal to 0, 1, 2, or 3");
}
let val = i + 27 + 4;
if (!compressed) {
val = val - 4;
}
return Buffer.concat([Uint8Array.of(val), Uint8Array.from(signature)]);
};
14 changes: 7 additions & 7 deletions packages/extension/src/providers/bitcoin/ui/libs/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { bufferToHex } from "ethereumjs-util";
import sendUsingInternalMessengers from "@/libs/messenger/internal-messenger";
import { hexToBuffer } from "@enkryptcom/utils";
import { Psbt } from "bitcoinjs-lib";
import { signAsync } from "bitcoinjs-message";
import { BitcoinNetwork, PaymentType } from "../../types/bitcoin-network";
import { EnkryptAccount } from "@enkryptcom/types";
import { signMessageOfBIP322Simple } from "../../libs/bip322-message-sign";
import { magicHash, toCompact } from "../../libs/sign-message-utils";

const PSBTSigner = (account: EnkryptAccount, network: BitcoinNetwork) => {
return {
Expand Down Expand Up @@ -111,10 +111,7 @@ const MessageSigner = (
error: res.error,
});
} else {
const sigBuffer = hexToBuffer(JSON.parse(res.result!)).subarray(
0,
64
);
const sigBuffer = hexToBuffer(JSON.parse(res.result!));
return {
signature: sigBuffer.subarray(0, 64),
recovery: sigBuffer[64],
Expand All @@ -123,9 +120,12 @@ const MessageSigner = (
});
},
};
return signAsync(payload, signer, false).then((res) => {
const mHash = magicHash(payload);
return signer.sign(mHash).then((sig) => {
return {
result: JSON.stringify(res.toString("base64")),
result: JSON.stringify(
toCompact(sig.recovery, sig.signature, true).toString("base64")
),
};
});
}
Expand Down
8 changes: 5 additions & 3 deletions packages/swap/tests/swap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe("Swap", () => {
toToken,
toAddress,
});
expect(quotes?.length).to.be.eq(4);
expect(quotes?.length).to.be.gte(3);
const oneInceQuote = quotes.find(
(q) => q.provider === ProviderName.oneInch
);
Expand All @@ -72,12 +72,14 @@ describe("Swap", () => {
(q) => q.provider === ProviderName.changelly
);
const zeroxQuote = quotes.find((q) => q.provider === ProviderName.zerox);
// const rangoQuote = quotes.find((q) => q.provider === ProviderName.rango);
if (quotes?.length > 3) {
const rangoQuote = quotes.find((q) => q.provider === ProviderName.rango);
expect(rangoQuote!.provider).to.be.eq(ProviderName.rango);
}
expect(zeroxQuote).to.be.eq(undefined);
expect(changellyQuote!.provider).to.be.eq(ProviderName.changelly);
expect(oneInceQuote!.provider).to.be.eq(ProviderName.oneInch);
expect(paraswapQuote!.provider).to.be.eq(ProviderName.paraswap);
// expect(rangoQuote!.provider).to.be.eq(ProviderName.rango);
const swapOneInch = await enkryptSwap.getSwap(oneInceQuote!.quote);
expect(swapOneInch?.fromTokenAmount.toString()).to.be.eq(amount.toString());
expect(swapOneInch?.transactions.length).to.be.eq(2);
Expand Down
58 changes: 4 additions & 54 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2396,7 +2396,6 @@ __metadata:
bignumber.js: ^9.1.2
bip39: ^3.1.0
bitcoinjs-lib: ^6.1.5
bitcoinjs-message: ^2.2.0
buffer: ^6.0.3
chai: ^4.4.1
concurrently: ^8.2.2
Expand Down Expand Up @@ -9829,7 +9828,7 @@ __metadata:
languageName: node
linkType: hard

"bindings@npm:^1.3.0, bindings@npm:^1.5.0":
"bindings@npm:^1.3.0":
version: 1.5.0
resolution: "bindings@npm:1.5.0"
dependencies:
Expand Down Expand Up @@ -9971,20 +9970,6 @@ __metadata:
languageName: node
linkType: hard

"bitcoinjs-message@npm:^2.2.0":
version: 2.2.0
resolution: "bitcoinjs-message@npm:2.2.0"
dependencies:
bech32: ^1.1.3
bs58check: ^2.1.2
buffer-equals: ^1.0.3
create-hash: ^1.1.2
secp256k1: ^3.0.1
varuint-bitcoin: ^1.0.1
checksum: 2b851d7b976487118f34507739aabf5bca4b3c14ba997f46b932c71a83104a66a9476a808688a319e3a772c026d65381939852f091affc9dbae0f174eb6b03a1
languageName: node
linkType: hard

"bl@npm:^4.1.0":
version: 4.1.0
resolution: "bl@npm:4.1.0"
Expand Down Expand Up @@ -10235,7 +10220,7 @@ __metadata:
languageName: node
linkType: hard

"browserify-aes@npm:^1.0.0, browserify-aes@npm:^1.0.4, browserify-aes@npm:^1.0.6, browserify-aes@npm:^1.2.0":
"browserify-aes@npm:^1.0.0, browserify-aes@npm:^1.0.4, browserify-aes@npm:^1.2.0":
version: 1.2.0
resolution: "browserify-aes@npm:1.2.0"
dependencies:
Expand Down Expand Up @@ -10353,13 +10338,6 @@ __metadata:
languageName: node
linkType: hard

"buffer-equals@npm:^1.0.3":
version: 1.0.4
resolution: "buffer-equals@npm:1.0.4"
checksum: 392a2f82acdaad46392aec7ce54a8ff0b2a650b5802ccb0c77072050bbc7fd4e101f38460c7e88cdc7e130421882977f595d5c1a3d3343611562ecf7c684a70f
languageName: node
linkType: hard

"buffer-from@npm:^1.0.0, buffer-from@npm:^1.1.0":
version: 1.1.2
resolution: "buffer-from@npm:1.1.2"
Expand Down Expand Up @@ -12543,17 +12521,6 @@ __metadata:
languageName: node
linkType: hard

"drbg.js@npm:^1.0.1":
version: 1.0.1
resolution: "drbg.js@npm:1.0.1"
dependencies:
browserify-aes: ^1.0.6
create-hash: ^1.1.2
create-hmac: ^1.1.4
checksum: f8df5cdd4fb792e548d6187cbc446fbd0afd8f1ef7fa486e1c286c2adee55a687183ce48ab178e9f24965c2deabb6e2ba7a7ee2d675264b951356480eb042476
languageName: node
linkType: hard

"duplexer3@npm:^0.1.4":
version: 0.1.4
resolution: "duplexer3@npm:0.1.4"
Expand Down Expand Up @@ -17998,7 +17965,7 @@ __metadata:
languageName: node
linkType: hard

"nan@npm:^2.13.2, nan@npm:^2.14.0, nan@npm:^2.15.0":
"nan@npm:^2.13.2, nan@npm:^2.15.0":
version: 2.17.0
resolution: "nan@npm:2.17.0"
dependencies:
Expand Down Expand Up @@ -21155,23 +21122,6 @@ __metadata:
languageName: node
linkType: hard

"secp256k1@npm:^3.0.1":
version: 3.8.0
resolution: "secp256k1@npm:3.8.0"
dependencies:
bindings: ^1.5.0
bip66: ^1.1.5
bn.js: ^4.11.8
create-hash: ^1.2.0
drbg.js: ^1.0.1
elliptic: ^6.5.2
nan: ^2.14.0
node-gyp: latest
safe-buffer: ^5.1.2
checksum: 37aaae687a8de9b7bc733ab26bc89c4302b9c681d69d71d531842d99d3af9301a4e30dbe40122793ec64b7a08b8fee8d2330397b7b2dd3a7e404ed259a458089
languageName: node
linkType: hard

"secp256k1@npm:^4.0.0, secp256k1@npm:^4.0.1, secp256k1@npm:^4.0.3":
version: 4.0.3
resolution: "secp256k1@npm:4.0.3"
Expand Down Expand Up @@ -23710,7 +23660,7 @@ __metadata:
languageName: node
linkType: hard

"varuint-bitcoin@npm:1.1.2, varuint-bitcoin@npm:^1.0.1, varuint-bitcoin@npm:^1.0.4, varuint-bitcoin@npm:^1.1.2":
"varuint-bitcoin@npm:1.1.2, varuint-bitcoin@npm:^1.0.4, varuint-bitcoin@npm:^1.1.2":
version: 1.1.2
resolution: "varuint-bitcoin@npm:1.1.2"
dependencies:
Expand Down

2 comments on commit 75347fd

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.