generated from jasonsturges/typescript-npm-package
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcreate-transaction.ts
53 lines (47 loc) · 1.32 KB
/
create-transaction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import {
ErrorNoAdminKey,
ErrorResponse,
SPVWalletUserAPI,
SpvWalletError,
} from '../dist/typescript-npm-package.cjs.js';
import { exampleXPriv } from './example-keys.js';
import { errMessage } from './utils.js';
const server = 'http://localhost:3003';
if (!exampleXPriv) {
console.log(errMessage('xPriv'));
process.exit(1);
}
const client = new SPVWalletUserAPI(server, {
xPriv: exampleXPriv,
});
try {
const newTransaction = await client.sendToRecipients(
{
outputs: [
{
to: '[email protected]',
satoshis: 1,
},
],
},
{ some_metadata: 'example' },
);
console.log('SendToRecipients response:', newTransaction);
const tx = await client.transaction(newTransaction.id);
console.log('GetTransaction response:', tx);
} catch (e) {
if (e instanceof SpvWalletError) {
// You can check the type of the error and do something specific
if (e instanceof ErrorResponse) {
console.error('Response status:', e.response.status);
console.error('Content:', e.content);
} else if (e instanceof ErrorNoAdminKey) {
console.error('ErrorNoAdminKey', e.message);
} else {
//check all the other error types here: src/errors.ts
console.error('SpvWalletError:', e.message);
}
} else {
console.log('Unknown error:', e);
}
}