-
Notifications
You must be signed in to change notification settings - Fork 11
/
create_transfer.js
225 lines (190 loc) · 7.18 KB
/
create_transfer.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
var BigchainDB = require('bigchaindb-driver')
var bip39 = require('bip39')
// ***************************************************************
// Simple example:
const alice = new BigchainDB.Ed25519Keypair()
const bob = new BigchainDB.Ed25519Keypair()
console.log(alice)
const API_PATH = 'http://127.0.0.1:9984/api/v1/'
const conn = new BigchainDB.Connection(API_PATH, {
app_id: '',
app_key: ''
})
// copy to file
const enemy = new BigchainDB.Ed25519Keypair(bip39.mnemonicToSeed('seedPhrase').slice(0, 32))
createAssets()
async function createAssets() {
const enemyAsset = await createEnemy(enemy)
// Transfer transaction
console.log('Enemy created', enemyAsset)
const groupAsset = await createGroup(enemy)
console.log('Group asset created', groupAsset)
const updatedGroup = await updateGroup(enemy, groupAsset)
console.log('Group updated', updatedGroup)
// Test exported functions
const asset = {did: '2322344',
brand: 'random_name'
}
const metadata = {
meta: 'simple_example'
}
const createSimpl = await createSimpleAsset(enemy, asset, metadata)
const searchById = await searchAsset('random_name')
const appendSimp = await appendTransaction(enemy, groupAsset.id, metadata)
const searchSimpl = await getAssetById(groupAsset.id)
}
async function createEnemy(keypair) {
const txCreateEnemy = BigchainDB.Transaction.makeCreateTransaction(
// Define the asset to store, in this example it is the current temperature
// (in Celsius) for the city of Berlin.
{
'entity': 'ENEMY',
'type': 'SRT'
},
{
'entity': 'ENEMY',
'CPShortName': 'SDFWR',
'Type': 'XSSD',
},
// enemy is the owner
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(keypair.publicKey))],
keypair.publicKey
)
// Sign the transaction with private keys
const txSigned = BigchainDB.Transaction.signTransaction(txCreateEnemy, keypair.privateKey)
return conn.postTransaction(txSigned)
.then(() => conn.pollStatusAndFetchTransaction(txSigned.id))
.then(res => {
console.log('createEnemy function finished. ', txSigned.id, 'accepted')
return res
})
}
async function createGroup(keypair, enemyId) {
const txCreateGroup = BigchainDB.Transaction.makeCreateTransaction(
// Define the asset to store, in this example it is the current temperature
// (in Celsius) for the city of Berlin.
{
'entity': 'GROUP',
'assettype': 'SW',
'authorizedaction': 'READ',
},
{
'entity': 'GROUP',
'assettype': 'SW',
'authorizedaction': 'READ'
},
// enemy is the owner
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(keypair.publicKey))],
keypair.publicKey
)
// Sign the transaction with private keys
const txSigned = BigchainDB.Transaction.signTransaction(txCreateGroup, keypair.privateKey)
return conn.postTransaction(txSigned)
.then(() => conn.pollStatusAndFetchTransaction(txSigned.id))
.then(res => {
console.log('createGroup function finished, ', txSigned.id, 'accepted')
return res
})
}
async function updateGroup(keypair, tx) {
const createTranfer = BigchainDB.Transaction.makeTransferTransaction(
tx,
{
'entity': 'GROUP',
'assettype': 'ASSSQ',
'authorizedaction': 'REWAD'
}, [BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(keypair.publicKey))],
0
)
const signedTransfer = BigchainDB.Transaction.signTransaction(createTranfer, keypair.privateKey)
return conn.postTransaction(signedTransfer)
.then(() => conn.pollStatusAndFetchTransaction(signedTransfer.id))
.then(res => {
console.log('updateGroup function finished, ', signedTransfer.id, 'accepted')
return signedTransfer
})
}
async function createSimpleAsset(keypair, asset, metadata){
const txSimpleAsset = BigchainDB.Transaction.makeCreateTransaction(
// Define the asset to store, in this example it is the current temperature
// (in Celsius) for the city of Berlin.
asset,
metadata,
// ENEMY is the owner
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(keypair.publicKey))],
keypair.publicKey
)
// Sign the transaction with private keys
const txSigned = BigchainDB.Transaction.signTransaction(txSimpleAsset, keypair.privateKey)
return conn.postTransaction(txSigned)
.then(() => conn.pollStatusAndFetchTransaction(txSigned.id))
.then(res => {
console.log('Created simple asset', txSigned.id, 'accepted')
return res
})
}
async function getAssetById(assetId){
return conn.getTransaction(assetId)
.then(res => {
console.log('Retrieve asset by id', res)
return res
})
}
async function appendTransaction(keypair, assetId, metadata){
// First, we query for the asset that we created
conn.listTransactions(assetId)
.then((txList) => {
if (txList.length <= 1) {
return txList
}
const inputTransactions = []
txList.forEach((tx) =>
tx.inputs.forEach(input => {
// Create transaction have fulfills = null
if (input.fulfills) {
inputTransactions.push(input.fulfills.transaction_id)
}
})
)
// In our case there should be just one input not spend with the assetId
return txList.filter((tx) => inputTransactions.indexOf(tx.id) === -1)
})
.then((tx) => {
// As there is just one input
return conn.getTransaction(tx[0].id)
})
.then((txCreated) => {
const createTranfer = BigchainDB.Transaction.makeTransferTransaction(
txCreated,
metadata,
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(keypair.publicKey))],
0
)
// Sign with the owner of the car as she was the creator of the car
const signedTransfer = BigchainDB.Transaction.signTransaction(createTranfer, keypair.privateKey)
return conn.postTransaction(signedTransfer)
})
.then((signedTransfer) => conn.pollStatusAndFetchTransaction(signedTransfer.id))
.then(res => {
console.log('Appended transaction', res.id)
})
}
async function searchAsset(asset){
return conn.searchAssets(asset)
.then((txList) => {
console.log('Search Asset ', txList)
return txList
})
}
async function searchMetadata(metadata){
return conn.searchMetadata(metadata)
.then((txList) => {
console.log('Search Asset ', txList)
return txList
})
}