-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbsvabi.js
More file actions
239 lines (199 loc) · 5.29 KB
/
Copy pathbsvabi.js
File metadata and controls
239 lines (199 loc) · 5.29 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const _Buffer = require('buffer/');
const Validators = require('./src/validators');
const Transaction = require('./src/transaction');
const Signature = require('./src/signature');
const isNode = typeof window === 'undefined';
const ECDSA = require('./bsv/lib/crypto/ecdsa');
const Script = require('./bsv/lib/script');
const Opcode = require('./bsv/lib/opcode');
const bsvSignature = require('./bsv/lib/crypto/signature');
const bsvMessage = require('./bsv/lib/message');
class BSVABI {
constructor(abi, options = {}) {
this.network = options.network || 'mainnet';
this.options = options;
this.abi = abi;
this.args = [];
if (options.action) {
this.action(options.action);
}
}
static get bitcoin() {
return require('./bsv');
}
static get IES() {
return require('./bsv/ecies');
}
static get Mnemonic() {
return require('./bsv/mnemonic');
}
static get Transaction() {
return Transaction;
}
get fs() {
return isNode ? eval(`require('fs')`) : {};
}
get path() {
return isNode ? eval(`require('path')`) : {};
}
signaturePublicKey() {
const signature = this.args[this.action.args.findIndex(e => e.type === 'Signature')];
const ecdsa = new ECDSA();
ecdsa.hashbuf = bsvMessage(this.contentHash()).magicHash();
ecdsa.sig = bsvSignature.fromCompact(Buffer.from(signature, 'base64'));
return ecdsa.toPublicKey().toString('hex');
}
action(actionName) {
const action = this.abi.actions[actionName];
if (!action) {
throw new Error('BSVABI Error: action not found in abi schema');
}
this.actionName = actionName;
this.action = action;
return this;
}
fromObject(object) {
this.args = this.action.args.map(
(e, i) => object[e.name] || this.args[i] || e.value || e.replaceValue || e.defaultValue
);
this.validate();
return this;
}
fromFile(filepath) {
const file = this.fs.readFileSync(filepath);
this.args = this.action.args.map(
(e, i) => this.args[i] || e.value || e.replaceValue || e.defaultValue
);
this.args[this.action.encodingIndex] = 'binary';
this.args[this.action.contentIndex] = file;
this.args[this.action.filenameIndex] = this.path.basename(filepath);
this.args[this.action.contentTypeIndex] = {
'.txt': 'text/plain',
'.jpeg': 'image/jpeg',
'.jpg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.tiff': 'image/tiff',
'.jgd': 'image/jgd',
'.bmp': 'image/bmp',
'.webp': 'image/webp',
'.mp4': 'video/mp4',
'.mp3': 'audio/mp3'
}[this.path.extname(filepath)];
this.validate();
return this;
}
fromTx(tx) {
const transaction = Transaction.decodeTx(tx, this.network);
this.decodedTx = transaction;
const dataOutput = transaction.vout.find(e => e.scriptPubKey.opReturn);
this.args = dataOutput.scriptPubKey.opReturn.parts;
this.action.args.map((e, i) => {
if (!e.encodingIndex) {
return;
}
const encoding = this.args[e.encodingIndex];
if (encoding === 'binary') {
this.args[i] = dataOutput.scriptPubKey.opReturn.bufferParts[i];
}
});
this.validate();
return this;
}
fromArgs(args) {
this.args = args;
this.validate();
return this;
}
toChunks() {
return this.args.map(e => Buffer.from(e));
}
toArray() {
return this.args;
}
toObject() {
return this.action.args
.map((e, i) => ({ ...e, value: this.args[i] === 'null' ? null : this.args[i] }))
.reduce((a, e) => Object.assign(a, { [e.name]: e.value }), {});
}
toFile() {
const path = `${__dirname}/${this.args[this.action.filenameIndex]}`;
this.fs.writeFileSync(path, this.args[this.action.contentIndex]);
}
toScript() {
let s = new Script();
s.add(Opcode.OP_FALSE);
s.add(Opcode.OP_RETURN);
this.args.forEach(function(item) {
let buffer = _Buffer.Buffer.from(item);
s.add(buffer);
});
return s;
}
contentHash(index) {
if (!this.action.args.length) {
return;
}
let arg = this.action.args[index];
if (!arg) {
arg = this.action.args.find(e => e.type === 'Signature');
}
const value = Buffer.concat(
this.toChunks().slice(arg.messageStartIndex || 0, arg.messageEndIndex + 1 || index - 1)
);
return Signature.sha256(value);
}
async replace(options) {
const replacements = Object.assign(
{
'#{mySignature}': async index => {
if (this.options.sign) {
return this.options.sign(this.contentHash(index));
}
},
'#{myAddress}': async index => {
if (this.options.address) {
return this.options.address();
}
},
'#{invoice}': async index => {
if (this.options.invoice) {
return await this.options.invoice();
}
}
},
options
);
for (let i in this.args) {
const e = this.args[i];
if (this.action.args[i].replaceValue === e) {
const replacement = replacements[e];
if (replacement) {
this.args[i] = (await replacement(i)) || e;
}
}
}
return this;
}
validate() {
if (this.options.disableValidation) {
return;
}
const errors = [];
this.action.args.forEach((e, i) => {
const value = this.args[i];
const validator = Validators[e.type];
if (!e.type) {
return;
}
if (!validator) {
return errors.push(`unsupported type '${e.type}' for argument ${e.name}: '${value}'`);
}
validator(value, e, errors, this.args, this.action.args, i);
});
if (errors.length) {
throw new Error(errors.join('\n'));
}
}
}
module.exports = BSVABI;