-
Notifications
You must be signed in to change notification settings - Fork 0
/
algosigner.html
166 lines (156 loc) · 6.24 KB
/
algosigner.html
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
<html>
<head>
<script
src="https://unpkg.com/[email protected]/dist/browser/algosdk.min.js"
integrity="sha384-N6rRZtVTe4Rvktk9jRWB/jo+NHuNpd2Uh87V0GndIMZbWkKKZZfn1FuzORyMypsZ"
crossorigin="anonymous"
></script>
<script src="/myalgo.min.js" crossorigin="anonymous"></script>
</head>
<body>
<div style="margin: auto; width: 30%">
<form>
<input type="radio" name="network" value="0" checked>TestNet</input>
<input type="radio" name="network" value="1">MainNet</input>
<br>
<input type="radio" name="signer" value="0" checked>AlgoSigner</input>
<input type="radio" name="signer" value="1">MyAlgo</input>
<br>
<label>Transaction (base64):</label>
<br>
<textarea id="txn" name="txn" style="width: 100%; height: 20%">gaN0eG6Ko2FtdM0H0KNmZWXNA+iiZnbOARJkfaNnZW6sdGVzdG5ldC12MS4womdoxCBIY7UYpLPITsgQ8i1PEIHLD3HwWaesIN7GL39w5Qk6IqJsds4BEmhlpG5vdGXECJpx3TAQtoLpo3JjdsQgx/zNsljw1BicK/i21o7ml1CGQrCtAB8x/LkYw1S6hZqjc25kxCCPFU77+/Oqtnap8HWJLczNsVocQVlc2MCxcsXgPvyJ3KR0eXBlo3BheQ==</textarea><br>
<input type="button" value="Sign" onclick="signTxn()"></input>
<form>
<p id="signedTxn" style="overflow-wrap: anywhere">
</p>
<input type="button" value="Send" id="sendTxnButton" onclick="sendTxn()" style="display: none"></input>
<p id="sentTxn" style="overflow-wrap: anywhere">
</p>
</div>
<script crossorigin="anonymous">
const networks = [
{
name: "TestNet",
host: "https://testnet-algorand.api.purestake.io/ps2",
port: '',
token: {
'X-API-Key': "HoCY9Z8KbU2gV2oQkLCPj8GESBP83j271c3gs5iF"
}
},
{
name: "MainNet",
host: "https://mainnet-algorand.api.purestake.io/ps2",
port: "",
token: {
"X-API-Key": "HoCY9Z8KbU2gV2oQkLCPj8GESBP83j271c3gs5iF"
},
}
]
const handlers = [
{
name: "AlgoSigner",
func: txns =>
(AlgoSigner.connect()
.then(() => AlgoSigner.signTxn(txns.map(t => ({txn: t}))))
.then(result => {
signedTxns = result.map(t => base64Decode(t.blob))
base64SignedTxns = result.map(t => t.blob);
})),
},
{
name: "MyAlgo",
func: txns =>
(myAlgoConnect.connect()
.then(accounts => myAlgoConnect.signTransaction(txns))
.then(result => {
signedTxns = result.map(t => t.blob)
base64SignedTxns = result.map(t => base64Encode(t.blob));
})),
}
]
function base64Decode(input) {
return new Uint8Array(atob(input).split("").reduce((arr, chr) => arr.concat([chr.charCodeAt(0)]), []));
}
function base64Encode(input) {
return btoa(input.reduce((str, byte) => str + String.fromCharCode(byte), ""));
}
function handleError(err) {
console.error(err);
sentTxn.innerHTML = "ERROR: " + (err.message || err);
}
const myAlgoConnect = new MyAlgoConnect();
const signingForm = document.forms[0];
let algodClient = null;
let signedTxns = null;
let base64SignedTxns = null;
let network = null;
function prepareTransactions(txns) {
// Add suggested params and compute group ID (if applicable)
// Apparently there are a number of different formats for everything
// so this function is a mess
return (algodClient.getTransactionParams().do()
.then(params => {
txns = txns.map(t => algosdk.decodeObj(t))
for (i in txns) {
if (typeof(txns[i].rcv) === "string")
txns[i].rcv = algosdk.decodeAddress(txns[i].rcv).publicKey
if (typeof(txns[i].snd) === "string")
txns[i].snd = algosdk.decodeAddress(txns[i].snd).publicKey
}
txns = txns.map(t => algosdk.encodeObj(t))
let dtxns = txns.map(txn => ({
suggestedParams: {
...params,
},
...algosdk.decodeUnsignedTransaction(txn)
}))
for (i in dtxns) {
if (dtxns[i].lease.length == 0)
delete dtxns[i].lease
if (dtxns[i].from instanceof Object)
dtxns[i].from = algosdk.encodeAddress(dtxns[i].from.publicKey)
if (dtxns[i].to instanceof Object)
dtxns[i].to = algosdk.encodeAddress(dtxns[i].to.publicKey)
}
dtxns = dtxns.map(dtxn => new algosdk.Transaction(dtxn))
if (dtxns.length > 1) {
const groupID = algosdk.computeGroupID(dtxns);
for (i in dtxns)
dtxns[i].group = groupID;
}
return dtxns.map(dtxn => base64Encode(algosdk.encodeUnsignedTransaction(dtxn)));
}))
}
function signTxn() {
newNetwork = networks[signingForm.network.value];
if (network != newNetwork) {
network = newNetwork;
delete algodClient;
algodClient = new algosdk.Algodv2(network.token, network.host, network.port);
}
signedTxns = null;
base64SignedTxns = null;
signedTxn.innerHTML = "";
sentTxn.innerHTML = "";
sendTxnButton.style.display = "none";
const txns = signingForm.txn.value.split(",")
.map(t => algosdk.decodeObj(base64Decode(t)))
.map(t => ("txn" in t) ? t.txn : t)
.map(t => algosdk.encodeObj(t));
prepareTransactions(txns)
.then(txns => handlers[signingForm.signer.value].func(txns))
.then(() => {
signedTxn.innerHTML = base64SignedTxns;
sendTxnButton.style.display = "inline";
}).catch(err => handleError(err));
}
function sendTxn() {
if (signedTxns == null)
return;
algodClient.sendRawTransaction(signedTxns).do()
.then(response => sentTxn.innerHTML = "Success! Txn: " + response.txId)
.catch(err => handleError(err));
}
</script>
</body>
</html>