-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
95 lines (88 loc) · 3.48 KB
/
app.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
const BigchainDB = require('bigchaindb-driver')
const API_PATH = 'https://test.ipdb.io/api/v1/'
const conn = new BigchainDB.Connection(API_PATH)
const bip39 = require('bip39')
const runThis = async () => {
let txCreatedID
const seed = await bip39.mnemonicToSeed('seedPhrase')
console.log(seed)
const alice = new BigchainDB.Ed25519Keypair(seed.slice(0, 32))
const josh = new BigchainDB.Ed25519Keypair(seed.slice(0, 32))
const painting = {
name: 'Meninas',
author: 'Diego Rodríguez de Silva y Velázquez',
place: 'Madrid',
year: '1656'
}
const createPaint = async () => {
// Construct a transaction payload
const txCreatePaint = BigchainDB.Transaction.makeCreateTransaction(
// Asset field
{
painting,
},
// Metadata field, contains information about the transaction itself
// (can be `null` if not needed)
{
datetime: new Date().toString(),
location: 'Madrid',
value: {
value_eur: '25000000€',
value_btc: '2200',
}
},
// Output. For this case we create a simple Ed25519 condition
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(alice.publicKey))],
// Issuers
alice.publicKey
)
// The owner of the painting signs the transaction
const txSigned = BigchainDB.Transaction.signTransaction(txCreatePaint,
alice.privateKey)
// Send the transaction off to BigchainDB
await conn.postTransactionCommit(txSigned)
.then(res => {
console.log('Transaction created')
// console.log(txSigned.id)
txCreatedID = txSigned.id
// txSigned.id corresponds to the asset id of the painting
})
}
await createPaint()
console.log(txCreatedID)
const transferOwnership = async (txCreatedID, newOwner) => {
// Get transaction payload by ID
await conn.getTransaction(txCreatedID)
.then((txCreated) => {
const createTranfer = BigchainDB.Transaction.
makeTransferTransaction(
// The output index 0 is the one that is being spent
[{
tx: txCreated,
output_index: 0
}],
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(
newOwner.publicKey))],
{
datetime: new Date().toString(),
value: {
value_eur: '30000000€',
value_btc: '2100',
}
}
)
// Sign with the key of the owner of the painting (Alice)
const signedTransfer = BigchainDB.Transaction
.signTransaction(createTranfer, alice.privateKey)
return conn.postTransactionCommit(signedTransfer)
})
.then(res => {
console.log('Transfer Transaction created')
console.log(res.id)
})
}
await transferOwnership(txCreatedID, josh)
}
runThis()