In Andreas Antonopoulos lecture BitcoinSouth, Money As A Content Type he mentions a transaction is simply a message broadcast to the network. Exactly, a bitcoin transaction is nothing more than a set of bytes transmit to the network. He talks about encoding the transaction message in other forms, let’s take that to practice. Encoding messages in other forms is an old art called Stenography. We will encode the transaction in an image. The package python-stepic is required.
1
|
sudo apt-get install python-stepic
|
A simple transaction represented as hexadecimal digits will be about 600 bytes long. We run the program with:
1
|
python content.py
|
The program will ask a receival address and amount, and encode the transaction in an image:
1
2
3
4
5
6
7
|
$ python encode.py
Your brain wallet password: **********************
Address : 1HoSFymoqteYrmmr7s3jDDqmggoxacbk37
Where to send: 1H2PmMHdLRNouJLbsKErksT7yMD6gfNATR
How much: 10000
Encoding transaction in image...
Image out.jpg contains the transaction
|
Encoding program (encode.py):
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
|
#!/usr/bin/env python
# Encoding bitcoin transactions
from pybitcointools import *
import base64
import binascii
import Image, operator
import stepic
class Wallet:
priv = ""
pub = ""
address = ""
def createWallet(self,brainwalletpassword):
self.priv = sha256(brainwalletpassword)
self.pub = privtopub(self.priv)
self.address = pubtoaddr(self.pub)
print "Address : " + self.address
def sendBTCt(self,dst, amount):
h = history(self.address)
outs = [{'value': amount, 'address': dst}]
tx = mksend(h,outs, self.address, 10000)
tx2 = sign(tx,0,self.priv)
return tx2
#pushtx(tx2)
# Create wallet
x = Wallet()
password = raw_input("Your brain wallet password: ")
x.createWallet(password)
# Create transaction
to = raw_input("Where to send: ")
amount = raw_input("How much: ")
tx = x.sendBTCt(to, int(amount) )
print "Encoding transaction in image..."
im = Image.open('in.jpg')
s = stepic.Steganographer(im)
im2 = s.encode(tx)
im2.save('out.jpg','JPEG')
|
The decoding program (decode.py):
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/usr/bin/env python
# Encoding bitcoin transactions
from pybitcointools import *
import base64
import binascii
import Image, operator
import stepic
print "Decoding image"
im = Image.open('out.png')
s = stepic.decode(im)
data = s.decode()
print data
|
Finally, the receiver of the content can decode the transaction from the image and push it to the bitcoin network:
1
2
3
|
python decode.py out.png
Decoding image
0100000002dd684b97715af043465aa7aee9f86d5cce74e0172f9f751bb15b08c4a0c70fda000000008b48304502200148397cf8befe7f884aac20f4fea0a7d43323a22c5d7f21137b878b7865075402210088d669c041e37921a6e792d4867fa3bb8e026a0a25c8916ecba3aacf7a1d67f801410487d82042d93447008dfe2af762068a1e53ff394a5bf8f68a045fa642b99ea5d153f577dd2dba6c7ae4cfd7b6622409d7edd2d76dd13a8092cd3af97b77bd2c77ffffffff23b6ed6ee112b74a4c2d7bcd4b6f11982f8b1ebe0f53f1fd849154734f1f9b030000000000ffffffff02e8030000000000001976a914afc56e7537553ccfc66734d8f02c7bcdec82375988ac79e20100000000001976a914b84a3b37524e049979df3b4e715cac485385d40088ac00000000
|