-
Notifications
You must be signed in to change notification settings - Fork 15
/
example.js
93 lines (88 loc) · 2.02 KB
/
example.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const { ECPairFactory } = require('ecpair')
const ecc = require('tiny-secp256k1')
const sjs = require('syscoinjs-lib')
const syscoinjs = new sjs.SyscoinJSLib(
undefined,
undefined,
sjs.utils.syscoinNetworks.mainnet
)
const ECPair = ECPairFactory(ecc)
const utxos = [
{
txid: '8ff929825167675d346bb3d0b67987fc844b4d256ca989efbd8f804b0ed0e36f',
vout: 0,
value: '5998190',
address: 'sys1qn7t8r8r36e7z3c7x83ye2n8v346ny26e3xhnzz'
}
]
const outputs = [
{
address: 'sys1q6f2053q2fnlqpxrwrrqpkhnutwemu984p4vjzm',
value: '100000'
},
{
address: 'sys1qn7t8r8r36e7z3c7x83ye2n8v346ny26e3xhnzz',
value: '5897060'
}
]
async function signMultiInputs (
wifs,
inputs,
outputs,
changeAddress,
rate = 10
) {
// complete sign offline here and return transaction id & raw transaction hex string
const feeRate = new sjs.utils.BN(rate)
const txOpts = { rbf: true }
const result = await syscoinjs.createTransaction(
txOpts,
changeAddress,
outputs,
feeRate,
null /* no custom addresses passed in */,
inputs /* custom utxos passed in with blockbook format */
)
if (!result) {
console.log('Could not create transaction, not enough funds?')
}
let tx
try {
const psbt = await syscoinjs.signAndSendWithWIF(
result.psbt,
wifs,
result.assets
)
if (!psbt) {
console.log('Could not sign transaction')
}
// will fail if PSBT is not completely signed
tx = psbt.extractTransaction()
} catch (e) {
console.log('Could not sign/send transaction: ' + e.stack)
}
return {
id: tx.getId(),
raw: tx.toHex()
}
}
(async () => {
// account1
const wif1 = ECPair.fromPrivateKey(
Buffer.from(
'9be988ba3ab8809876ee77a26065c563a338a1694eb876637cea84b7b2b76fff',
'hex'
)
).toWIF()
// signTransaction offline
const signedInformation = await signMultiInputs(
[wif1],
utxos,
outputs,
utxos[0].address
)
console.log(signedInformation)
})().catch((err) => {
console.log(err)
process.exit(1)
})