Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SEP0010 transaction challenge builder. (#375)
* Implement SEP0010 transaction challenge builder. * Improve docs. * Use BASE_FEE instead of fixed number. * Add example in jsdoc. * Add randombytes. * Use randomBytes instead of crypto.randomBytes.
- Loading branch information
Showing
5 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import randomBytes from "randombytes"; | ||
import { | ||
Account, | ||
BASE_FEE, | ||
Keypair, | ||
Operation, | ||
TransactionBuilder, | ||
} from "stellar-base"; | ||
|
||
/** | ||
* @namespace Utils | ||
*/ | ||
export namespace Utils { | ||
/** | ||
* Returns a valid [SEP0010](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md) | ||
* challenge transaction which you can use for Stellar Web Authentication. | ||
* | ||
* @see [SEP0010: Stellar Web Authentication](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md) | ||
* @function | ||
* @memberof Utils | ||
* @param {Keypair} serverKeypair Keypair for server's signing account. | ||
* @param {string} clientAccountID The stellar account that the wallet wishes to authenticate with the server. | ||
* @param {string} anchorName Anchor's name to be used in the manage_data key. | ||
* @param {number} [timeout=300] Challenge duration (default to 5 minutes). | ||
* @example | ||
* import { Utils, Keypair, Network } from 'stellar-sdk' | ||
* | ||
* Network.useTestNetwork(); | ||
* | ||
* let serverKeyPair = Keypair.fromSecret("server-secret") | ||
* let challenge = Utils.buildChallengeTx(serverKeyPair, "client-stellar-account-id", "SDF", 300) | ||
* @returns {string} A base64 encoded string of the raw TransactionEnvelope xdr struct for the transaction. | ||
*/ | ||
export function buildChallengeTx( | ||
serverKeypair: Keypair, | ||
clientAccountID: string, | ||
anchorName: string, | ||
timeout: number = 300, | ||
): string { | ||
const account = new Account(serverKeypair.publicKey(), "-1"); | ||
const now = Math.floor(Date.now() / 1000); | ||
|
||
const transaction = new TransactionBuilder(account, { | ||
fee: BASE_FEE, | ||
timebounds: { | ||
minTime: now, | ||
maxTime: now + timeout, | ||
}, | ||
}) | ||
.addOperation( | ||
Operation.manageData({ | ||
name: `${anchorName} auth`, | ||
value: randomBytes(64), | ||
source: clientAccountID, | ||
}), | ||
) | ||
.build(); | ||
|
||
transaction.sign(serverKeypair); | ||
|
||
return transaction | ||
.toEnvelope() | ||
.toXDR("base64") | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
describe('Utils', function() { | ||
describe('Utils.buildChallengeTx', function() { | ||
it('returns challenge which follows SEP0010 spec', function() { | ||
let keypair = StellarSdk.Keypair.random(); | ||
|
||
const challenge = StellarSdk.Utils.buildChallengeTx( | ||
keypair, | ||
"GBDIT5GUJ7R5BXO3GJHFXJ6AZ5UQK6MNOIDMPQUSMXLIHTUNR2Q5CFNF", | ||
"SDF" | ||
); | ||
|
||
const transaction = new StellarSdk.Transaction(challenge); | ||
|
||
expect(transaction.sequence).to.eql("0"); | ||
expect(transaction.source).to.eql(keypair.publicKey()); | ||
expect(transaction.operations.length).to.eql(1); | ||
|
||
const { maxTime, minTime } = transaction.timeBounds; | ||
|
||
expect(parseInt(maxTime) - parseInt(minTime)).to.eql(300); | ||
|
||
const [ operation ] = transaction.operations; | ||
|
||
expect(operation.name).to.eql("SDF auth"); | ||
expect(operation.source).to.eql("GBDIT5GUJ7R5BXO3GJHFXJ6AZ5UQK6MNOIDMPQUSMXLIHTUNR2Q5CFNF"); | ||
expect(operation.type).to.eql("manageData"); | ||
expect(operation.value.length).to.eql(64); | ||
}); | ||
|
||
it('uses the passed-in timeout', function() { | ||
let keypair = StellarSdk.Keypair.random(); | ||
|
||
const challenge = StellarSdk.Utils.buildChallengeTx( | ||
keypair, | ||
"GBDIT5GUJ7R5BXO3GJHFXJ6AZ5UQK6MNOIDMPQUSMXLIHTUNR2Q5CFNF", | ||
"SDF", | ||
600 | ||
); | ||
|
||
const transaction = new StellarSdk.Transaction(challenge); | ||
const { maxTime, minTime } = transaction.timeBounds; | ||
expect(parseInt(maxTime) - parseInt(minTime)).to.eql(600); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters