stellar-sdk
Advanced tools
Comparing version 2.2.1 to 2.2.2
@@ -5,2 +5,12 @@ # Changelog | ||
## [v2.2.2](https://github.com/stellar/js-stellar-sdk/compare/v2.2.1...v2.2.2) | ||
### Fix | ||
- Fix manage data value in SEP0010 challenge builder. ([#396](https://github.com/stellar/js-stellar-sdk/issues/396)) | ||
### Add | ||
- Add support for networkPassphrase in SEP0010 challenge builder. ([#397](https://github.com/stellar/js-stellar-sdk/issues/397)) | ||
## [v2.2.1](https://github.com/stellar/js-stellar-sdk/compare/v2.2.0...v2.2.1) | ||
@@ -7,0 +17,0 @@ |
import { Keypair, Transaction } from "stellar-base"; | ||
export declare namespace Utils { | ||
function buildChallengeTx(serverKeypair: Keypair, clientAccountID: string, anchorName: string, timeout?: number): string; | ||
function verifyChallengeTx(challengeTx: string, serverAccountId: string): boolean; | ||
function buildChallengeTx(serverKeypair: Keypair, clientAccountID: string, anchorName: string, timeout?: number, networkPassphrase?: string): string; | ||
function verifyChallengeTx(challengeTx: string, serverAccountId: string, networkPassphrase?: string): boolean; | ||
function verifyTxSignedBy(transaction: Transaction, accountId: string): boolean; | ||
} |
@@ -9,8 +9,10 @@ "use strict"; | ||
(function (Utils) { | ||
function buildChallengeTx(serverKeypair, clientAccountID, anchorName, timeout) { | ||
function buildChallengeTx(serverKeypair, clientAccountID, anchorName, timeout, networkPassphrase) { | ||
if (timeout === void 0) { timeout = 300; } | ||
var account = new stellar_base_1.Account(serverKeypair.publicKey(), "-1"); | ||
var now = Math.floor(Date.now() / 1000); | ||
var value = randombytes_1.default(48).toString("base64"); | ||
var transaction = new stellar_base_1.TransactionBuilder(account, { | ||
fee: stellar_base_1.BASE_FEE, | ||
networkPassphrase: networkPassphrase, | ||
timebounds: { | ||
@@ -23,3 +25,3 @@ minTime: now, | ||
name: anchorName + " auth", | ||
value: randombytes_1.default(64), | ||
value: value, | ||
source: clientAccountID, | ||
@@ -35,4 +37,4 @@ })) | ||
Utils.buildChallengeTx = buildChallengeTx; | ||
function verifyChallengeTx(challengeTx, serverAccountId) { | ||
var transaction = new stellar_base_1.Transaction(challengeTx); | ||
function verifyChallengeTx(challengeTx, serverAccountId, networkPassphrase) { | ||
var transaction = new stellar_base_1.Transaction(challengeTx, networkPassphrase); | ||
var sequence = Number.parseInt(transaction.sequence, 10); | ||
@@ -55,2 +57,8 @@ if (sequence !== 0) { | ||
} | ||
if (Buffer.from(operation.value.toString(), "base64").length !== 48) { | ||
throw new errors_1.InvalidSep10ChallengeError("The transaction's operation value should be a 64 bytes base64 random string"); | ||
} | ||
if (operation.type !== "manageData") { | ||
throw new errors_1.InvalidSep10ChallengeError("The transaction's operation should be manageData"); | ||
} | ||
if (!verifyTxSignedBy(transaction, serverAccountId)) { | ||
@@ -57,0 +65,0 @@ throw new errors_1.InvalidSep10ChallengeError("The transaction is not signed by the server"); |
{ | ||
"name": "stellar-sdk", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"description": "stellar-sdk is a library for working with the Stellar Horizon server.", | ||
@@ -144,3 +144,3 @@ "main": "./lib/index.js", | ||
"randombytes": "^2.1.0", | ||
"stellar-base": "^1.0.3", | ||
"stellar-base": "^1.1.1", | ||
"toml": "^2.3.0", | ||
@@ -147,0 +147,0 @@ "tslib": "^1.10.0", |
@@ -27,9 +27,8 @@ import randomBytes from "randombytes"; | ||
* @param {number} [timeout=300] Challenge duration (default to 5 minutes). | ||
* @param {string} [networkPassphrase] The network passphrase. If you pass this argument then timeout is required. | ||
* @example | ||
* import { Utils, Keypair, Network } from 'stellar-sdk' | ||
* import { Utils, Keypair, Networks } from 'stellar-sdk' | ||
* | ||
* Network.useTestNetwork(); | ||
* | ||
* let serverKeyPair = Keypair.fromSecret("server-secret") | ||
* let challenge = Utils.buildChallengeTx(serverKeyPair, "client-stellar-account-id", "SDF", 300) | ||
* let challenge = Utils.buildChallengeTx(serverKeyPair, "client-stellar-account-id", "SDF", 300, Networks.TESTNET) | ||
* @returns {string} A base64 encoded string of the raw TransactionEnvelope xdr struct for the transaction. | ||
@@ -42,2 +41,3 @@ */ | ||
timeout: number = 300, | ||
networkPassphrase?: string, | ||
): string { | ||
@@ -47,4 +47,12 @@ const account = new Account(serverKeypair.publicKey(), "-1"); | ||
// A Base64 digit represents 6 bits, to generate a random 64 bytes | ||
// base64 string, we need 48 random bytes = (64 * 6)/8 | ||
// | ||
// Each Base64 digit is in ASCII and each ASCII characters when | ||
// turned into binary represents 8 bits = 1 bytes. | ||
const value = randomBytes(48).toString("base64"); | ||
const transaction = new TransactionBuilder(account, { | ||
fee: BASE_FEE, | ||
networkPassphrase, | ||
timebounds: { | ||
@@ -58,3 +66,3 @@ minTime: now, | ||
name: `${anchorName} auth`, | ||
value: randomBytes(64), | ||
value, | ||
source: clientAccountID, | ||
@@ -91,8 +99,7 @@ }), | ||
* @param {string} serverAccountID The server's stellar account. | ||
* @param {string} [networkPassphrase] The network passphrase. If you pass this argument then timeout is required. | ||
* @example | ||
* import { Utils, Network } from 'stellar-sdk' | ||
* import { Utils, Networks } from 'stellar-sdk' | ||
* | ||
* Network.useTestNetwork(); | ||
* | ||
* let challenge = Utils.verifyChallengeTx("base64tx", "server-account-id") | ||
* let challenge = Utils.verifyChallengeTx("base64tx", "server-account-id", Networks.TESTNET) | ||
* @returns {boolean} | ||
@@ -103,4 +110,5 @@ */ | ||
serverAccountId: string, | ||
networkPassphrase?: string, | ||
): boolean { | ||
const transaction = new Transaction(challengeTx); | ||
const transaction = new Transaction(challengeTx, networkPassphrase); | ||
@@ -141,2 +149,14 @@ const sequence = Number.parseInt(transaction.sequence, 10); | ||
if (Buffer.from(operation.value.toString(), "base64").length !== 48) { | ||
throw new InvalidSep10ChallengeError( | ||
"The transaction's operation value should be a 64 bytes base64 random string", | ||
); | ||
} | ||
if (operation.type !== "manageData") { | ||
throw new InvalidSep10ChallengeError( | ||
"The transaction's operation should be manageData", | ||
); | ||
} | ||
if (!verifyTxSignedBy(transaction, serverAccountId)) { | ||
@@ -143,0 +163,0 @@ throw new InvalidSep10ChallengeError( |
const randomBytes = require("randombytes"); | ||
describe('Utils', function() { | ||
let clock; | ||
let clock, txBuilderOpts; | ||
beforeEach(function() { | ||
clock = sinon.useFakeTimers(); | ||
StellarSdk.Network.useTestNetwork(); | ||
txBuilderOpts = { | ||
fee: 100, | ||
networkPassphrase: StellarSdk.Networks.TESTNET | ||
}; | ||
}); | ||
@@ -22,6 +25,8 @@ | ||
"GBDIT5GUJ7R5BXO3GJHFXJ6AZ5UQK6MNOIDMPQUSMXLIHTUNR2Q5CFNF", | ||
"SDF" | ||
"SDF", | ||
300, | ||
StellarSdk.Networks.TESTNET | ||
); | ||
const transaction = new StellarSdk.Transaction(challenge); | ||
const transaction = new StellarSdk.Transaction(challenge, StellarSdk.Networks.TESTNET); | ||
@@ -42,2 +47,3 @@ expect(transaction.sequence).to.eql("0"); | ||
expect(operation.value.length).to.eql(64); | ||
expect(Buffer.from(operation.value.toString(), 'base64').length).to.eql(48); | ||
}); | ||
@@ -52,6 +58,7 @@ | ||
"SDF", | ||
600 | ||
600, | ||
StellarSdk.Networks.TESTNET | ||
); | ||
const transaction = new StellarSdk.Transaction(challenge); | ||
const transaction = new StellarSdk.Transaction(challenge, StellarSdk.Networks.TESTNET); | ||
@@ -76,3 +83,4 @@ let maxTime = parseInt(transaction.timeBounds.maxTime); | ||
"SDF", | ||
300 | ||
300, | ||
StellarSdk.Networks.TESTNET | ||
); | ||
@@ -82,3 +90,3 @@ | ||
const transaction = new StellarSdk.Transaction(challenge); | ||
const transaction = new StellarSdk.Transaction(challenge, StellarSdk.Networks.TESTNET); | ||
transaction.sign(clientKeypair); | ||
@@ -91,3 +99,3 @@ | ||
expect(StellarSdk.Utils.verifyChallengeTx(signedChallenge, keypair.publicKey())).to.eql(true); | ||
expect(StellarSdk.Utils.verifyChallengeTx(signedChallenge, keypair.publicKey(), StellarSdk.Networks.TESTNET)).to.eql(true); | ||
}); | ||
@@ -99,3 +107,3 @@ | ||
const account = new StellarSdk.Account(keypair.publicKey(), "100"); | ||
const transaction = new StellarSdk.TransactionBuilder(account, { fee: 100 }) | ||
const transaction = new StellarSdk.TransactionBuilder(account, txBuilderOpts) | ||
.setTimeout(30) | ||
@@ -110,3 +118,3 @@ .build(); | ||
expect( | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, keypair.publicKey()) | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, keypair.publicKey(), StellarSdk.Networks.TESTNET) | ||
).to.throw( | ||
@@ -124,3 +132,5 @@ StellarSdk.InvalidSep10ChallengeError, | ||
"GBDIT5GUJ7R5BXO3GJHFXJ6AZ5UQK6MNOIDMPQUSMXLIHTUNR2Q5CFNF", | ||
"SDF" | ||
"SDF", | ||
300, | ||
StellarSdk.Networks.TESTNET | ||
); | ||
@@ -131,3 +141,3 @@ | ||
expect( | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, serverAccountId) | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, serverAccountId, StellarSdk.Networks.TESTNET) | ||
).to.throw( | ||
@@ -142,3 +152,3 @@ StellarSdk.InvalidSep10ChallengeError, | ||
const account = new StellarSdk.Account(keypair.publicKey(), "-1"); | ||
const transaction = new StellarSdk.TransactionBuilder(account, { fee: 100 }) | ||
const transaction = new StellarSdk.TransactionBuilder(account, txBuilderOpts) | ||
.setTimeout(30) | ||
@@ -154,3 +164,3 @@ .build(); | ||
expect( | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, keypair.publicKey()) | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, keypair.publicKey(), StellarSdk.Networks.TESTNET) | ||
).to.throw( | ||
@@ -165,7 +175,7 @@ StellarSdk.InvalidSep10ChallengeError, | ||
const account = new StellarSdk.Account(keypair.publicKey(), "-1"); | ||
const transaction = new StellarSdk.TransactionBuilder(account, { fee: 100 }) | ||
const transaction = new StellarSdk.TransactionBuilder(account, txBuilderOpts) | ||
.addOperation( | ||
StellarSdk.Operation.manageData({ | ||
name: 'SDF auth', | ||
value: randomBytes(64) | ||
value: randomBytes(48).toString('base64') | ||
}) | ||
@@ -183,3 +193,3 @@ ) | ||
expect( | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, keypair.publicKey()) | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, keypair.publicKey(), StellarSdk.Networks.TESTNET) | ||
).to.throw( | ||
@@ -194,3 +204,3 @@ StellarSdk.InvalidSep10ChallengeError, | ||
const account = new StellarSdk.Account(keypair.publicKey(), "-1"); | ||
const transaction = new StellarSdk.TransactionBuilder(account, { fee: 100 }) | ||
const transaction = new StellarSdk.TransactionBuilder(account, txBuilderOpts) | ||
.addOperation( | ||
@@ -212,3 +222,3 @@ StellarSdk.Operation.accountMerge({ | ||
expect( | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, keypair.publicKey()) | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, keypair.publicKey(), StellarSdk.Networks.TESTNET) | ||
).to.throw( | ||
@@ -220,2 +230,30 @@ StellarSdk.InvalidSep10ChallengeError, | ||
it('throws an error if operation value is not a 64 bytes base64 string', function() { | ||
let keypair = StellarSdk.Keypair.random(); | ||
const account = new StellarSdk.Account(keypair.publicKey(), "-1"); | ||
const transaction = new StellarSdk.TransactionBuilder(account, txBuilderOpts) | ||
.addOperation( | ||
StellarSdk.Operation.manageData({ | ||
name: 'SDF auth', | ||
value: randomBytes(64), | ||
source: 'GBDIT5GUJ7R5BXO3GJHFXJ6AZ5UQK6MNOIDMPQUSMXLIHTUNR2Q5CFNF' | ||
}) | ||
) | ||
.setTimeout(30) | ||
.build(); | ||
transaction.sign(keypair); | ||
const challenge = transaction | ||
.toEnvelope() | ||
.toXDR("base64") | ||
.toString(); | ||
expect( | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, keypair.publicKey(), StellarSdk.Networks.TESTNET) | ||
).to.throw( | ||
StellarSdk.InvalidSep10ChallengeError, | ||
/The transaction\'s operation value should be a 64 bytes base64 random string/ | ||
); | ||
}); | ||
it('throws an error if transaction is not signed by the server', function() { | ||
@@ -227,6 +265,8 @@ let keypair = StellarSdk.Keypair.random(); | ||
"GBDIT5GUJ7R5BXO3GJHFXJ6AZ5UQK6MNOIDMPQUSMXLIHTUNR2Q5CFNF", | ||
"SDF" | ||
"SDF", | ||
300, | ||
StellarSdk.Networks.TESTNET | ||
); | ||
const transaction = new StellarSdk.Transaction(challenge); | ||
const transaction = new StellarSdk.Transaction(challenge, StellarSdk.Networks.TESTNET); | ||
@@ -245,3 +285,3 @@ transaction.signatures = []; | ||
expect( | ||
() => StellarSdk.Utils.verifyChallengeTx(unsignedChallenge, keypair.publicKey()) | ||
() => StellarSdk.Utils.verifyChallengeTx(unsignedChallenge, keypair.publicKey(), StellarSdk.Networks.TESTNET) | ||
).to.throw( | ||
@@ -259,7 +299,9 @@ StellarSdk.InvalidSep10ChallengeError, | ||
"GBDIT5GUJ7R5BXO3GJHFXJ6AZ5UQK6MNOIDMPQUSMXLIHTUNR2Q5CFNF", | ||
"SDF" | ||
"SDF", | ||
300, | ||
StellarSdk.Networks.TESTNET | ||
); | ||
expect( | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, keypair.publicKey()) | ||
() => StellarSdk.Utils.verifyChallengeTx(challenge, keypair.publicKey(), StellarSdk.Networks.TESTNET) | ||
).to.throw( | ||
@@ -279,3 +321,4 @@ StellarSdk.InvalidSep10ChallengeError, | ||
"SDF", | ||
300 | ||
300, | ||
StellarSdk.Networks.TESTNET | ||
); | ||
@@ -285,3 +328,3 @@ | ||
const transaction = new StellarSdk.Transaction(challenge); | ||
const transaction = new StellarSdk.Transaction(challenge, StellarSdk.Networks.TESTNET); | ||
transaction.sign(clientKeypair); | ||
@@ -295,3 +338,3 @@ | ||
expect( | ||
() => StellarSdk.Utils.verifyChallengeTx(signedChallenge, keypair.publicKey()) | ||
() => StellarSdk.Utils.verifyChallengeTx(signedChallenge, keypair.publicKey(), StellarSdk.Networks.TESTNET) | ||
).to.throw( | ||
@@ -308,3 +351,3 @@ StellarSdk.InvalidSep10ChallengeError, | ||
this.account = new StellarSdk.Account(this.keypair.publicKey(), "-1"); | ||
this.transaction = new StellarSdk.TransactionBuilder(this.account, { fee: 100 }) | ||
this.transaction = new StellarSdk.TransactionBuilder(this.account, txBuilderOpts) | ||
.setTimeout(30) | ||
@@ -311,0 +354,0 @@ .build(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2955615
57855
Updatedstellar-base@^1.1.1