Skip to content
This repository has been archived by the owner on Dec 18, 2021. It is now read-only.

Commit

Permalink
fix: Shards are always base64 encoded
Browse files Browse the repository at this point in the history
Provide encoding only for the secret.
  • Loading branch information
franky47 committed Nov 29, 2019
1 parent 15eb4a5 commit d4605f6
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/horcrux-crypto/src/tss.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as tss from '@stablelib/tss'
import { Encoding, encoders, decoders } from './codec'
import { Encoding, encoders, decoders, b64 } from './codec'

/**
* Split a secret into a given amount of shards.
Expand All @@ -8,34 +8,31 @@ import { Encoding, encoders, decoders } from './codec'
* @param secret The secret to split
* @param numShards How many pieces to split into
* @param threshold How many pieces are needed (min) to re-assemble the secret
* @param encoding Encoding for the secret and the shards
* @param encoding Input encoding for the secret
*/
export const splitSecret = (
secret: string,
numShards: number,
threshold: number,
encoding: Encoding = 'base64'
encoding: Encoding = 'utf8'
) => {
const encode = encoders[encoding]
const decode = decoders[encoding]
const shards = tss.split(decode(secret), threshold, numShards)
return shards.map(shard => encode(shard))
return shards.map(shard => b64.encode(shard))
}

/**
* Try to re-assemble the original secret from shards.
* Will throw if anything goes wrong.
*
* @param shards Any number of shards
* @param encoding Encoding for the shards and the output
* @returns The secret in the same encoding as the shards
* @param encoding Encoding for the output
*/
export const assembleSecret = (
shards: string[],
encoding: Encoding = 'base64'
encoding: Encoding = 'utf8'
) => {
const encode = encoders[encoding]
const decode = decoders[encoding]
const secret = tss.combine(shards.map(shard => decode(shard)))
const secret = tss.combine(shards.map(shard => b64.decode(shard)))
return encode(secret)
}

0 comments on commit d4605f6

Please sign in to comment.