Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change mnemonicToSeed to synchronous version to avoid slice error #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"dependencies": {
"bignumber.js": "^7.2.1",
"bip39": "^2.5.0",
"bip39": "^3.0.2",
"bs58check": "^2.1.1",
"buffer": "^5.1.0",
"codecov": "^2.3.1",
Expand Down
102 changes: 51 additions & 51 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof Buffer == "undefined") Buffer = require("buffer/").Buffer;
if (typeof XMLHttpRequest == "undefined") XMLHttpRequest = require('xhr2');
const BN = require("bignumber.js");
const
const
//CLI below
defaultProvider = "https://mainnet.tezrpc.me/",
counters = {},
Expand All @@ -16,24 +16,24 @@ prefix = {
tz2: new Uint8Array([6, 161, 161]),
tz3: new Uint8Array([6, 161, 164]),
KT: new Uint8Array([2,90,121]),


edpk: new Uint8Array([13, 15, 37, 217]),
edsk2: new Uint8Array([13, 15, 58, 7]),
spsk: new Uint8Array([17, 162, 224, 201]),
p2sk: new Uint8Array([16,81,238,189]),

sppk: new Uint8Array([3, 254, 226, 86]),
p2pk: new Uint8Array([3, 178, 139, 127]),

edesk: new Uint8Array([7, 90, 60, 179, 41]),

edsk: new Uint8Array([43, 246, 78, 7]),
edsig: new Uint8Array([9, 245, 205, 134, 18]),
spsig1: new Uint8Array([13, 115, 101, 19, 63]),
p2sig: new Uint8Array([54, 240, 44, 52]),
sig: new Uint8Array([4, 130, 43]),

Net: new Uint8Array([87, 82, 0]),
nce: new Uint8Array([69, 220, 169]),
b: new Uint8Array([1,52]),
Expand Down Expand Up @@ -266,11 +266,11 @@ crypto = {
if (typeof esk == 'undefined') return false;
if (typeof password == 'undefined') return false;
if (typeof window.crypto.subtle == 'undefined') return false;

const esb = utility.b58cdecode(esk, prefix.edesk);
const salt = esb.slice(0, 8);
const esm = esb.slice(8);

return window.crypto.subtle.importKey('raw', new TextEncoder('utf-8').encode(password), {name: 'PBKDF2'}, false, ['deriveBits']).then(function(key){
console.log(key);
return window.crypto.subtle.deriveBits(
Expand All @@ -281,7 +281,7 @@ crypto = {
hash: {name: 'SHA-512'}
},
key,
256
256
);
}).then(function(key){
console.log(key);
Expand Down Expand Up @@ -330,7 +330,7 @@ crypto = {
}
},
generateKeys: function (m, p) {
const s = library.bip39.mnemonicToSeed(m, p).slice(0, 32);
const s = library.bip39.mnemonicToSeedSync(m, p).slice(0, 32);
const kp = library.sodium.crypto_sign_seed_keypair(s);
return {
mnemonic: m,
Expand Down Expand Up @@ -376,7 +376,7 @@ node = {
if (typeof o === 'undefined') {
if (typeof t === 'undefined') {
t = "GET";
} else
} else
o = {};
} else {
if (typeof t === 'undefined')
Expand Down Expand Up @@ -407,7 +407,7 @@ node = {
if (node.debugMode)
console.log(e, o, http.responseText);
reject(http.responseText);
} else {
} else {
if (node.debugMode)
console.log(e, o, http.statusText);
reject(http.statusText);
Expand All @@ -421,7 +421,7 @@ node = {
};
if (t == 'POST'){
http.setRequestHeader("Content-Type", "application/json");
http.send(JSON.stringify(o));
http.send(JSON.stringify(o));
} else {
http.send();
}
Expand Down Expand Up @@ -462,7 +462,7 @@ rpc = {
getHeadHash: function () {
return node.query("/chains/main/blocks/head/hash");
},

getBallotList: function(){
return node.query("/chains/main/blocks/head/votes/ballot_list");
},
Expand All @@ -484,7 +484,7 @@ rpc = {
getCurrentQuorum: function(){
return node.query("/chains/main/blocks/head/votes/current_quorum ");
},

awaitOperation : function(hash, interval, timeout){
if (typeof interval == 'undefined') '30';
if (typeof timeout == 'undefined') '180';
Expand Down Expand Up @@ -561,7 +561,7 @@ rpc = {
if (typeof ops[i].gas_limit == 'undefined') ops[i].gas_limit = "0";
if (typeof ops[i].storage_limit == 'undefined') ops[i].storage_limit = "0";
ops[i].counter = (counters[from]++).toString();

ops[i].fee = ops[i].fee.toString();
ops[i].gas_limit = ops[i].gas_limit.toString();
ops[i].storage_limit = ops[i].storage_limit.toString();
Expand All @@ -577,7 +577,7 @@ rpc = {
},
simulateOperation : function(from, operation, keys){
return rpc.prepareOperation(from, operation, keys).then(function(fullOp){
return node.query('/chains/main/blocks/head/helpers/scripts/run_operation', fullOp.opOb)
return node.query('/chains/main/blocks/head/helpers/scripts/run_operation', fullOp.opOb)
});
},
sendOperation: function (from, operation, keys, skipPrevalidation, revealFee) {
Expand Down Expand Up @@ -611,7 +611,7 @@ rpc = {
if (typeof f[i].contents[j].metadata.operation_result != 'undefined' && f[i].contents[j].metadata.operation_result.status == "failed")
errors = errors.concat(f[i].contents[j].metadata.operation_result.errors);
}
}
}
if (errors.length) throw {error: "Operation Failed", errors:errors};
return node.query('/injection/operation', sopbytes);
}).then(function (f) {
Expand All @@ -628,7 +628,7 @@ rpc = {
};
});
},

account: function (keys, amount, spendable, delegatable, delegate, fee, gasLimit, storageLimit) {
if (typeof gasLimit == 'undefined') gasLimit = '10000';
if (typeof storageLimit == 'undefined') storageLimit = '257';
Expand Down Expand Up @@ -709,7 +709,7 @@ rpc = {
};
return rpc.sendOperation(keys.pkh, operation, keys);
},

activate: function (pkh, secret) {
var operation = {
"kind": "activate_account",
Expand All @@ -718,7 +718,7 @@ rpc = {
};
return rpc.sendOperation(pkh, operation, false);
},

typecheckCode(code) {
var _code = utility.ml2mic(code);
return node.query("/chains/main/blocks/head/helpers/scripts/typecheck_code", {program : _code, gas : "10000"});
Expand Down Expand Up @@ -768,7 +768,7 @@ contract = {
if (typeof storageLimit == 'undefined') storageLimit = '10000';
return rpc.originate(keys, amount, code, init, spendable, delegatable, delegate, fee, gasLimit, storageLimit);
},
send: function (contract, from, keys, amount, parameter, fee, gasLimit, storageLimit) {
send: function (contract, from, keys, amount, parameter, fee, gasLimit, storageLimit) {
if (typeof gasLimit == 'undefined') gasLimit = '2000';
if (typeof storageLimit == 'undefined') storageLimit = '0';
return rpc.transfer(from, keys, contract, amount, fee, parameter, gasLimit, storageLimit);
Expand All @@ -778,7 +778,7 @@ contract = {
},
storage: function (contract) {
return new Promise(function (resolve, reject) {
eztz.node.query("/chains/main/blocks/head/context/contracts/" + contract +
eztz.node.query("/chains/main/blocks/head/context/contracts/" + contract +
"/storage").then(function (r) {
resolve(r);
}).catch(function (e) {
Expand Down Expand Up @@ -806,15 +806,15 @@ contract = {
tezos = {
forge : function(head, opOb, validateLocalForge){
if (typeof validateLocalForge == 'undefined') validateLocalForge = true;


return node.query('/chains/'+head.chain_id+'/blocks/'+head.hash+'/helpers/forge/operations', opOb).then(function(remoteForgedBytes){
var localForgedBytes;
localForgedBytes = utility.buf2hex(utility.b58cdecode(opOb.branch, prefix.b));
for(var i = 0; i < opOb.contents.length; i++){
localForgedBytes += forgeOp(opOb.contents[i]);
}

opOb.protocol = head.protocol;
if (localForgedBytes == remoteForgedBytes) return {
opbytes : localForgedBytes,
Expand Down Expand Up @@ -865,12 +865,12 @@ tezos = {
const positive_mark = num.toString(2)[0] === '-' ? '1' : '0';
const binary = num.toString(2).replace('-', '');
const pad = binary.length <= 6 ? 6 : ((binary.length - 6) % 7 ? binary.length + 7 - (binary.length - 6) % 7 : binary.length);

const splitted = binary.padStart(pad, '0').match(/\d{6,7}/g);
const reversed = splitted.reverse();

reversed[0] = positive_mark + reversed[0];
const num_hex = reversed.map(function(x, i){
const num_hex = reversed.map(function(x, i){
return parseInt((i === reversed.length - 1 ? '0' : '1') + x, 2)
.toString(16)
.padStart(2, '0')
Expand Down Expand Up @@ -898,15 +898,15 @@ tezos = {
},
decodeRawBytes : function (bytes) {
bytes = bytes.toUpperCase()

let index = 0

const read = function(len) { return bytes.slice(index, index + len) };

const rec = function(){
const b = read(2)
const prim = prim_mapping[b]

if (prim instanceof Object) {

index += 2
Expand All @@ -925,7 +925,7 @@ tezos = {

const string_hex_lst = read(annots_len).match(/[\dA-F]{2}/g)
index += annots_len

if (string_hex_lst) {
const string_bytes = new Uint8Array(string_hex_lst.map(function(x) { return parseInt(x, 16)}))
const string_result = new TextDecoder('utf-8').decode(string_bytes)
Expand Down Expand Up @@ -1016,7 +1016,7 @@ trezor = {
var bytes = utility.b58cdecode(address, pp);
if (tag == 1) {
bytes = utility.mergebuf(bytes, [0])
} else {
} else {
bytes = utility.mergebuf([curve], bytes)
}
return {
Expand All @@ -1031,7 +1031,7 @@ trezor = {
var bytes = utility.b58cdecode(address, pp);
if (tag == 1) {
bytes = utility.mergebuf(bytes, [0])
} else {
} else {
bytes = utility.mergebuf([curve], bytes)
}
hex = utility.buf2hex(utility.mergebuf([tag], bytes));
Expand Down Expand Up @@ -1220,17 +1220,17 @@ var op_mapping_reverse = (function(){
})()

var prim_mapping = {
'00': 'int',
'01': 'string',
'02': 'seq',
'03': {name: 'prim', len: 0, annots: false},
'00': 'int',
'01': 'string',
'02': 'seq',
'03': {name: 'prim', len: 0, annots: false},
'04': {name: 'prim', len: 0, annots: true},
'05': {name: 'prim', len: 1, annots: false},
'06': {name: 'prim', len: 1, annots: true},
'07': {name: 'prim', len: 2, annots: false},
'08': {name: 'prim', len: 2, annots: true},
'05': {name: 'prim', len: 1, annots: false},
'06': {name: 'prim', len: 1, annots: true},
'07': {name: 'prim', len: 2, annots: false},
'08': {name: 'prim', len: 2, annots: true},
'09': {name: 'prim', len: 3, annots: true},
'0A': 'bytes'
'0A': 'bytes'
}
var prim_mapping_reverse = {
[0]: {
Expand Down Expand Up @@ -1266,8 +1266,8 @@ function forgeOp(op){
var fop;
fop = eztz.utility.buf2hex(new Uint8Array([forgeOpTags[op.kind]]));
switch (forgeOpTags[op.kind]) {
case 0:
case 1:
case 0:
case 1:
fop += eztz.utility.buf2hex(toBytesInt32(op.level));
if (forgeOpTags[op.kind] == 0) break;
fop += op.nonce;
Expand All @@ -1281,8 +1281,8 @@ function forgeOp(op){
fop += eztz.utility.buf2hex(eztz.utility.b58cdecode(op.pkh, eztz.prefix.tz1));
fop += op.secret;
if (forgeOpTags[op.kind] == 4) break;
case 5:
case 6:
case 5:
case 6:
fop += forgePublicKeyHash(op.source);
fop += eztz.utility.buf2hex(toBytesInt32(op.period));
if (forgeOpTags[op.kind] == 5) {
Expand All @@ -1293,10 +1293,10 @@ function forgeOp(op){
fop += (op.ballot == "yay" ? "00" : (op.ballot == "nay" ? "01" : "02"));
break;
}
case 7:
case 8:
case 9:
case 10:
case 7:
case 8:
case 9:
case 10:
fop += forgeAddress(op.source);
fop += forgeZarith(op.fee);
fop += forgeZarith(op.counter);
Expand Down
Loading