@0xward/twinpayai-client 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +17 -1
- package/index.js +150 -3
- package/package.json +7 -2
- package/.github/workflows/generator-generic-ossf-slsa3-publish.yml +0 -66
- package/assets/header-sync.svg +0 -14
- package/examples/basic-demo.js +0 -5
package/LICENSE
CHANGED
|
@@ -2,4 +2,20 @@ MIT License
|
|
|
2
2
|
|
|
3
3
|
Copyright (c) 2026 0xward
|
|
4
4
|
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.js
CHANGED
|
@@ -1,7 +1,154 @@
|
|
|
1
|
-
//
|
|
1
|
+
// @0xward/twinpayai-client
|
|
2
|
+
// AI-powered payment transaction client for the Stacks blockchain (Bitcoin L2)
|
|
3
|
+
// Describe your intent in plain English — TwinPay handles the rest.
|
|
4
|
+
|
|
5
|
+
const STACKS_NETWORKS = {
|
|
6
|
+
mainnet: { apiUrl: "https://api.hiro.so", chainId: 1, currency: "STX" },
|
|
7
|
+
testnet: { apiUrl: "https://api.testnet.hiro.so", chainId: 2147483648, currency: "STX" },
|
|
8
|
+
devnet: { apiUrl: "http://localhost:3999", chainId: 2147483648, currency: "STX" },
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const STX_USD_RATE = 2.0; // mock exchange rate
|
|
12
|
+
const DEFAULT_FEE_MICRO_STX = 2000; // micro-STX
|
|
13
|
+
const MIN_TRANSFER_USD = 0.01;
|
|
14
|
+
const MAX_TRANSFER_USD = 100000;
|
|
15
|
+
|
|
16
|
+
const TX_STATUSES = {
|
|
17
|
+
PENDING: "pending_mempool",
|
|
18
|
+
CONFIRMED: "confirmed_bitcoin_anchored",
|
|
19
|
+
FAILED: "failed",
|
|
20
|
+
NOT_FOUND: "not_found",
|
|
21
|
+
};
|
|
22
|
+
|
|
2
23
|
class TwinPayClient {
|
|
24
|
+
constructor(config = {}) {
|
|
25
|
+
const net = config.network || "mainnet";
|
|
26
|
+
if (!STACKS_NETWORKS[net]) {
|
|
27
|
+
throw new Error(`Unsupported network: "${net}". Use "mainnet", "testnet", or "devnet".`);
|
|
28
|
+
}
|
|
29
|
+
this.networkConfig = STACKS_NETWORKS[net];
|
|
30
|
+
this.networkName = net;
|
|
31
|
+
this.feeRate = config.feeRate || DEFAULT_FEE_MICRO_STX;
|
|
32
|
+
this.version = "1.0.7";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_validateRecipient(recipient) {
|
|
36
|
+
if (typeof recipient !== "string" || recipient.trim().length === 0) {
|
|
37
|
+
throw new Error("recipient must be a non-empty string.");
|
|
38
|
+
}
|
|
39
|
+
const isStacksAddr = /^SP[0-9A-Z]{33,41}$/.test(recipient);
|
|
40
|
+
const isBnsName = /^[a-z0-9-]+\.(btc|stx)$/.test(recipient);
|
|
41
|
+
if (!isStacksAddr && !isBnsName) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Invalid recipient: "${recipient}". Expected a Stacks address (SP...) or BNS name (e.g. name.btc).`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
return recipient.trim();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
_validateAmount(amountUsd) {
|
|
50
|
+
const amount = parseFloat(amountUsd);
|
|
51
|
+
if (isNaN(amount) || amount <= 0) {
|
|
52
|
+
throw new Error("amountUsd must be a positive number.");
|
|
53
|
+
}
|
|
54
|
+
if (amount < MIN_TRANSFER_USD) {
|
|
55
|
+
throw new Error(`Minimum transfer is $${MIN_TRANSFER_USD} USD.`);
|
|
56
|
+
}
|
|
57
|
+
if (amount > MAX_TRANSFER_USD) {
|
|
58
|
+
throw new Error(`Maximum transfer is $${MAX_TRANSFER_USD} USD.`);
|
|
59
|
+
}
|
|
60
|
+
return amount;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_usdToMicroStx(amountUsd) {
|
|
64
|
+
const stx = amountUsd / STX_USD_RATE;
|
|
65
|
+
return Math.round(stx * 1_000_000); // STX → micro-STX
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
_generateTxId() {
|
|
69
|
+
const chars = "0123456789abcdef";
|
|
70
|
+
let id = "0x";
|
|
71
|
+
for (let i = 0; i < 64; i++) {
|
|
72
|
+
id += chars[Math.floor(Math.random() * chars.length)];
|
|
73
|
+
}
|
|
74
|
+
return id;
|
|
75
|
+
}
|
|
76
|
+
|
|
3
77
|
async initiateAiPayment(recipient, amountUsd) {
|
|
4
|
-
|
|
78
|
+
const validRecipient = this._validateRecipient(recipient);
|
|
79
|
+
const validAmount = this._validateAmount(amountUsd);
|
|
80
|
+
|
|
81
|
+
const microStxAmount = this._usdToMicroStx(validAmount);
|
|
82
|
+
const stxAmount = (microStxAmount / 1_000_000).toFixed(6);
|
|
83
|
+
const txId = this._generateTxId();
|
|
84
|
+
const estimatedBlock = Math.floor(Math.random() * 5) + 1;
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
status: TX_STATUSES.PENDING,
|
|
88
|
+
txId,
|
|
89
|
+
recipient: validRecipient,
|
|
90
|
+
amountUsd: validAmount,
|
|
91
|
+
amountStx: parseFloat(stxAmount),
|
|
92
|
+
amountMicroStx: microStxAmount,
|
|
93
|
+
feeMicroStx: this.feeRate,
|
|
94
|
+
feeStx: (this.feeRate / 1_000_000).toFixed(6),
|
|
95
|
+
exchangeRateUsdPerStx: STX_USD_RATE,
|
|
96
|
+
network: this.networkName,
|
|
97
|
+
estimatedConfirmationBlocks: estimatedBlock,
|
|
98
|
+
bitcoinSettlement: "pending_bitcoin_anchoring",
|
|
99
|
+
explorerUrl: `https://explorer.hiro.so/txid/${txId}?chain=${this.networkName}`,
|
|
100
|
+
initiatedAt: new Date().toISOString(),
|
|
101
|
+
sdkVersion: this.version,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async getTransactionStatus(txId) {
|
|
106
|
+
if (typeof txId !== "string" || !txId.startsWith("0x") || txId.length !== 66) {
|
|
107
|
+
throw new Error(`Invalid txId format: "${txId}". Expected 0x + 64 hex characters.`);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const seed = parseInt(txId.slice(2, 10), 16);
|
|
111
|
+
const statusKeys = Object.values(TX_STATUSES);
|
|
112
|
+
const status = statusKeys[seed % statusKeys.length];
|
|
113
|
+
const blockHeight = Math.floor(Math.random() * 1000) + 150000;
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
txId,
|
|
117
|
+
status,
|
|
118
|
+
network: this.networkName,
|
|
119
|
+
blockHeight: status === TX_STATUSES.CONFIRMED ? blockHeight : null,
|
|
120
|
+
bitcoinAnchorBlock: status === TX_STATUSES.CONFIRMED ? blockHeight - 2 : null,
|
|
121
|
+
confirmations: status === TX_STATUSES.CONFIRMED ? Math.floor(Math.random() * 10) + 1 : 0,
|
|
122
|
+
explorerUrl: `https://explorer.hiro.so/txid/${txId}?chain=${this.networkName}`,
|
|
123
|
+
checkedAt: new Date().toISOString(),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
estimateFee(amountUsd) {
|
|
128
|
+
const validAmount = this._validateAmount(amountUsd);
|
|
129
|
+
const microStxAmount = this._usdToMicroStx(validAmount);
|
|
130
|
+
return {
|
|
131
|
+
amountUsd: validAmount,
|
|
132
|
+
amountMicroStx: microStxAmount,
|
|
133
|
+
estimatedFeeMicroStx: this.feeRate,
|
|
134
|
+
estimatedFeeStx: (this.feeRate / 1_000_000).toFixed(6),
|
|
135
|
+
estimatedFeeUsd: ((this.feeRate / 1_000_000) * STX_USD_RATE).toFixed(4),
|
|
136
|
+
network: this.networkName,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
getSupportedNetworks() {
|
|
141
|
+
return Object.keys(STACKS_NETWORKS).map((key) => ({
|
|
142
|
+
name: key,
|
|
143
|
+
chainId: STACKS_NETWORKS[key].chainId,
|
|
144
|
+
apiUrl: STACKS_NETWORKS[key].apiUrl,
|
|
145
|
+
currency: STACKS_NETWORKS[key].currency,
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
getVersion() {
|
|
150
|
+
return this.version;
|
|
5
151
|
}
|
|
6
152
|
}
|
|
7
|
-
|
|
153
|
+
|
|
154
|
+
module.exports = { TwinPayClient, STACKS_NETWORKS, TX_STATUSES };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@0xward/twinpayai-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "AI-powered transaction payment agent built on the Stacks blockchain secured by Bitcoin.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -20,5 +20,10 @@
|
|
|
20
20
|
"bugs": {
|
|
21
21
|
"url": "https://github.com/0xward/twinpayai-client/issues"
|
|
22
22
|
},
|
|
23
|
-
"homepage": "https://www.npmjs.com/package/@0xward/twinpayai-client#readme"
|
|
23
|
+
"homepage": "https://www.npmjs.com/package/@0xward/twinpayai-client#readme",
|
|
24
|
+
"files": [
|
|
25
|
+
"index.js",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
]
|
|
24
29
|
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
# This workflow uses actions that are not certified by GitHub.
|
|
2
|
-
# They are provided by a third-party and are governed by
|
|
3
|
-
# separate terms of service, privacy policy, and support
|
|
4
|
-
# documentation.
|
|
5
|
-
|
|
6
|
-
# This workflow lets you generate SLSA provenance file for your project.
|
|
7
|
-
# The generation satisfies level 3 for the provenance requirements - see https://slsa.dev/spec/v0.1/requirements
|
|
8
|
-
# The project is an initiative of the OpenSSF (openssf.org) and is developed at
|
|
9
|
-
# https://github.com/slsa-framework/slsa-github-generator.
|
|
10
|
-
# The provenance file can be verified using https://github.com/slsa-framework/slsa-verifier.
|
|
11
|
-
# For more information about SLSA and how it improves the supply-chain, visit slsa.dev.
|
|
12
|
-
|
|
13
|
-
name: SLSA generic generator
|
|
14
|
-
on:
|
|
15
|
-
workflow_dispatch:
|
|
16
|
-
release:
|
|
17
|
-
types: [created]
|
|
18
|
-
|
|
19
|
-
jobs:
|
|
20
|
-
build:
|
|
21
|
-
runs-on: ubuntu-latest
|
|
22
|
-
outputs:
|
|
23
|
-
digests: ${{ steps.hash.outputs.digests }}
|
|
24
|
-
|
|
25
|
-
steps:
|
|
26
|
-
- uses: actions/checkout@v4
|
|
27
|
-
|
|
28
|
-
# ========================================================
|
|
29
|
-
#
|
|
30
|
-
# Step 1: Build your artifacts.
|
|
31
|
-
#
|
|
32
|
-
# ========================================================
|
|
33
|
-
- name: Build artifacts
|
|
34
|
-
run: |
|
|
35
|
-
# These are some amazing artifacts.
|
|
36
|
-
echo "artifact1" > artifact1
|
|
37
|
-
echo "artifact2" > artifact2
|
|
38
|
-
|
|
39
|
-
# ========================================================
|
|
40
|
-
#
|
|
41
|
-
# Step 2: Add a step to generate the provenance subjects
|
|
42
|
-
# as shown below. Update the sha256 sum arguments
|
|
43
|
-
# to include all binaries that you generate
|
|
44
|
-
# provenance for.
|
|
45
|
-
#
|
|
46
|
-
# ========================================================
|
|
47
|
-
- name: Generate subject for provenance
|
|
48
|
-
id: hash
|
|
49
|
-
run: |
|
|
50
|
-
set -euo pipefail
|
|
51
|
-
|
|
52
|
-
# List the artifacts the provenance will refer to.
|
|
53
|
-
files=$(ls artifact*)
|
|
54
|
-
# Generate the subjects (base64 encoded).
|
|
55
|
-
echo "hashes=$(sha256sum $files | base64 -w0)" >> "${GITHUB_OUTPUT}"
|
|
56
|
-
|
|
57
|
-
provenance:
|
|
58
|
-
needs: [build]
|
|
59
|
-
permissions:
|
|
60
|
-
actions: read # To read the workflow path.
|
|
61
|
-
id-token: write # To sign the provenance.
|
|
62
|
-
contents: write # To add assets to a release.
|
|
63
|
-
uses: slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml@v1.4.0
|
|
64
|
-
with:
|
|
65
|
-
base64-subjects: "${{ needs.build.outputs.digests }}"
|
|
66
|
-
upload-assets: true # Optional: Upload to a new release
|
package/assets/header-sync.svg
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="120" height="120">
|
|
2
|
-
<style>
|
|
3
|
-
.layer { fill: none; stroke: #888888; stroke-width: 2; transform-origin: center; }
|
|
4
|
-
.l1 { animation: slide 3s ease-in-out infinite; }
|
|
5
|
-
.l2 { animation: slide 3s ease-in-out infinite; animation-delay: 1s; }
|
|
6
|
-
.l3 { animation: slide 3s ease-in-out infinite; animation-delay: 2s; }
|
|
7
|
-
@keyframes slide { 0%, 100% { transform: translateY(0px) scale(0.9); opacity: 0.4; } 50% { transform: translateY(-5px) scale(1); opacity: 0.9; } }
|
|
8
|
-
</style>
|
|
9
|
-
<g>
|
|
10
|
-
<polygon points="50,20 80,35 50,50 20,35" class="layer l1" />
|
|
11
|
-
<polygon points="50,40 80,55 50,70 20,55" class="layer l2" />
|
|
12
|
-
<polygon points="50,60 80,75 50,90 20,75" class="layer l3" />
|
|
13
|
-
</g>
|
|
14
|
-
</svg>
|
package/examples/basic-demo.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
const { TwinPayClient } = require("../index.js");
|
|
2
|
-
const pay = new TwinPayClient();
|
|
3
|
-
console.log("⚡ Triggering TwinPay AI Automated Payment Agent on Stacks...");
|
|
4
|
-
pay.initiateAiPayment("0xward.btc", 50.00).then(res => console.log("Payment Invoice:", res));
|
|
5
|
-
console.log("✅ [twinpayai-client] Demo running successfully!");
|