Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Support for xrpl-secret-numbers encoded seed #187

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"grpc-web": "1.0.7",
"ripple-address-codec": "4.1.1",
"ripple-binary-codec": "0.2.6",
"ripple-keypairs": "^1.0.0"
"ripple-keypairs": "^1.0.0",
"xrpl-secret-numbers": "^0.2.1"
},
"scripts": {
"build": "npm run clean && ./scripts/regenerate_protos.sh && npm run lint && tsc -d && copyfiles -u 3 './src/XRP/generated/**/*' ./build/src/XRP/generated",
Expand Down
2 changes: 1 addition & 1 deletion rippled
24 changes: 22 additions & 2 deletions src/XRP/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as bip32 from 'bip32'
import * as bip39 from 'bip39'
import * as rippleKeyPair from 'ripple-keypairs'

import Utils from '../Common/utils'
import { Account as SecretNumbersAccount } from 'xrpl-secret-numbers'
import Utils from './utils'

/**
* The default derivation path to use with BIP44.
Expand Down Expand Up @@ -132,6 +132,26 @@ class Wallet {
}
}

/**
* Generate a new wallet from the given secret numbers.
*
* @param secretNumbers The given seed for the wallet.
* @param test Whether the address is for use on a test network, defaults to `false`.
* @returns A new wallet from the given seed, or undefined if the seed was invalid.
*/
public static generateWalletFromSecretNumbers(
secretNumbers: string | string[],
test = false,
): Wallet | undefined {
try {
const account = new SecretNumbersAccount(secretNumbers)
const keyPair = account.getKeypair()
return new Wallet(keyPair.publicKey, keyPair.privateKey, test)
} catch (exception) {
return undefined
}
}

/**
* Create a new Wallet object.
*
Expand Down
50 changes: 50 additions & 0 deletions test/XRP/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,56 @@ describe('wallet', function (): void {
assert.isUndefined(wallet)
})

it('walletFromSecretNumbers - MainNet', function(): void {
// GIVEN a seed used to generate a wallet on MainNet
const seed = [
'554872',
'394230',
'209376',
'323698',
'140250',
'387423',
'652803',
'258676',
]
const isTestNet = false

// WHEN a wallet is generated from the seed.
const wallet = Wallet.generateWalletFromSecretNumbers(seed, isTestNet)

// THEN the wallet has the expected address.
assert.equal(
wallet!.getAddress(),
'XVPCcgRJZmYGpSczab57QWbcoTnP2FQsKrCTFWughwrqBH2',
)
})

it('walletFromSecretNumbers - TestNet', function(): void {
// GIVEN a seed used to generate a wallet on TestNet
const seed = '554872 394230 209376 323698 140250 387423 652803 258676'
const test = true

// WHEN a wallet is generated from the seed.
const wallet = Wallet.generateWalletFromSecretNumbers(seed, test)

// THEN the wallet has the expected address.
assert.equal(
wallet!.getAddress(),
'TVKUp82HRjaHTxPb4QTEJWMXGtcUaQhnGBcv3U9UekYpy9T',
)
})

it('walletFromSecretNumbers - invalid seed', function(): void {
// GIVEN an invalid seed.
const seed = '554872 394230 209372 323695 140250 387423 652803 258676'

// WHEN a wallet is generated from the seed.
const wallet = Wallet.generateWalletFromSecretNumbers(seed)

// THEN the wallet is undefined.
assert.isUndefined(wallet)
})

it('sign', function (): void {
// GIVEN a wallet.
const testData = derivationPathTestCases.index0
Expand Down