diff --git a/README.md b/README.md index d3eb2a3..f07f126 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ const Lamden = require('lamden-js') ## Wallet Functions ### Create a Lamden Keypair +Creates a new wallet object. +- **verifying key (vk)**: public key +- **secret key (sk)**: private key ```javascript let lamdenWallet = Lamden.wallet.new_wallet() @@ -35,6 +38,7 @@ console.log(lamdenWallet) ``` ### Get a public key (vk) from a private key (sk) +Takes the sk as an argument and returns the vk ```javascript let sk = "69a8db3fb7196debc2711fad1fa1935918d09f5d8900d84c3288ea5237611c03" let vk = wallet.get_vk(sk) @@ -44,6 +48,7 @@ console.log(vk) ``` ### Sign a message +Signs a string payload ```javascript const stringBuffer = Buffer.from('message') let messageBytes = new Uint8Array(stringBuffer); @@ -56,6 +61,7 @@ console.log(signedMessage) ``` #### Verify signature +verify a payload ```javascript let validSignature = wallet.verify(vk, messageBytes, signedMessage) diff --git a/con_values_testing.py b/con_values_testing.py new file mode 100644 index 0000000..4cc705d --- /dev/null +++ b/con_values_testing.py @@ -0,0 +1,21 @@ +yourState = Hash(default_value='') + +@export +def test_values(UID: str, Str: str, Int: int, Float: float, Bool: bool, Dict: dict, List: list, ANY: Any, DateTime: datetime.datetime, TimeDelta: datetime.timedelta): + yourState[UID, 'Str'] = Str + assert isinstance(yourState[UID, 'Str'], str), 'Str should be of type str' + yourState[UID, 'Int'] = Int + assert isinstance(yourState[UID, 'Int'], int), 'Int should be of type int' + yourState[UID, 'Float'] = Float + #assert isinstance(yourState[UID, 'Float'], ContractingDecimal), type(yourState[UID, 'Float']) + yourState[UID, 'Bool'] = Bool + assert isinstance(yourState[UID, 'Bool'], bool), 'Bool should be of type bool' + yourState[UID, 'Dict'] = Dict + assert isinstance(yourState[UID, 'Dict'], dict), 'Dict should be of type dict' + yourState[UID, 'List'] = List + assert isinstance(yourState[UID, 'List'], list), 'List should be of type list' + yourState[UID, 'ANY'] = ANY + yourState[UID, 'DateTime'] = DateTime + assert isinstance(yourState[UID, 'DateTime'], datetime.datetime), 'DateTime should be of type DateTime' + yourState[UID, 'TimeDelta'] = TimeDelta + assert isinstance(yourState[UID, 'TimeDelta'], datetime.timedelta), 'TimeDelta should be of type TimeDelta' \ No newline at end of file diff --git a/dist/lamden.js b/dist/lamden.js index 2495ec4..eda1264 100644 --- a/dist/lamden.js +++ b/dist/lamden.js @@ -2392,10 +2392,10 @@ class EventEmitter { } /* - * bignumber.js v9.0.0 + * bignumber.js v9.0.1 * A JavaScript library for arbitrary-precision arithmetic. * https://github.com/MikeMcl/bignumber.js - * Copyright (c) 2019 Michael Mclaughlin + * Copyright (c) 2020 Michael Mclaughlin * MIT Licensed. * * BigNumber.prototype methods | BigNumber methods @@ -4787,7 +4787,7 @@ function clone(configObject) { e = bitFloor((e + 1) / 2) - (e < 0 || e % 2); if (s == 1 / 0) { - n = '1e' + e; + n = '5e' + e; } else { n = s.toExponential(); n = n.slice(0, n.indexOf('e') + 1) + e; @@ -5355,7 +5355,7 @@ function Encoder (type, value) { const encodeDateTime = (val) => { val = !isDate(val) ? new Date(val) : val; if (!isDate(val)) throwError(val); - return [ + return {'__time__': [ val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), @@ -5363,13 +5363,13 @@ function Encoder (type, value) { val.getUTCMinutes(), val.getUTCSeconds(), val.getUTCMilliseconds() - ] + ]} }; const encodeTimeDelta = (val) => { const time = isDate(val) ? val.getTime() : new Date(val).getTime(); const days = parseInt(time / 1000 / 60 / 60 / 24); const seconds = (time - (days * 24 * 60 * 60 * 1000)) / 1000; - return [days, seconds] + return {'__delta__':[days, seconds]} }; const encodeList = (val) => { @@ -5404,7 +5404,7 @@ function Encoder (type, value) { function parseObject (obj) { const encode = (k, v) => { - if (k === "datetime" || k === "datetime.datetime") return Encoder("datetime.datetime", v) + if (k === "datetime" || k === "datetime.datetime" ) return Encoder("datetime.datetime", v) if (k === "timedelta" || k === "datetime.timedelta") return Encoder("datetime.timedelta", v) if (k !== "__fixed__" && isFloat(v)) return encodeFloat(v) return v @@ -5905,7 +5905,7 @@ class TransactionBuilder extends Network { //Set error if txSendResult doesn't exist if (response === 'undefined' || validateTypes$2.isStringWithValue(response)){ this.txSendResult.errors = ['TypeError: Failed to fetch']; - }else{ + }else { if (response.error) this.txSendResult.errors = [response.error]; else this.txSendResult = response; } @@ -5925,31 +5925,31 @@ class TransactionBuilder extends Network { if (typeof res === 'undefined'){ res = {}; res.error = 'TypeError: Failed to fetch'; - }else{ + }else { if (typeof res === 'string') { if (this.txCheckAttempts < this.txCheckLimit){ checkAgain = true; - }else{ + }else { this.txCheckResult.errors = [res]; } - }else{ + }else { if (res.error){ if (res.error === 'Transaction not found.'){ if (this.txCheckAttempts < this.txCheckLimit){ checkAgain = true; - }else{ + }else { this.txCheckResult.errors = [res.error, `Retry Attmpts ${this.txCheckAttempts} hit while checking for Tx Result.`]; } - }else{ + }else { this.txCheckResult.errors = [res.error]; } - }else{ + }else { this.txCheckResult = res; } } } if (checkAgain) timerId = setTimeout(checkTx.bind(this), 1000); - else{ + else { if (validateTypes$2.isNumber(this.txCheckResult.status)){ if (this.txCheckResult.status > 0){ if (!validateTypes$2.isArray(this.txCheckResult.errors)) this.txCheckResult.errors = []; @@ -5968,7 +5968,7 @@ class TransactionBuilder extends Network { if (validateTypes$2.isStringWithValue(result.hash) && validateTypes$2.isStringWithValue(result.success)){ this.txHash = result.hash; this.setPendingBlockInfo(); - }else{ + }else { this.setBlockResultInfo(result); this.txBlockResult = result; } diff --git a/package.json b/package.json index 566def9..b541cec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lamden-js", - "version": "1.21.0", + "version": "1.3.0", "description": "A javascript implementaion for creating wallets, submitting transactions and interacting with masternodes on the Lamden Blockchain.", "main": "dist/lamden.js", "scripts": { diff --git a/src/js/encoder.js b/src/js/encoder.js index 9c4d43f..2a8e073 100644 --- a/src/js/encoder.js +++ b/src/js/encoder.js @@ -68,7 +68,7 @@ function Encoder (type, value) { const encodeDateTime = (val) => { val = !isDate(val) ? new Date(val) : val if (!isDate(val)) throwError(val) - return [ + return {'__time__': [ val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), @@ -76,13 +76,13 @@ function Encoder (type, value) { val.getUTCMinutes(), val.getUTCSeconds(), val.getUTCMilliseconds() - ] + ]} } const encodeTimeDelta = (val) => { const time = isDate(val) ? val.getTime() : new Date(val).getTime() const days = parseInt(time / 1000 / 60 / 60 / 24) const seconds = (time - (days * 24 * 60 * 60 * 1000)) / 1000 - return [days, seconds] + return {'__delta__':[days, seconds]} } const encodeList = (val) => { @@ -117,7 +117,7 @@ function Encoder (type, value) { function parseObject (obj) { const encode = (k, v) => { - if (k === "datetime" || k === "datetime.datetime") return Encoder("datetime.datetime", v) + if (k === "datetime" || k === "datetime.datetime" ) return Encoder("datetime.datetime", v) if (k === "timedelta" || k === "datetime.timedelta") return Encoder("datetime.timedelta", v) if (k !== "__fixed__" && isFloat(v)) return encodeFloat(v) return v diff --git a/test/encoder-test.js b/test/encoder-test.js index f282aad..d48796d 100644 --- a/test/encoder-test.js +++ b/test/encoder-test.js @@ -107,13 +107,13 @@ describe('Test Type Encoder', () => { expect( JSON.stringify(Encoder('dict', '{"vk":"833f3f66de0da4599ca60ae7854256f37404f543cf7a97c328d38aff9d3f8ac7"}')) ).to.be( JSON.stringify({vk:"833f3f66de0da4599ca60ae7854256f37404f543cf7a97c328d38aff9d3f8ac7"}) ) }) it('encodes datetime and float inside a dict from a string', () => { - expect( JSON.stringify(Encoder('dict', {'datetime':new Date(dateString), 'float': 1.1})) ).to.be( '{"datetime":[2020,6,28,19,16,35,59],"float":{"__fixed__":"1.1"}}' ) + expect( JSON.stringify(Encoder('dict', {'datetime':new Date(dateString), 'float': 1.1})) ).to.be( '{"datetime":{"__time__":[2020,6,28,19,16,35,59]},"float":{"__fixed__":"1.1"}}' ) }) it('replaces datetime object with value in dict', () => { - expect( JSON.stringify( Encoder('dict', {'DateTime':{'datetime':new Date(dateString)}})) ).to.be('{"DateTime":[2020,6,28,19,16,35,59]}'); + expect( JSON.stringify( Encoder('dict', {'DateTime':{'datetime':new Date(dateString)}})) ).to.be('{"DateTime":{"__time__":[2020,6,28,19,16,35,59]}}'); }) it('replaces timedelta object with value in dict', () => { - expect( JSON.stringify( Encoder('dict', {'TimeDelta':{'timedelta':1000}})) ).to.be('{"TimeDelta":[0,1]}'); + expect( JSON.stringify( Encoder('dict', {'TimeDelta':{'timedelta':1000}})) ).to.be('{"TimeDelta":{"__delta__":[0,1]}}'); }) it('fails to encode non-objects', () => { expect(() => Encoder('dict', undefined)).to.throwError(); @@ -129,12 +129,12 @@ describe('Test Type Encoder', () => { }) it('encodes fixed and datetime values in the list', () => { expect( JSON.stringify(Encoder('list', [ 1.1, {'datetime':new Date(dateString)}] )) ) - .to.be( '[{"__fixed__":"1.1"},[2020,6,28,19,16,35,59]]' ) + .to.be( '[{"__fixed__":"1.1"},{"__time__":[2020,6,28,19,16,35,59]}]' ) }) it('encodes a list of all values and encodes accordingly', () => { let testList = [1, 1.1, "string", {'datetime':new Date(dateString)}, {'timedelta':1000}, {'TimeDelta':{'timedelta':1000}}, true, [1.1]] expect( JSON.stringify(Encoder('list', testList)) ) - .to.be( '[1,{"__fixed__":"1.1"},"string",[2020,6,28,19,16,35,59],[0,1],{"TimeDelta":[0,1]},true,[{"__fixed__":"1.1"}]]' ) + .to.be( '[1,{"__fixed__":"1.1"},"string",{"__time__":[2020,6,28,19,16,35,59]},{"__delta__":[0,1]},{"TimeDelta":{"__delta__":[0,1]}},true,[{"__fixed__":"1.1"}]]' ) }) it('fails to encode non-list', () => { expect(() => Encoder('list', {})).to.throwError(); @@ -155,22 +155,22 @@ describe('Test Type Encoder', () => { context('DateTime', () => { it('Encodes a Date into a value list', () => { - expect( JSON.stringify(Encoder('datetime.datetime', new Date(dateString))) ).to.be(JSON.stringify([2020, 6, 28, 19, 16, 35, 59])) + expect( JSON.stringify(Encoder('datetime.datetime', new Date(dateString))) ).to.be(JSON.stringify({'__time__':[2020, 6, 28, 19, 16, 35, 59]})) }) it('Encodes a Date string into a value list', () => { - expect( JSON.stringify(Encoder('datetime.datetime', dateString)) ).to.be(JSON.stringify([2020, 6, 28, 19, 16, 35, 59])) + expect( JSON.stringify(Encoder('datetime.datetime', dateString)) ).to.be(JSON.stringify({'__time__':[2020, 6, 28, 19, 16, 35, 59]})) }) it('Encodes milliseconds into a value list', () => { - expect( JSON.stringify(Encoder('datetime.datetime', new Date(dateString).getTime())) ).to.be(JSON.stringify([2020, 6, 28, 19, 16, 35, 59])) + expect( JSON.stringify(Encoder('datetime.datetime', new Date(dateString).getTime())) ).to.be(JSON.stringify({'__time__':[2020, 6, 28, 19, 16, 35, 59]})) }) }) context('TimeDelta', () => { it('Encodes a Date into days seconds', () => { - expect( JSON.stringify(Encoder('datetime.timedelta', new Date(millisecondsDelta))) ).to.be(JSON.stringify([5, 43200])) + expect( JSON.stringify(Encoder('datetime.timedelta', new Date(millisecondsDelta))) ).to.be(JSON.stringify({'__delta__':[5, 43200]})) }) it('Encodes a millisenconds into days seconds', () => { - expect( JSON.stringify(Encoder('datetime.timedelta', millisecondsDelta)) ).to.be(JSON.stringify([5, 43200])) + expect( JSON.stringify(Encoder('datetime.timedelta', millisecondsDelta)) ).to.be(JSON.stringify({'__delta__':[5, 43200]})) }) }) @@ -200,21 +200,21 @@ describe('Test Type Encoder', () => { expect( encodedObj.includes('"bool":true') ).to.be(true) }) it('encodes a datetime.datetime', () => { - expect( encodedObj.includes('"datetime.datetime":[2020,6,28,19,16,35,59]') ).to.be(true) + expect( encodedObj.includes('"datetime.datetime":{"__time__":[2020,6,28,19,16,35,59]}') ).to.be(true) }) it('encodes an datetime.timdelta', () => { - expect( encodedObj.includes('"datetime.timedelta":[5,43200]') ).to.be(true) + expect( encodedObj.includes('"datetime.timedelta":{"__delta__":[5,43200]}') ).to.be(true) }) it('encodes an list', () => { expect( encodedObj - .includes('"list":[1,{"__fixed__":"1.1"},"this is a string",true,[1,2,3,4,5,6,7],[0,1234567],[{"__fixed__":"1.1"}],{"fixed":{"__fixed__":"1.1"},"DateTime":[2020,6,28,19,16,35,59],"TimeDelta":[5,43200]}]') ) + .includes('"list":[1,{"__fixed__":"1.1"},"this is a string",true,[1,2,3,4,5,6,7],[0,1234567],[{"__fixed__":"1.1"}],{"fixed":{"__fixed__":"1.1"},"DateTime":{"__time__":[2020,6,28,19,16,35,59]},"TimeDelta":{"__delta__":[5,43200]}}]') ) .to.be(true) }) it('encodes a dict/object', () => { expect( encodedObj - .includes('"dict":{"integer":1,"float":{"__fixed__":"1.1"},"list":[1,{"__fixed__":"1.1"},"this is a string",true,[1,2,3,4,5,6,7],[0,1234567],[{"__fixed__":"1.1"}],{"fixed":{"__fixed__":"1.1"},"DateTime":[2020,6,28,19,16,35,59],"TimeDelta":[5,43200]}],"str":"this is a string","bool":true,"datetime.datetime":[2020,6,28,19,16,35,59],"datetime.timedelta":[5,43200]}') ) + .includes('"dict":{"integer":1,"float":{"__fixed__":"1.1"},"list":[1,{"__fixed__":"1.1"},"this is a string",true,[1,2,3,4,5,6,7],[0,1234567],[{"__fixed__":"1.1"}],{"fixed":{"__fixed__":"1.1"},"DateTime":{"__time__":[2020,6,28,19,16,35,59]},"TimeDelta":{"__delta__":[5,43200]}}],"str":"this is a string","bool":true,"datetime.datetime":{"__time__":[2020,6,28,19,16,35,59]},"datetime.timedelta":{"__delta__":[5,43200]}}') ) .to.be(true) }) }) diff --git a/test/transactionBatcher-test.js b/test/transactionBatcher-test.js index 4460786..5135b8f 100644 --- a/test/transactionBatcher-test.js +++ b/test/transactionBatcher-test.js @@ -132,7 +132,7 @@ describe('Test TransactionBuilder class', () => { }) context('TransactionBatcher.sendBatch()', () => { it('can send a batch of successful transactions', async function () { - this.timeout(30000); + this.timeout(60000); let txb = new Lamden.TransactionBatcher(networkInfo) let response = await txb.getStartingNonce(senderWallet1.vk) @@ -155,7 +155,7 @@ describe('Test TransactionBuilder class', () => { context('TransactionBatcher.sendAllBatches()', () => { it('Can send batches from all senders', async function () { - this.timeout(30000); + this.timeout(60000); sleep(1500) let txb = new Lamden.TransactionBatcher(networkInfo) const txList1 = makeTxList(senderWallet1.vk, recieverWallet.vk, 15) @@ -169,7 +169,7 @@ describe('Test TransactionBuilder class', () => { if (!txBuilder.txSendResult.hash) console.log(txBuilder.nonce + ": " + txBuilder.txSendResult.errors) expect(typeof txBuilder.txSendResult.hash === 'string').to.be(true) }) - console.log(txb) + expect(txb.hasTransactions()).to.be(false) }) it('Can process overflow', async function () { diff --git a/test/transactionBuilder-test.js b/test/transactionBuilder-test.js index 8f444de..9e5fe7e 100644 --- a/test/transactionBuilder-test.js +++ b/test/transactionBuilder-test.js @@ -37,7 +37,7 @@ let kwargs = { let valuesTxInfo = { senderVk: senderWallet.vk, - contractName: 'con_values_testing', + contractName: 'con_values_testing_2', methodName: 'test_values', stampLimit: 100, kwargs: { @@ -284,6 +284,7 @@ describe('Test TransactionBuilder class', () => { //Send Transaction let response = await newTx.send(senderWallet.sk); + expect(response.success).to.be("Transaction successfully submitted to the network.") //Check Transaction