Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-chillios committed Feb 21, 2025
1 parent d4241db commit f82ceac
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 43 deletions.
8 changes: 3 additions & 5 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,7 @@

"command-line-usage": ["[email protected]", "", { "dependencies": { "array-back": "^4.0.2", "chalk": "^2.4.2", "table-layout": "^1.0.2", "typical": "^5.2.0" } }, "sha512-sH5ZSPr+7UStsloltmDh7Ce5fb8XPlHyoPzTpyyMuYCtervL65+ubVZ6Q61cFtFl62UyJlc8/JwERRbAFPUqgw=="],

"commander": ["commander@8.3.0", "", {}, "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww=="],
"commander": ["commander@2.20.3", "", {}, "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ=="],

"common-ancestor-path": ["[email protected]", "", {}, "sha512-L3sHRo1pXXEqX8VU28kfgUY+YGsk09hPqZiZmLacNib6XNTCM8ubYeT7ryXQw8asB1sKgcU5lkB7ONug08aB8w=="],

Expand Down Expand Up @@ -4033,8 +4033,6 @@

"jayson/@types/ws": ["@types/[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-JQbbmxZTZehdc2iszGKs5oC3NFnjeay7mtAWrdt7qNtAVK0g19muApzAy4bm9byz79xa2ZnO/BOBC2R8RC5Lww=="],

"jayson/commander": ["[email protected]", "", {}, "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ=="],

"jayson/uuid": ["[email protected]", "", { "bin": { "uuid": "dist/bin/uuid" } }, "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="],

"jayson/ws": ["[email protected]", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": "^5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ=="],
Expand Down Expand Up @@ -4159,6 +4157,8 @@

"socks-proxy-agent/agent-base": ["[email protected]", "", {}, "sha512-jRR5wdylq8CkOe6hei19GGZnxM6rBGwFl3Bg0YItGDimvjGtAvdZk4Pu6Cl4u4Igsws4a1fd1Vq3ezrhn4KmFw=="],

"solc/commander": ["[email protected]", "", {}, "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww=="],

"solc/semver": ["[email protected]", "", { "bin": { "semver": "bin/semver" } }, "sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g=="],

"solidity-coverage/chalk": ["[email protected]", "", { "dependencies": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", "supports-color": "^5.3.0" } }, "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ=="],
Expand All @@ -4185,8 +4185,6 @@

"table-layout/typical": ["[email protected]", "", {}, "sha512-dvdQgNDNJo+8B2uBQoqdb11eUCE1JQXhvjC/CZtgvZseVd5TYMXnq0+vuUemXbd/Se29cTaUuPX3YIc2xgbvIg=="],

"terser/commander": ["[email protected]", "", {}, "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ=="],

"then-request/@types/node": ["@types/[email protected]", "", {}, "sha512-tktOkFUA4kXx2hhhrB8bIFb5TbwzS4uOhKEmwiD+NoiL0qtP2OQ9mFldbgD4dV1djrlBYP6eBuQZiWjuHUpqFw=="],

"then-request/form-data": ["[email protected]", "", { "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", "es-set-tostringtag": "^2.1.0", "mime-types": "^2.1.35", "safe-buffer": "^5.2.1" } }, "sha512-XHIrMD0NpDrNM/Ckf7XJiBbLl57KEhT3+i3yY+eWm+cqYZJQTZrKo8Y8AWKnuV5GT4scfuUGt9LzNoIx3dU1nQ=="],
Expand Down
18 changes: 10 additions & 8 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ function createTypeDoc(docs, nest = "") {
}

function createDocs() {
/**
* I recommend commenting out unnecessary entries in development as re-generating the docs is a bit slow.
*/
if (!process.env.REFERENCES) {
return { plugins: [], items: [] };
}

const base = createTypeDoc([
{ label: "/core", entrypoint: "core/src/index.ts" },
Expand Down Expand Up @@ -119,11 +119,13 @@ export default defineConfig({
sidebar: [
{ label: "Guides", autogenerate: { directory: "guides" } },
{ label: "Others", autogenerate: { directory: "others" } },
{
label: "References",
collapsed: true,
items: [{ label: "@swapkit", items: docsSidebarItems }],
},
...(process.env.REFERENCES
? {
label: "References",
collapsed: true,
items: [{ label: "@swapkit", items: docsSidebarItems }],
}
: []),
],
}),
],
Expand Down
5 changes: 1 addition & 4 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
"version": "0.0.0",
"scripts": {
"dev": "astro dev",
"start": "astro dev",
"build": "astro build",
"preview": "astro preview",
"astro": "astro"
"deploy": "REFERENCES=true astro build"
},
"dependencies": {
"@astrojs/react": "4.2.0",
Expand Down
44 changes: 42 additions & 2 deletions docs/src/content/docs/others/migrate_to_v4.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,49 @@ description: A guide to migrate to v4 and update your codebase.
---

```ts twoslash
import { EVMToolbox } from "@swapkit/toolboxes/evm"
import { BTCToolbox } from "@swapkit/toolboxes/utxo"
import { Chain, createSwapKit, DerivationPath } from "@swapkit/sdk"

const btc = BTCToolbox()

const btcKeys = await btc.createKeysForPath({
phrase: "...",
chain: Chain.Bitcoin,
derivationPath: `${DerivationPath.BTC}/0`,
})

const publicKey = btcKeys.getPublicKey?.();



const skClient = createSwapKit()



























const evm = EVMToolbox



Expand Down
52 changes: 28 additions & 24 deletions packages/toolboxes/src/utxo/toolbox/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,26 @@ import type { BCHToolbox, BTCToolbox, DASHToolbox, DOGEToolbox, LTCToolbox } fro

export const nonSegwitChains = [Chain.Dash, Chain.Dogecoin];

async function createKeysForPath({
phrase,
wif,
derivationPath,
chain,
}: { phrase?: string; wif?: string; derivationPath: string; chain: Chain }) {
const { ECPairFactory } = await import("ecpair");
if (!(wif || phrase)) throw new Error("Either phrase or wif must be provided");
function createKeysForPath(chain: Chain) {
return async function createKeysForPath({
phrase,
wif,
derivationPath,
}: { phrase?: string; wif?: string; derivationPath: string; chain: Chain }) {
const { ECPairFactory } = await import("ecpair");
if (!(wif || phrase)) throw new Error("Either phrase or wif must be provided");

const factory = ECPairFactory(secp256k1);
const network = getNetwork(chain);
const factory = ECPairFactory(secp256k1);
const network = getNetwork(chain);

if (wif) return factory.fromWIF(wif, network);
if (wif) return factory.fromWIF(wif, network);

const seed = mnemonicToSeedSync(phrase as string);
const master = HDKey.fromMasterSeed(seed, network).derive(derivationPath);
if (!master.privateKey) throw new Error("Could not get private key from phrase");
const seed = mnemonicToSeedSync(phrase as string);
const master = HDKey.fromMasterSeed(seed, network).derive(derivationPath);
if (!master.privateKey) throw new Error("Could not get private key from phrase");

return factory.fromPrivateKey(Buffer.from(master.privateKey), { network });
return factory.fromPrivateKey(Buffer.from(master.privateKey), { network });
};
}

function validateAddress({ address, chain }: { address: string; chain: UTXOChain }) {
Expand All @@ -60,14 +61,16 @@ function validateAddress({ address, chain }: { address: string; chain: UTXOChain
}
}

function getAddressFromKeys({ keys, chain }: { chain: UTXOChain; keys: ECPairInterface }) {
if (!keys) throw new Error("Keys must be provided");
function getAddressFromKeys(chain: UTXOChain) {
return function getAddressFromKeys(keys: ECPairInterface) {
if (!keys) throw new Error("Keys must be provided");

const method = nonSegwitChains.includes(chain) ? payments.p2pkh : payments.p2wpkh;
const { address } = method({ pubkey: keys.publicKey, network: getNetwork(chain) });
if (!address) throw new Error("Address not defined");
const method = nonSegwitChains.includes(chain) ? payments.p2pkh : payments.p2wpkh;
const { address } = method({ pubkey: keys.publicKey, network: getNetwork(chain) });
if (!address) throw new Error("Address not defined");

return address;
return address;
};
}

function transfer(chain: UTXOChain) {
Expand Down Expand Up @@ -307,15 +310,16 @@ export const BaseUTXOToolbox = (chain: UTXOChain) => ({
getInputsOutputsFee: getInputsOutputsFee(chain),

broadcastTx: (txHash: string) => getUtxoApi(chain).broadcastTx(txHash),
getAddressFromKeys: (keys: ECPairInterface) => getAddressFromKeys({ keys, chain }),
getAddressFromKeys: getAddressFromKeys(chain),
validateAddress: (address: string) => validateAddress({ address, chain }),
createKeysForPath: (params: any) => createKeysForPath({ ...params, chain }),
createKeysForPath: createKeysForPath(chain),

getPrivateKeyFromMnemonic: async (params: {
phrase: string;
derivationPath: string;
}) => {
const keys = await createKeysForPath({ ...params, chain });
const getKeysForPath = createKeysForPath(chain);
const keys = await getKeysForPath({ ...params, chain });
return keys.toWIF();
},

Expand Down

0 comments on commit f82ceac

Please sign in to comment.