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 1265488
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 38 deletions.
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
7 changes: 7 additions & 0 deletions packages/ui/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { buildPackage } from "../../tools/builder";

const packages = ["react", "widget"];

buildPackage({
entrypoints: packages.map((p) => `./src/${p}/index.ts`),
});
32 changes: 32 additions & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"author": "swapkit-oss",
"dependencies": {
"@swapkit/core": "workspace:*",
"@swapkit/plugins": "workspace:*",
"@swapkit/wallets": "workspace:*"
},
"description": "SwapKit - UI",
"files": ["src/", "dist/"],
"homepage": "https://github.com/thorswap/SwapKit",
"license": "Apache-2.0",
"name": "@swapkit/ui",
"repository": {
"type": "git",
"url": "git+https://github.com/thorswap/SwapKit.git"
},
"exports": {
"./widget": {
"default": "./dist/widget.js",
"require": "./dist/widget.cjs",
"types": "./src/widget.ts"
}
},
"scripts": {
"build": "bun run ./build.ts",
"clean": "rm -rf dist node_modules *.tsbuildinfo",
"lint": "biome check --write ./src",
"type-check": "tsc --noEmit"
},
"type": "module",
"version": "0.1.0"
}
Empty file added packages/ui/src/react/index.ts
Empty file.
Empty file added packages/ui/src/widget/index.ts
Empty file.
4 changes: 4 additions & 0 deletions packages/ui/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tools/typescript/base.json",
"include": ["src", "./build.ts"]
}

0 comments on commit 1265488

Please sign in to comment.