Comparing version 0.18.2 to 0.18.3
@@ -17,3 +17,3 @@ import BN from 'bn.js'; | ||
private _ready; | ||
protected readonly ready: Promise<void>; | ||
protected get ready(): Promise<void>; | ||
constructor(connection: Connection, accountId: string); | ||
@@ -20,0 +20,0 @@ fetchState(): Promise<void>; |
@@ -40,3 +40,3 @@ 'use strict'; | ||
if (result && result.error) { | ||
throw new Error(`Quering ${path} failed: ${result.error}.\n${JSON.stringify(result, null, 2)}`); | ||
throw new Error(`Querying ${path} failed: ${result.error}.\n${JSON.stringify(result, null, 2)}`); | ||
} | ||
@@ -43,0 +43,0 @@ return result; |
@@ -0,1 +1,2 @@ | ||
/// <reference types="node" /> | ||
import BN from 'bn.js'; | ||
@@ -60,6 +61,5 @@ import { Enum, Assignable } from './utils/enums'; | ||
export declare function deleteAccount(beneficiaryId: string): Action; | ||
declare class Signature { | ||
declare class Signature extends Assignable { | ||
keyType: KeyType; | ||
data: Uint8Array; | ||
constructor(signature: Uint8Array); | ||
} | ||
@@ -73,2 +73,4 @@ export declare class Transaction extends Assignable { | ||
blockHash: Uint8Array; | ||
encode(): Uint8Array; | ||
static decode(bytes: Buffer): Transaction; | ||
} | ||
@@ -79,2 +81,3 @@ export declare class SignedTransaction extends Assignable { | ||
encode(): Uint8Array; | ||
static decode(bytes: Buffer): SignedTransaction; | ||
} | ||
@@ -92,3 +95,4 @@ export declare class Action extends Enum { | ||
export declare const SCHEMA: Map<Function, any>; | ||
export declare function createTransaction(signerId: string, publicKey: PublicKey, receiverId: string, nonce: number, actions: Action[], blockHash: Uint8Array): Transaction; | ||
export declare function signTransaction(receiverId: string, nonce: number, actions: Action[], blockHash: Uint8Array, signer: Signer, accountId?: string, networkId?: string): Promise<[Uint8Array, SignedTransaction]>; | ||
export {}; |
@@ -81,9 +81,11 @@ 'use strict'; | ||
exports.deleteAccount = deleteAccount; | ||
class Signature { | ||
constructor(signature) { | ||
this.keyType = key_pair_1.KeyType.ED25519; | ||
this.data = signature; | ||
} | ||
class Signature extends enums_1.Assignable { | ||
} | ||
class Transaction extends enums_1.Assignable { | ||
encode() { | ||
return serialize_1.serialize(exports.SCHEMA, this); | ||
} | ||
static decode(bytes) { | ||
return serialize_1.deserialize(exports.SCHEMA, Transaction, bytes); | ||
} | ||
} | ||
@@ -95,2 +97,5 @@ exports.Transaction = Transaction; | ||
} | ||
static decode(bytes) { | ||
return serialize_1.deserialize(exports.SCHEMA, SignedTransaction, bytes); | ||
} | ||
} | ||
@@ -174,11 +179,18 @@ exports.SignedTransaction = SignedTransaction; | ||
]); | ||
function createTransaction(signerId, publicKey, receiverId, nonce, actions, blockHash) { | ||
return new Transaction({ signerId, publicKey, nonce, receiverId, actions, blockHash }); | ||
} | ||
exports.createTransaction = createTransaction; | ||
async function signTransaction(receiverId, nonce, actions, blockHash, signer, accountId, networkId) { | ||
const publicKey = await signer.getPublicKey(accountId, networkId); | ||
const transaction = new Transaction({ signerId: accountId, publicKey, nonce, receiverId, actions, blockHash }); | ||
const transaction = createTransaction(accountId, publicKey, receiverId, nonce, actions, blockHash); | ||
const message = serialize_1.serialize(exports.SCHEMA, transaction); | ||
const hash = new Uint8Array(js_sha256_1.default.sha256.array(message)); | ||
const signature = await signer.signMessage(message, accountId, networkId); | ||
const signedTx = new SignedTransaction({ transaction, signature: new Signature(signature.signature) }); | ||
const signedTx = new SignedTransaction({ | ||
transaction, | ||
signature: new Signature({ keyType: publicKey.keyType, data: signature.signature }) | ||
}); | ||
return [hash, signedTx]; | ||
} | ||
exports.signTransaction = signTransaction; |
@@ -14,7 +14,7 @@ "use strict"; | ||
const amtBN = new BN(balance, 10); | ||
if (amtBN.lte(NEAR_NOMINATION)) { | ||
if (amtBN.lt(NEAR_NOMINATION)) { | ||
return trimTrailingZeroes(`0.${balance.padStart(NEAR_NOMINATION_EXP, '0')}`); | ||
} | ||
const wholePart = amtBN.div(NEAR_NOMINATION).toString(10, 0); | ||
return trimTrailingZeroes(`${formatWithCommas(wholePart)}.${amtBN.mod(NEAR_NOMINATION).toString(10, 0)}`); | ||
return trimTrailingZeroes(`${formatWithCommas(wholePart)}.${amtBN.mod(NEAR_NOMINATION).toString(10, NEAR_NOMINATION_EXP)}`); | ||
} | ||
@@ -45,3 +45,6 @@ exports.formatNearAmount = formatNearAmount; | ||
for (let i = value.length - 1; i >= 0; i--) { | ||
if (value[i] === '.' || value[i] !== '0') { | ||
if (value[i] === '.') { | ||
return value.substring(0, i); | ||
} | ||
else if (value[i] !== '0') { | ||
return value.substring(0, i + 1); | ||
@@ -48,0 +51,0 @@ } |
@@ -23,5 +23,3 @@ 'use strict'; | ||
const encoding = __importStar(require("text-encoding-utf-8")); | ||
if (typeof global.TextDecoder !== 'function') { | ||
global.TextDecoder = encoding.TextDecoder; | ||
} | ||
const TextDecoder = (typeof global.TextDecoder !== 'function') ? encoding.TextDecoder : global.TextDecoder; | ||
const textDecoder = new TextDecoder('utf-8', { fatal: true }); | ||
@@ -28,0 +26,0 @@ function base_encode(value) { |
{ | ||
"name": "nearlib", | ||
"description": "Javascript library to interact with NEAR blockchain", | ||
"version": "0.18.2", | ||
"version": "0.18.3", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "type": "git", |
@@ -56,3 +56,3 @@ 'use strict'; | ||
if (result && result.error) { | ||
throw new Error(`Quering ${path} failed: ${result.error}.\n${JSON.stringify(result, null, 2)}`); | ||
throw new Error(`Querying ${path} failed: ${result.error}.\n${JSON.stringify(result, null, 2)}`); | ||
} | ||
@@ -59,0 +59,0 @@ return result; |
@@ -7,3 +7,3 @@ 'use strict'; | ||
import { Enum, Assignable } from './utils/enums'; | ||
import { serialize } from './utils/serialize'; | ||
import { serialize, deserialize } from './utils/serialize'; | ||
import { KeyType, PublicKey } from './utils/key_pair'; | ||
@@ -81,10 +81,5 @@ import { Signer } from './signer'; | ||
class Signature { | ||
class Signature extends Assignable { | ||
keyType: KeyType; | ||
data: Uint8Array; | ||
constructor(signature: Uint8Array) { | ||
this.keyType = KeyType.ED25519; | ||
this.data = signature; | ||
} | ||
} | ||
@@ -99,2 +94,10 @@ | ||
blockHash: Uint8Array; | ||
encode(): Uint8Array { | ||
return serialize(SCHEMA, this); | ||
} | ||
static decode(bytes: Buffer): Transaction { | ||
return deserialize(SCHEMA, Transaction, bytes); | ||
} | ||
} | ||
@@ -109,2 +112,6 @@ | ||
} | ||
static decode(bytes: Buffer): SignedTransaction { | ||
return deserialize(SCHEMA, SignedTransaction, bytes); | ||
} | ||
} | ||
@@ -197,10 +204,17 @@ | ||
export function createTransaction(signerId: string, publicKey: PublicKey, receiverId: string, nonce: number, actions: Action[], blockHash: Uint8Array): Transaction { | ||
return new Transaction({ signerId, publicKey, nonce, receiverId, actions, blockHash }); | ||
} | ||
export async function signTransaction(receiverId: string, nonce: number, actions: Action[], blockHash: Uint8Array, signer: Signer, accountId?: string, networkId?: string): Promise<[Uint8Array, SignedTransaction]> { | ||
const publicKey = await signer.getPublicKey(accountId, networkId); | ||
const transaction = new Transaction({ signerId: accountId, publicKey, nonce, receiverId, actions, blockHash }); | ||
const transaction = createTransaction(accountId, publicKey, receiverId, nonce, actions, blockHash); | ||
const message = serialize(SCHEMA, transaction); | ||
const hash = new Uint8Array(sha256.sha256.array(message)); | ||
const signature = await signer.signMessage(message, accountId, networkId); | ||
const signedTx = new SignedTransaction({transaction, signature: new Signature(signature.signature) }); | ||
const signedTx = new SignedTransaction({ | ||
transaction, | ||
signature: new Signature({ keyType: publicKey.keyType, data: signature.signature }) | ||
}); | ||
return [hash, signedTx]; | ||
} |
@@ -14,7 +14,7 @@ const BN = require ('bn.js'); | ||
const amtBN = new BN(balance, 10); | ||
if (amtBN.lte(NEAR_NOMINATION)) { | ||
if (amtBN.lt(NEAR_NOMINATION)) { | ||
return trimTrailingZeroes(`0.${balance.padStart(NEAR_NOMINATION_EXP, '0')}`); | ||
} | ||
const wholePart = amtBN.div(NEAR_NOMINATION).toString(10, 0); | ||
return trimTrailingZeroes(`${formatWithCommas(wholePart)}.${amtBN.mod(NEAR_NOMINATION).toString(10, 0)}`); | ||
return trimTrailingZeroes(`${formatWithCommas(wholePart)}.${amtBN.mod(NEAR_NOMINATION).toString(10, NEAR_NOMINATION_EXP)}`); | ||
} | ||
@@ -43,3 +43,5 @@ | ||
for (let i = value.length - 1; i >= 0; i--) { | ||
if (value[i] === '.' || value[i] !== '0') { | ||
if (value[i] === '.') { | ||
return value.substring(0, i); | ||
} else if (value[i] !== '0') { | ||
return value.substring(0, i + 1); | ||
@@ -46,0 +48,0 @@ } |
@@ -8,6 +8,3 @@ 'use strict'; | ||
import * as encoding from 'text-encoding-utf-8'; | ||
if (typeof (global as any).TextDecoder !== 'function') { | ||
(global as any).TextDecoder = encoding.TextDecoder; | ||
} | ||
const TextDecoder = (typeof (global as any).TextDecoder !== 'function') ? encoding.TextDecoder : (global as any).TextDecoder; | ||
const textDecoder = new TextDecoder('utf-8', { fatal: true }); | ||
@@ -14,0 +11,0 @@ |
@@ -44,15 +44,14 @@ | ||
const blockHash = nearlib.utils.serialize.base_decode('244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM'); | ||
const transaction = new nearlib.transactions.Transaction({ | ||
signerId: 'test.near', | ||
publicKey: nearlib.utils.PublicKey.fromString('Anu7LYDfpLtkP7E16LT9imXF694BdQaa9ufVkQiwTQxC'), | ||
nonce: 1, | ||
receiverId: 'whatever.near', | ||
const transaction = nearlib.transactions.createTransaction( | ||
'test.near', | ||
nearlib.utils.PublicKey.fromString('Anu7LYDfpLtkP7E16LT9imXF694BdQaa9ufVkQiwTQxC'), | ||
'whatever.near', | ||
1, | ||
actions, | ||
blockHash | ||
}); | ||
const serialized = nearlib.utils.serialize.serialize(nearlib.transactions.SCHEMA, transaction); | ||
blockHash); | ||
const serialized = transaction.encode(); | ||
expect(serialized.toString('hex')).toEqual('09000000746573742e6e65617200917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d01000000000000000d00000077686174657665722e6e6561720fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6010000000301000000000000000000000000000000'); | ||
const deserialized = nearlib.utils.serialize.deserialize(nearlib.transactions.SCHEMA, nearlib.transactions.Transaction, serialized); | ||
expect(nearlib.utils.serialize.serialize(nearlib.transactions.SCHEMA, deserialized)).toEqual(serialized); | ||
const deserialized = nearlib.transactions.Transaction.decode(serialized); | ||
expect(deserialized.encode()).toEqual(serialized); | ||
}); | ||
@@ -72,4 +71,7 @@ | ||
expect(Buffer.from(signedTx.signature.data).toString('base64')).toEqual('lpqDMyGG7pdV5IOTJVJYBuGJo9LSu0tHYOlEQ+l+HE8i3u7wBZqOlxMQDtpuGRRNp+ig735TmyBwi6HY0CG9AQ=='); | ||
const serialized = nearlib.utils.serialize.serialize(nearlib.transactions.SCHEMA, signedTx); | ||
const serialized = signedTx.encode(); | ||
expect(serialized.toString('hex')).toEqual('09000000746573742e6e65617200917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d01000000000000000d00000077686174657665722e6e6561720fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef601000000030100000000000000000000000000000000969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01'); | ||
const deserialized = nearlib.transactions.SignedTransaction.decode(serialized); | ||
expect(deserialized.encode()).toEqual(serialized); | ||
}); | ||
@@ -76,0 +78,0 @@ |
// Unit tests for simple util code | ||
/* | ||
const nearlib = require('../../lib/index'); | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 50000; | ||
beforeAll(async () => { | ||
}); | ||
beforeEach(async () => { | ||
}); | ||
*/ | ||
test('formatting attonear amounts', async() => { | ||
/*expect(nearlib.utils.format.formatNearAmount('8999999999837087887')).toEqual('0.000008999999999837087887'); | ||
xtest('formatting yoctonear amounts', async() => { | ||
expect(nearlib.utils.format.formatNearAmount('8999999999837087887')).toEqual('0.000008999999999837087887'); | ||
expect(nearlib.utils.format.formatNearAmount('8099099999837087887')).toEqual('0.000008099099999837087887'); | ||
@@ -22,7 +14,12 @@ expect(nearlib.utils.format.formatNearAmount('8099099999837087887')).not.toEqual('0.000008099099999837087888'); | ||
expect(nearlib.utils.format.formatNearAmount('9999989999999998370878870000000')).toEqual('9,999,989.99999999837087887'); | ||
// TODO: do not format smaller values */ | ||
}); | ||
/* | ||
test('converting near to account balance units', async() => { | ||
test('formatting attonear amounts', async() => { | ||
// These tests are in atto near right now. Need to update when the constant changes. | ||
expect(nearlib.utils.format.formatNearAmount('1000000000000000000')).toEqual('1'); | ||
expect(nearlib.utils.format.formatNearAmount('10000000999999997410')).toEqual('10.00000099999999741'); | ||
}); | ||
// TODO: Renable after moving to yoctonear | ||
xtest('converting near to account balance units', async() => { | ||
expect(nearlib.utils.format.parseNearAmount(null)).toEqual(null); | ||
@@ -45,2 +42,2 @@ expect(nearlib.utils.format.parseNearAmount('5.3')).toEqual('5300000000000000000000000'); | ||
} | ||
}); */ | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
1081663
224
18683