This commit is contained in:
Matthew Zipkin 2022-03-06 11:33:27 -05:00
commit c85ef390d9
No known key found for this signature in database
GPG Key ID: E7E2984B6289C93A
7 changed files with 32604 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules/
!log/
log/*

61
README.md Normal file
View File

@ -0,0 +1,61 @@
# HIP2-server
## Derive new receive address from xpub with every request
### Configuration
#### Get your wallet account's XPUB
It's reccomended to create a new wallet for your HIP2 server.
https://hsd-dev.org/api-docs/#get-account-information
##### hsd
`hsw-cli --id=<WALLET NAME> account get default | jq accountKey`
##### Bob Wallet
- Get your API key from Settings > Wallet then use hs-client:
`hsw-cli --id=<WALLET NAME> --api-key=<API KEY> account get default | jq -r .accountKey`
##### Paste the xpub by itself into the file in this repo: `conf/xpub`
### Installation & Execution
This package has been pre-built from hsd modules and requires no additional
packages or C compiler, etc. Just run the build with a port number
```
git clone https://github.com/pinheadmz/hip2-server
node hip2-server/build/hip2-server.js 8080
```
Check:
```
--> curl 127.0.0.1:8080
hs1q9f5d5jzhqynzamca4g8arm8qv8s9sc9lly7h7d
--> curl 127.0.0.1:8080
hs1q7z7w94kwja70qfwav0vxzfff0mk653jrllcq7y
--> curl 127.0.0.1:8080
hs1q22rl34g6s8f2druz2v7wvrlq39g8d0satydzz7
```
HIP2-server will track the number of requests in the file `log/hip2-index`.
This value may be needed to fully recover all payments made to your account
since many users may make requests without sending payments.
### NGINX
### Build from source
Don't trust my build? fine. Requires C compiler and whatever else `hsd` wants.
```
git clone https://github.com/pinheadmz/hip2-server
cd hip2-server
npm install
npm run build
```

32132
build/hip2-server.js Normal file

File diff suppressed because it is too large Load Diff

1
conf/xpub Normal file
View File

@ -0,0 +1 @@
xpub6DBMpym6PM3qe7Ug7BwG6zo7dinMMjpk8nmb73czsjkzPTzfQ1d5ZvqDea4uNmMVv1Y9DT6v17GuDL1x2km9FQuKqWMdnrDfRiDNrG1nTMr

326
package-lock.json generated Normal file
View File

@ -0,0 +1,326 @@
{
"name": "hip2-server",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"bcfg": {
"version": "0.1.7",
"resolved": "https://registry.npmjs.org/bcfg/-/bcfg-0.1.7.tgz",
"integrity": "sha512-+4beq5bXwfmxdcEoHYQsaXawh1qFzjLcRvPe5k5ww/NEWzZTm56Jk8LuPmfeGB7X584jZ8xGq6UgMaZnNDa5Ww==",
"requires": {
"bsert": "~0.0.10"
}
},
"bcrypto": {
"version": "5.4.0",
"resolved": "https://registry.npmjs.org/bcrypto/-/bcrypto-5.4.0.tgz",
"integrity": "sha512-KDX2CR29o6ZoqpQndcCxFZAtYA1jDMnXU3jmCfzP44g++Cu7AHHtZN/JbrN/MXAg9SLvtQ8XISG+eVD9zH1+Jg==",
"requires": {
"bufio": "~1.0.7",
"loady": "~0.0.5"
}
},
"bcurl": {
"version": "0.1.10",
"resolved": "https://registry.npmjs.org/bcurl/-/bcurl-0.1.10.tgz",
"integrity": "sha512-NPxrIkc61tI2FvuibKW8IHaWs7YpgGvOWTgYEJYoE0vOiCvjuL6PXeKYq6bZqadeZfQ6v+R74qvj3Siiv+/Pvg==",
"requires": {
"brq": "~0.1.8",
"bsert": "~0.0.10",
"bsock": "~0.1.9"
}
},
"bdb": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/bdb/-/bdb-1.3.0.tgz",
"integrity": "sha512-oJnWnHOTcnJhazwpEzQvPFtSR1IdHtS3PczuLY3klgZTTtRUbARX7tdphQS8iNUUwEVMfuO93eHDWwTICoeJlg==",
"requires": {
"bsert": "~0.0.10",
"loady": "~0.0.5"
}
},
"bdns": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/bdns/-/bdns-0.1.5.tgz",
"integrity": "sha512-LNVkfM7ynlAD0CvPvO9cKxW8YXt1KOCRQZlRsGZWeMyymUWVdHQpZudAzH9chaFAz6HiwAnQxwDemCKDPy6Mag==",
"requires": {
"bsert": "~0.0.10"
}
},
"bevent": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/bevent/-/bevent-0.1.5.tgz",
"integrity": "sha512-hs6T3BjndibrAmPSoKTHmKa3tz/c6Qgjv9iZw+tAoxuP6izfTCkzfltBQrW7SuK5xnY22gv9jCEf51+mRH+Qvg==",
"requires": {
"bsert": "~0.0.10"
}
},
"bfile": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/bfile/-/bfile-0.2.2.tgz",
"integrity": "sha512-X205SsJ7zFAnjeJ/pBLqDqF10x/4Su3pBy8UdVKw4hdGJk7t5pLoRi+uG4rPaDAClGbrEfT/06PGUbYiMYKzTg=="
},
"bfilter": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/bfilter/-/bfilter-1.0.5.tgz",
"integrity": "sha512-GupIidtCvLbKhXnA1sxvrwa+gh95qbjafy7P1U1x/2DHxNabXq4nGW0x3rmgzlJMYlVl+c8fMxoMRIwpKYlgcQ==",
"requires": {
"bsert": "~0.0.10",
"bufio": "~1.0.6",
"mrmr": "~0.1.6"
}
},
"bheep": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/bheep/-/bheep-0.1.5.tgz",
"integrity": "sha512-0KR5Zi8hgJBKL35+aYzndCTtgSGakOMxrYw2uszd5UmXTIfx3+drPGoETlVbQ6arTdAzSoQYA1j35vbaWpQXBg==",
"requires": {
"bsert": "~0.0.10"
}
},
"binet": {
"version": "0.3.7",
"resolved": "https://registry.npmjs.org/binet/-/binet-0.3.7.tgz",
"integrity": "sha512-GF+QD4ajs3GWabaVzso7Kn9aZEbwI0e54FKU2ID8bM/7rIk7BpSJytB1KS7SMpix+fWAi9MAGkOgSFljl0aaKg==",
"requires": {
"bs32": "~0.1.5",
"bsert": "~0.0.10"
}
},
"blgr": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/blgr/-/blgr-0.2.0.tgz",
"integrity": "sha512-2jZdqajYCGD5rwGdOooQpxgjKsiAAV2g8LapwSnbTjAYTZAqmqBAS+GsVGFi+/y7t1Pspidv/5HsWBbJrsEuFw==",
"requires": {
"bsert": "~0.0.10"
}
},
"blru": {
"version": "0.1.6",
"resolved": "https://registry.npmjs.org/blru/-/blru-0.1.6.tgz",
"integrity": "sha512-34+xZ2u4ys/aUzWCU9m6Eee4nVuN1ywdxbi8b3Z2WULU6qvnfeHvCWEdGzlVfRbbhimG2xxJX6R77GD2cuVO6w==",
"requires": {
"bsert": "~0.0.10"
}
},
"blst": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/blst/-/blst-0.1.5.tgz",
"integrity": "sha512-TPl04Cx3CHdPFAJ2x9Xx1Z1FOfpAzmNPfHkfo+pGAaNH4uLhS58ExvamVkZh3jadF+B7V5sMtqvrqdf9mHINYA==",
"requires": {
"bsert": "~0.0.10"
}
},
"bmutex": {
"version": "0.1.6",
"resolved": "https://registry.npmjs.org/bmutex/-/bmutex-0.1.6.tgz",
"integrity": "sha512-nXWOXtQHbfPaMl6jyEF/rmRMrcemj2qn+OCAI/uZYurjfx7Dg3baoXdPzHOL0U8Cfvn8CWxKcnM/rgxL7DR4zw==",
"requires": {
"bsert": "~0.0.10"
}
},
"bns": {
"version": "0.15.0",
"resolved": "https://registry.npmjs.org/bns/-/bns-0.15.0.tgz",
"integrity": "sha512-iJWQVE399vQzPfhalFMJGEQ7k5Ot2D6Mz8dkoPeLO8huWAMOiJNJ1tHzOu5j+ZyNNew6ITgG/LsSyaRPxvkXuw==",
"requires": {
"bcrypto": "~5.4.0",
"bfile": "~0.2.2",
"bheep": "~0.1.5",
"binet": "~0.3.6",
"bs32": "~0.1.6",
"bsert": "~0.0.10",
"btcp": "~0.1.5",
"budp": "~0.1.6",
"bufio": "~1.0.7",
"unbound": "~0.4.3"
}
},
"bpkg": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/bpkg/-/bpkg-0.6.0.tgz",
"integrity": "sha512-GegdmXsSBwMN3yBiO4WC8FmsDsqCRDwOim0IZSEa3EMZoMDYPdTaV9NlNNAV6YkOBFvlnfbAHhdrUEaNQI8nKg==",
"dev": true
},
"brq": {
"version": "0.1.8",
"resolved": "https://registry.npmjs.org/brq/-/brq-0.1.8.tgz",
"integrity": "sha512-6SDY1lJMKXgt5TZ6voJQMH2zV1XPWWtm203PSkx3DSg9AYNYuRfOPFSBDkNemabzgpzFW9/neR4YhTvyJml8rQ==",
"requires": {
"bsert": "~0.0.10"
}
},
"bs32": {
"version": "0.1.6",
"resolved": "https://registry.npmjs.org/bs32/-/bs32-0.1.6.tgz",
"integrity": "sha512-usjDesQqZ8ihHXOnOEQuAdymBHnJEfSd+aELFSg1jN/V3iAf12HrylHlRJwIt6DTMmXpBDQ+YBg3Q3DIYdhRgQ==",
"requires": {
"bsert": "~0.0.10"
}
},
"bsert": {
"version": "0.0.10",
"resolved": "https://registry.npmjs.org/bsert/-/bsert-0.0.10.tgz",
"integrity": "sha512-NHNwlac+WPy4t2LoNh8pXk8uaIGH3NSaIUbTTRXGpE2WEbq0te/tDykYHkFK57YKLPjv/aGHmbqvnGeVWDz57Q=="
},
"bsock": {
"version": "0.1.9",
"resolved": "https://registry.npmjs.org/bsock/-/bsock-0.1.9.tgz",
"integrity": "sha512-/l9Kg/c5o+n/0AqreMxh2jpzDMl1ikl4gUxT7RFNe3A3YRIyZkiREhwcjmqxiymJSRI/Qhew357xGn1SLw/xEw==",
"requires": {
"bsert": "~0.0.10"
}
},
"bsocks": {
"version": "0.2.6",
"resolved": "https://registry.npmjs.org/bsocks/-/bsocks-0.2.6.tgz",
"integrity": "sha512-66UkjoB9f7lhT+WKgYq8MQa6nkr96mlX64JYMlIsXe/X4VeqNwvsx7UOE3ZqD6lkwg8GvBhapRTWj0qWO3Pw8w==",
"requires": {
"binet": "~0.3.5",
"bsert": "~0.0.10"
}
},
"btcp": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/btcp/-/btcp-0.1.5.tgz",
"integrity": "sha512-tkrtMDxeJorn5p0KxaLXELneT8AbfZMpOFeoKYZ5qCCMMSluNuwut7pGccLC5YOJqmuk0DR774vNVQLC9sNq/A=="
},
"budp": {
"version": "0.1.6",
"resolved": "https://registry.npmjs.org/budp/-/budp-0.1.6.tgz",
"integrity": "sha512-o+a8NPq3DhV91j4nInjht2md6mbU1XL+7ciPltP66rw5uD3KP1m5r8lA94LZVaPKcFdJ0l2HVVzRNxnY26Pefg=="
},
"buffer-map": {
"version": "0.0.7",
"resolved": "https://registry.npmjs.org/buffer-map/-/buffer-map-0.0.7.tgz",
"integrity": "sha512-95try3p/vMRkIAAnJDaGkFhGpT/65NoeW6XelEPjAomWYR58RQtW4khn0SwKj34kZoE7uxL7w2koZSwbnszvQQ=="
},
"bufio": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/bufio/-/bufio-1.0.7.tgz",
"integrity": "sha512-bd1dDQhiC+bEbEfg56IdBv7faWa6OipMs/AFFFvtFnB3wAYjlwQpQRZ0pm6ZkgtfL0pILRXhKxOiQj6UzoMR7A=="
},
"bupnp": {
"version": "0.2.6",
"resolved": "https://registry.npmjs.org/bupnp/-/bupnp-0.2.6.tgz",
"integrity": "sha512-J6ykzJhZMxXKN78K+1NzFi3v/51X2Mvzp2hW42BWwmxIVfau6PaN99gyABZ8x05e8MObWbsAis23gShhj9qpbw==",
"requires": {
"binet": "~0.3.5",
"brq": "~0.1.7",
"bsert": "~0.0.10"
}
},
"bval": {
"version": "0.1.6",
"resolved": "https://registry.npmjs.org/bval/-/bval-0.1.6.tgz",
"integrity": "sha512-jxNH9gSx7g749hQtS+nTxXYz/bLxwr4We1RHFkCYalNYcj12RfbW6qYWsKu0RYiKAdFcbNoZRHmWrIuXIyhiQQ==",
"requires": {
"bsert": "~0.0.10"
}
},
"bweb": {
"version": "0.1.10",
"resolved": "https://registry.npmjs.org/bweb/-/bweb-0.1.10.tgz",
"integrity": "sha512-3Kkz/rfsyAWUS+8DV5XYhwcgVN4DfDewrP+iFTcpQfdZzcF6+OypAq7dHOtXV0sW7U/3msA/sEEqz0MHZ9ERWg==",
"requires": {
"bsert": "~0.0.10",
"bsock": "~0.1.8"
}
},
"goosig": {
"version": "0.10.0",
"resolved": "https://registry.npmjs.org/goosig/-/goosig-0.10.0.tgz",
"integrity": "sha512-+BVVLfxmawAmGVjjJpXzu5LNcFIOfgXgP7kWEyc3qu/xn9RMqbPbNfYDdHBZKfZkDMIO7Q4vD790iNYQAXhoFA==",
"requires": {
"bcrypto": "~5.4.0",
"bsert": "~0.0.10",
"loady": "~0.0.5"
}
},
"hsd": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/hsd/-/hsd-3.0.1.tgz",
"integrity": "sha512-AI7ruyDhxyzKQzaDdEj0N2zjjYXjEPwQGlWQTootg85ByNKWQsAfZChvWqi2ek0FLPuNlM1toaDT+jcYi+UmqA==",
"requires": {
"bcfg": "~0.1.7",
"bcrypto": "~5.4.0",
"bdb": "~1.3.0",
"bdns": "~0.1.5",
"bevent": "~0.1.5",
"bfile": "~0.2.2",
"bfilter": "~1.0.5",
"bheep": "~0.1.5",
"binet": "~0.3.6",
"blgr": "~0.2.0",
"blru": "~0.1.6",
"blst": "~0.1.5",
"bmutex": "~0.1.6",
"bns": "~0.15.0",
"bsert": "~0.0.10",
"bsock": "~0.1.9",
"bsocks": "~0.2.6",
"btcp": "~0.1.5",
"buffer-map": "~0.0.7",
"bufio": "~1.0.7",
"bupnp": "~0.2.6",
"bval": "~0.1.6",
"bweb": "~0.1.10",
"goosig": "~0.10.0",
"hs-client": "~0.0.10",
"n64": "~0.2.10",
"urkel": "~0.7.0"
},
"dependencies": {
"hs-client": {
"version": "0.0.10",
"resolved": "https://registry.npmjs.org/hs-client/-/hs-client-0.0.10.tgz",
"integrity": "sha512-15tfeQEMRS1FZA0q9gFbQ1jYs8v4z9oKw9xFwVEyRuckn72hoVAglN4IrFxkOCDMYV7TWCY/nO/yNZp5njYFBw==",
"requires": {
"bcfg": "~0.1.7",
"bcurl": "~0.1.9",
"bsert": "~0.0.10"
}
}
}
},
"loady": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/loady/-/loady-0.0.5.tgz",
"integrity": "sha512-uxKD2HIj042/HBx77NBcmEPsD+hxCgAtjEWlYNScuUjIsh/62Uyu39GOR68TBR68v+jqDL9zfftCWoUo4y03sQ=="
},
"mrmr": {
"version": "0.1.10",
"resolved": "https://registry.npmjs.org/mrmr/-/mrmr-0.1.10.tgz",
"integrity": "sha512-NJRJs+yJyRWwcTqLRf7O32n56UP1+UQoTrGVEoB3LMj0h2jlon790drDbxKvi5mK5k4HfC0cpNkxqHcrJK/evg==",
"requires": {
"bsert": "~0.0.10",
"loady": "~0.0.5"
}
},
"n64": {
"version": "0.2.10",
"resolved": "https://registry.npmjs.org/n64/-/n64-0.2.10.tgz",
"integrity": "sha512-uH9geV4+roR1tohsrrqSOLCJ9Mh1iFcDI+9vUuydDlDxUS1UCAWUfuGb06p3dj3flzywquJNrGsQ7lHP8+4RVQ=="
},
"unbound": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/unbound/-/unbound-0.4.3.tgz",
"integrity": "sha512-2ISqZLXtzp1l9f1V8Yr6S+zuhXxEwE1CjKHjXULFDHJcfhc9Gm3mn19hdPp4rlNGEdCivKYGKjYe3WRGnafYdA==",
"optional": true,
"requires": {
"loady": "~0.0.5"
}
},
"urkel": {
"version": "0.7.0",
"resolved": "https://registry.npmjs.org/urkel/-/urkel-0.7.0.tgz",
"integrity": "sha512-7Z3Gor4DkKKi0Ehp6H9xehWXqyL12+PA4JM41dcVc1LWks4zI4PGWv6DWgxaLCC+otpEuGdq3Vh5ayD/Mvzfbg==",
"requires": {
"bfile": "~0.2.1",
"bmutex": "~0.1.6",
"bsert": "~0.0.10"
}
}
}
}

24
package.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "hip2-server",
"version": "1.0.0",
"description": "",
"main": "build/hip2-server.js",
"scripts": {
"build": "bpkg --browser-field --ignore-bindings --no-license --node --output ./build/hip2-server.js ./src/derive.js",
"reset": "rm log/hip2-index"
},
"repository": {
"type": "git",
"url": "git+https://github.com/pinheadmz/hip2-server.git"
},
"author": "Matthew Zipkin",
"license": "MIT",
"bugs": {
"url": "https://github.com/pinheadmz/hip2-server/issues"
},
"homepage": "https://github.com/pinheadmz/hip2-server#readme",
"devDependencies": {
"bpkg": "^0.6.0",
"hsd": "^3.0.1"
}
}

57
src/derive.js Normal file
View File

@ -0,0 +1,57 @@
'use strict';
const HDPublicKey = require('hsd/lib/hd/public');
const Address = require('hsd/lib/primitives/address');
const fs = require('fs');
const path = require('path');
const http = require('http');
const MAX_INDEX = 0x7fffffff;
if (process.argv.length < 3)
throw new Error('Usage: node build/hip2-server.js <PORT>');
const port = process.argv[2];
const xpubFile = path.join(__dirname, '..', 'conf', 'xpub');
let xpub;
try {
xpub = fs.readFileSync(xpubFile, 'ascii');
} catch (e) {
throw new Error('xpub file missing');
}
if (xpub === 'xpub6DBMpym6PM3qe7Ug7BwG6zo7dinMMjpk8nmb73czsjkzPTzfQ1d5ZvqDea4uNmMVv1Y9DT6v17GuDL1x2km9FQuKqWMdnrDfRiDNrG1nTMr')
throw new Error('Example xpub must not be used! Repalce with your own account xpub.');
const acct = HDPublicKey.fromBase58(xpub);
const recv = acct.derive(0);
function addr() {
const indexFile = path.join(__dirname, '..', 'log', 'hip2-index');
let index = 0;
try {
index = parseInt(fs.readFileSync(indexFile));
} catch (e) {
;
}
fs.writeFileSync(indexFile, index + 1);
// Wow, used all our non-hardened addresses!
// Don't roll over the saved index (that way the user knows this has happened)
// but start over the address space and reuse addresses starting again at 0.
if (index >= MAX_INDEX)
index -= MAX_INDEX;
const pk = recv.derive(index);
const addr = Address.fromPubkey(pk.publicKey).toString();
return addr;
}
const requestListener = function (req, res) {
res.writeHead(200);
res.end(addr());
};
const server = http.createServer(requestListener);
server.listen({port});