-
Notifications
You must be signed in to change notification settings - Fork 305
CT-256 - algorand offline signing #414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -3,8 +3,7 @@ | |
| */ | ||
| import { BaseCoin } from '../baseCoin'; | ||
| import * as _ from 'lodash'; | ||
|
|
||
| const { | ||
| import { | ||
| NaclWrapper, | ||
| Multisig, | ||
| Address, | ||
|
|
@@ -13,7 +12,69 @@ const { | |
| generateAccount, | ||
| isValidAddress, | ||
| isValidSeed, | ||
| } = require('algosdk'); | ||
| Encoding, | ||
| } from 'algosdk'; | ||
|
|
||
| export interface TransactionExplanation { | ||
| displayOrder: string[]; | ||
| id: string; | ||
| outputs: Output[], | ||
| changeOutputs: Output[], | ||
| outputAmount: string; | ||
| changeAmount: number; | ||
| fee: TransactionFee; | ||
| memo: string; | ||
| } | ||
|
|
||
| export interface SignTransactionOptions { | ||
| txPrebuild: TransactionPrebuild; | ||
| prv: string; | ||
| wallet: { | ||
| addressVersion: string; | ||
| } | ||
| keychain: KeyPair; | ||
| backupKeychain: KeyPair; | ||
| bitgoKeychain: KeyPair; | ||
| } | ||
|
|
||
| export interface TransactionPrebuild { | ||
| txBase64: string; | ||
| txInfo: { | ||
| from: string; | ||
| to: string; | ||
| amount: string; | ||
| fee: number; | ||
| firstRound: number; | ||
| lastRound: number; | ||
| genesisID: string; | ||
| genesisHash: string; | ||
| note?: string; | ||
| } | ||
| } | ||
|
|
||
| export interface HalfSignedTransaction { | ||
| halfSigned: { | ||
| txBase64: string, | ||
| } | ||
| } | ||
|
|
||
| export interface Output { | ||
| address: string; | ||
| amount: string; | ||
| } | ||
|
|
||
| export interface TransactionFee { | ||
| fee: number; | ||
| feeRate?: number; | ||
| size?: number | ||
| } | ||
|
|
||
| export interface ExplainTransactionOptions { | ||
| txBase64: string; | ||
| wallet: { | ||
| addressVersion: string; | ||
| } | ||
| } | ||
|
|
||
| interface KeyPair { | ||
| pub: string; | ||
|
|
@@ -89,7 +150,7 @@ export class Algo extends BaseCoin { | |
| * @param {String} prv the prv to be checked | ||
| * @returns {Boolean} is it valid? | ||
| */ | ||
| isValidPrv(prv): boolean { | ||
| isValidPrv(prv: string): boolean { | ||
| return isValidSeed(prv); | ||
| } | ||
|
|
||
|
|
@@ -99,7 +160,7 @@ export class Algo extends BaseCoin { | |
| * @param {String} address the pub to be checked | ||
| * @returns {Boolean} is it valid? | ||
| */ | ||
| isValidAddress(address): boolean { | ||
| isValidAddress(address: string): boolean { | ||
| return isValidAddress(address); | ||
| } | ||
|
|
||
|
|
@@ -109,7 +170,7 @@ export class Algo extends BaseCoin { | |
| * @param key | ||
| * @param message | ||
| */ | ||
| signMessage(key, message): Buffer { | ||
| signMessage(key: KeyPair, message: string | Buffer): Buffer { | ||
| // key.prv actually holds the encoded seed, but we use the prv name to avoid breaking the keypair schema. | ||
| // See jsdoc comment in isValidPrv | ||
| let seed = key.prv; | ||
|
|
@@ -133,30 +194,79 @@ export class Algo extends BaseCoin { | |
| } | ||
|
|
||
| /** | ||
| * Specifies what key we will need for signing - Algorand needs the backup, bitgo pubs. | ||
| * Specifies what key we will need for signing` - Algorand needs the backup, bitgo pubs. | ||
| */ | ||
| keyIdsForSigning() { | ||
| keyIdsForSigning(): number[] { | ||
| return [ 0, 1, 2 ]; | ||
| } | ||
|
|
||
| /** | ||
| * Explain/parse transaction | ||
| * @param params | ||
| * - txBase64: transaction encoded as base64 string | ||
| */ | ||
| explainTransaction(params: ExplainTransactionOptions): TransactionExplanation { | ||
| const { txBase64 } = params; | ||
|
|
||
| let tx; | ||
| try { | ||
| const txToHex = Buffer.from(txBase64, 'base64'); | ||
| const decodedTx = Encoding.decode(txToHex); | ||
|
|
||
| // if we are a signed msig tx, the structure actually has the { msig, txn } as the root object | ||
| // if we are not signed, the decoded tx is the txn - refer to partialSignTxn and MultiSig constructor | ||
| // in algosdk for more information | ||
| const txnForDecoding = decodedTx.txn || decodedTx; | ||
|
|
||
| tx = Multisig.MultiSigTransaction.from_obj_for_encoding(txnForDecoding); | ||
| } catch (ex) { | ||
| throw new Error('txBase64 needs to be a valid tx encoded as base64 string'); | ||
| } | ||
|
|
||
| const id = tx.txID(); | ||
| const fee = { fee: tx.fee}; | ||
|
|
||
| const outputs = [{ | ||
| amount: tx.amount, | ||
| address: Address.encode(new Uint8Array(tx.to.publicKey)), | ||
| }]; | ||
|
|
||
| const outputAmount = tx.amount; | ||
|
|
||
| // TODO(CT-480): add recieving address display here | ||
| const memo = tx.note; | ||
|
|
||
| return { | ||
| displayOrder: ['id', 'outputAmount', 'changeAmount', 'outputs', 'changeOutputs', 'fee', 'memo'], | ||
| id, | ||
| outputs, | ||
| outputAmount, | ||
| changeAmount: 0, | ||
| fee, | ||
| changeOutputs: [], | ||
| memo | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Assemble keychain and half-sign prebuilt transaction | ||
| * | ||
| * @param params | ||
| * @param params.txPrebuild {Object} prebuild object returned by platform | ||
| * @param params.prv {String} user prv | ||
| * @param params.wallet.addressVersion {String} this is the version of the Algorand multisig address generation format | ||
| */ | ||
| signTransaction(params) { | ||
| signTransaction(params: SignTransactionOptions): HalfSignedTransaction { | ||
| const prv = params.prv; | ||
| const txData = params.txPrebuild.txData; | ||
| const txBase64 = params.txPrebuild.txBase64; | ||
| const addressVersion = params.wallet.addressVersion; | ||
|
|
||
| if (_.isUndefined(txData)) { | ||
| if (_.isUndefined(txBase64)) { | ||
| throw new Error('missing txPrebuild parameter'); | ||
| } | ||
|
|
||
| if (!_.isObject(txData)) { | ||
| throw new Error(`txPrebuild must be an object, got type ${typeof txData}`); | ||
| if (!_.isString(txBase64)) { | ||
| throw new Error(`txPrebuild must be an object, got type ${typeof txBase64}`); | ||
| } | ||
|
|
||
| if (_.isUndefined(prv)) { | ||
|
|
@@ -175,24 +285,6 @@ export class Algo extends BaseCoin { | |
| throw new Error('missing addressVersion parameter to sign transaction'); | ||
| } | ||
|
|
||
| const refinedTxData = txData; | ||
| refinedTxData.amount = parseInt(txData.amount, 10); | ||
|
|
||
| // if note is truly null, we need to pass an array | ||
| if (!_.has(txData, 'note')) { | ||
| refinedTxData.note = new Uint8Array(0); | ||
| } else { | ||
| // if note was passed as null, assume its an empty string | ||
| if (txData.note === null) { | ||
| txData.note = ''; | ||
| } | ||
|
|
||
| // note has a maximum length | ||
| if (txData.note.length > MAX_ALGORAND_NOTE_LENGTH) { | ||
| throw new Error('note size exceeded specification'); | ||
| } | ||
| } | ||
|
|
||
| // we need to re-encode our public keys using algosdk's format | ||
| const encodedPublicKeys = [ | ||
| Address.decode(params.keychain.pub).publicKey, | ||
|
|
@@ -205,15 +297,28 @@ export class Algo extends BaseCoin { | |
| const pair = generateAccountFromSeed(seed); | ||
| const sk = pair.sk; | ||
|
|
||
| // decode our tx | ||
| let transaction; | ||
| try { | ||
| const txToHex = Buffer.from(txBase64, 'base64'); | ||
| const decodedTx = Encoding.decode(txToHex); | ||
| transaction = Multisig.MultiSigTransaction.from_obj_for_encoding(decodedTx); | ||
| } catch (e) { | ||
| throw new Error('transaction needs to be a valid tx encoded as base64 string'); | ||
| } | ||
|
|
||
| // sign | ||
| const transaction = new Multisig.MultiSigTransaction(refinedTxData); | ||
| const halfSigned = transaction.partialSignTxn( | ||
| { version: addressVersion, threshold: 2, pks: encodedPublicKeys }, | ||
| sk | ||
| ); | ||
|
|
||
| const txHex = Buffer.from(halfSigned).toString('base64'); | ||
| const signedBase64 = Buffer.from(halfSigned).toString('base64'); | ||
|
|
||
| return { txHex }; | ||
| return { | ||
| halfSigned: { | ||
| txBase64: signedBase64, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we returning the same data twice with two different names? Can we clean this up?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've removed this, yeah we didn't need to do this |
||
| } | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.