feat: Add initial atomic swap

This commit is contained in:
Nathan Woodburn 2025-01-30 20:42:51 +11:00
parent 36612e791d
commit 47939cac3d
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
7 changed files with 2810 additions and 1 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
data

View File

@ -7,4 +7,9 @@ Go to Plugins > Custom Plugin Manager
Import this URL:
```
https://git.woodburn.au/nathanwoodburn/firesales-plugin.git
```
```
npm install shakedex
npm install hsd
node node_modules/shakedex/bin/shakedex transfer-lock coolcode -n main -w hot -a y5cSK42tgVCdt4E58jkHjI3nQ9GU32bC -p ./data

View File

@ -1,6 +1,10 @@
import json
import account
import requests
import os
import subprocess
path = os.path.realpath("customPlugins/firesales-plugin")
# Plugin Data
info = {
@ -12,6 +16,18 @@ info = {
# Functions
functions = {
"init": {
"name": "Init",
"description": "Install prerequisites for atomic swaps",
"type":"default",
"params": {},
"returns": {
"status": {
"type": "text",
"name": "Status"
}
}
},
"list": {
"name": "List",
"description": "List a new domain",
@ -60,6 +76,36 @@ functions = {
}
}
def init(params, authentication):
# Install npm packages
if not os.path.exists(f'{path}/node_modules'):
try:
result = subprocess.run(["npm", "install", ], capture_output=True, text=True, cwd=path)
if result.returncode != 0:
return {"status": "Error: " + result.stderr}
except:
return {"status": "Error: Failed to install npm packages"}
# Install js patch files
# Copy shakedex/main.js to node_modules/shakedex/src/cli/main.js
try:
result = subprocess.run(["cp", f'{path}/shakedex/main.js', f'{path}/node_modules/shakedex/src/cli/main.js'], capture_output=True, text=True, cwd=path)
if result.returncode != 0:
return {"status": "Error: " + result.stderr}
except:
return {"status": "Error: Failed to install js patch files"}
# Copy shakedex/index.js to node_modules/shakedex/src/index.js
try:
result = subprocess.run(["cp", f'{path}/shakedex/context.js', f'{path}/node_modules/shakedex/src/context.js'], capture_output=True, text=True, cwd=path)
if result.returncode != 0:
return {"status": "Error: " + result.stderr}
except:
return {"status": "Error: Failed to install js patch files"}
return {"status": "Success"}
def list(params, authentication):
domain = params["domain"]
price = params["price"]

1602
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

19
package.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "firesales-plugin",
"version": "1.0.0",
"description": "FireSales Plugin",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://git.woodburn.au/nathanwoodburn/firesales-plugin.git"
},
"author": "Nathan.Woodburn/",
"license": "AGPL-3.0-only",
"dependencies": {
"hsd": "^7.0.1",
"shakedex": "^0.0.19"
}
}

77
shakedex/context.js Normal file
View File

@ -0,0 +1,77 @@
const { NodeClient, WalletClient } = require('hsd/lib/client');
const Network = require('hsd/lib/protocol/network.js');
const passwordPrompt = require('password-prompt');
class Context {
constructor(
networkName,
walletId,
apiKey,
passphraseGetter = noopPassphraseGetter,
host = '127.0.0.1',
nodeApiKey,
) {
this.networkName = networkName;
this.network = Network.get(networkName);
this.walletId = walletId;
this.nodeClient = new NodeClient({
port: this.network.rpcPort,
host,
apiKey: nodeApiKey || apiKey,
});
this.walletClient = new WalletClient({
port: this.network.walletPort,
host,
apiKey: apiKey,
});
this.wallet = this.walletClient.wallet(walletId);
this.passphraseGetter = passphraseGetter;
}
getPassphrase = () => {
return this.passphraseGetter();
};
execNode = (method, ...args) => {
return this.nodeClient.execute(method, args);
};
execWallet = async (method, ...args) => {
await this.walletClient.execute('selectwallet', [this.walletId]);
return this.walletClient.execute(method, args);
};
unlockWallet = async () => {
const pass = await this.getPassphrase();
if (pass === null) {
return;
}
await this.wallet.unlock(pass, 60);
};
getMTP = async () => {
const info = await this.execNode('getblockchaininfo');
return info.mediantime;
};
getHeight = async () => {
const info = await this.execNode('getblockchaininfo');
return info.blocks;
};
}
exports.Context = Context;
exports.staticPassphraseGetter = function (passphrase) {
return () => new Promise((resolve) => resolve(passphrase));
};
function noopPassphraseGetter() {
return new Promise((resolve) => resolve(null));
}
exports.promptPassphraseGetter = function (
prefix = '>> Please enter your passphrase: '
) {
return () => new Promise((resolve) => resolve(passwordPrompt(prefix)));
};

1058
shakedex/main.js Normal file

File diff suppressed because it is too large Load Diff