Skip to content

Commit 8935531

Browse files
committed
Add check for valid XLM master seed
Add a check to verify that the XLM master seed is a valid seed for key derivation. Stellar Ecosystem Proposal 5 specifies (through BIP39 and BIP32) that valid seeds are between 128 bit and 512 bit. This fixes a bug when running the tests under latest node version (12.16.1) where the function utils.js/deriveChildKey would accept an invalid seed and derive child keys from this seed. This fix has been tested to work under both node v6 (current production version) and latest version (v12).
1 parent f008f34 commit 8935531

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

app/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ exports.sendMailQ = function(toEmail, subject, template, templateParams, attachm
8787
return sendMail(mailOptions);
8888
};
8989

90+
/*
91+
* Check if input is a valid seed input formatted as a hex string.
92+
* Cf. the BIP32 specification, a valid seed is between 128 bits and 512 bits (both included),
93+
* i.e. between 16 and 64 bytes.
94+
* https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#master-key-generation
95+
*/
96+
function IsValidBip32Seed(input) {
97+
return input.match(/^(([0-9a-f]{2}){16,64})$/);
98+
}
99+
90100
/** deriveChildKey
91101
*
92102
* returns the derived key as a string
@@ -107,6 +117,12 @@ exports.deriveChildKey = function(master, derivationPath, type, neuter) {
107117

108118
return childKey.toBase58();
109119
} else if (type === 'xlm') {
120+
121+
// Verify that input is a valid seed, cf. SEP05 (Stellar Ecosystem Proposals 5) which follows BIP39
122+
// which is based on BIP32:
123+
// https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md
124+
if (!IsValidBip32Seed(master)) { throw new Error(`Invalid seed. Got: ${master}`); }
125+
110126
const masterNode = stellarHd.fromSeed(master);
111127
const childKey = stellar.Keypair.fromRawEd25519Seed(masterNode.derive(derivationPath));
112128

0 commit comments

Comments
 (0)