This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2_escrow_account.js
91 lines (79 loc) · 2.64 KB
/
2_escrow_account.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
const StellarSdk = require('stellar-sdk')
const YAML = require('node-yaml')
const configFile = 'config.yml'
const config = YAML.readSync(configFile)
const newKey = StellarSdk.Keypair.random()
console.log('public : ', newKey.publicKey())
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org')
var govKey = StellarSdk.Keypair.fromSecret(config.accounts.gov.secret)
var ngoKey = StellarSdk.Keypair.fromSecret(config.accounts.ngo.secret)
StellarSdk.Network.useTestNetwork()
async function govCreateAccount (startingBalance) {
startingBalance = typeof startingBalance === 'string' ? startingBalance : startingBalance.toString()
const govAccount = await server.loadAccount(govKey.publicKey())
const transaction = new StellarSdk.TransactionBuilder(govAccount)
.addOperation(StellarSdk.Operation.createAccount({
destination: newKey.publicKey(),
startingBalance
}))
.build()
transaction.sign(govKey)
return server.submitTransaction(transaction)
}
async function setJointAccount () {
console.log('setting joint account')
const escrowAccount = await server.loadAccount(newKey.publicKey())
const transaction = new StellarSdk.TransactionBuilder(escrowAccount)
.addOperation(StellarSdk.Operation.setOptions({
signer: {
ed25519PublicKey: govKey.publicKey(),
weight: 1
}
}))
.addOperation(StellarSdk.Operation.setOptions({
masterWeight: 0,
lowThreshold: 2,
medThreshold: 2,
highThreshold: 2,
signer: {
ed25519PublicKey: ngoKey.publicKey(),
weight: 1
}
}))
.build()
transaction.sign(newKey)
return server.submitTransaction(transaction)
}
async function sendFund (from, to, amount) {
amount = typeof amount === 'string' ? amount : amount.toString()
const fromAccount = await server.loadAccount(from.publicKey())
const transaction = new StellarSdk.TransactionBuilder(fromAccount)
.addOperation(StellarSdk.Operation.payment({
destination: to.publicKey(),
asset: StellarSdk.Asset.native(),
amount
}))
.build()
transaction.sign(from)
return server.submitTransaction(transaction)
}
async function getEscrowAccount () {
try {
saveConfigSync()
await govCreateAccount(5)
await setJointAccount()
await sendFund(ngoKey, newKey, 100)
await sendFund(govKey, newKey, 100)
console.log('Success!!')
} catch (error) {
console.log('error = ', error.message, JSON.stringify(error.stack, null, 4))
}
}
function saveConfigSync () {
config['accounts']['joint_account'] = {
public: newKey.publicKey(),
secret: newKey.secret()
}
YAML.writeSync(configFile, config)
}
getEscrowAccount()