-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from nemfoundation/add-encoders
Added MnemonicQR, ExportMnemonicDataSchema and encoders
- Loading branch information
Showing
37 changed files
with
2,453 additions
and
434 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright 2019 NEM | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
*limitations under the License. | ||
*/ | ||
|
||
export abstract class Example { | ||
/** | ||
* Construct an example instance | ||
* | ||
*/ | ||
constructor() {} | ||
|
||
/// region Abstract Methods | ||
/** | ||
* The `execute()` method should run the underlying | ||
* example business flow. | ||
* | ||
* @return {number} | ||
*/ | ||
public abstract async execute(): Promise<number>; | ||
/// end-region Abstract Methods | ||
|
||
/** | ||
* | ||
*/ | ||
public resolve<T>(resp: T): Promise<T> { | ||
return new Promise((resolve, reject) => { | ||
return resolve(resp); | ||
}); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public reject<T>(resp: T): Promise<T> { | ||
return new Promise((resolve, reject) => { | ||
return reject(resp); | ||
}); | ||
} | ||
} |
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,60 @@ | ||
/** | ||
* Copyright 2019 NEM | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
*limitations under the License. | ||
*/ | ||
import { | ||
NetworkType, | ||
} from 'nem2-sdk'; | ||
|
||
// internal dependencies | ||
import { | ||
ContactQR, | ||
QRCodeType, | ||
} from '../index'; | ||
import {Example} from './Example'; | ||
|
||
export class ExampleAddContactQR extends Example { | ||
|
||
/** | ||
* The `execute()` method should run the underlying | ||
* example business flow. | ||
* | ||
* @return {number} | ||
*/ | ||
public async execute(): Promise<number> { | ||
|
||
// Arrange | ||
const contactInfo = { | ||
v: 3, | ||
type: QRCodeType.AddContact, | ||
network_id: NetworkType.MIJIN_TEST, | ||
chain_id: 'no-chain-id', | ||
data: { | ||
name: 'nemtech', | ||
publicKey: 'D90ABF5BADC4E709E79E8F168F1629CD90D7F5B41010B7C0616C2121D516C11C' | ||
} | ||
}; | ||
|
||
// create QR Code with JSON content | ||
const contactQR = ContactQR.fromJSON(JSON.stringify(contactInfo)); | ||
console.log("JSON: ", contactQR.toJSON()); | ||
console.log("BASE64: ", contactQR.toBase64()); | ||
console.log(""); | ||
console.log("ASCII: "); | ||
console.log(contactQR.toASCII()); | ||
console.log(""); | ||
return this.resolve(0); | ||
} | ||
} | ||
|
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,72 @@ | ||
/** | ||
* Copyright 2019 NEM | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
*limitations under the License. | ||
*/ | ||
import { | ||
NetworkType, | ||
Account, | ||
Password, | ||
} from 'nem2-sdk'; | ||
|
||
// internal dependencies | ||
import { | ||
AccountQR, | ||
QRCodeType, | ||
} from '../index'; | ||
import {Example} from './Example'; | ||
|
||
export class ExampleExportAccountQR extends Example { | ||
|
||
/** | ||
* The `execute()` method should run the underlying | ||
* example business flow. | ||
* | ||
* This example uses an encryption password value of `password` | ||
* and following account details | ||
* | ||
* Public Key: 9741183860ED711BD986A464004DB9A6D26B25F4CBB51F3B0FF1B220510B86B0 | ||
* Private Key: 749F1FF1972CD465CAB74566FF0AA021F846FBE3916ABB6A6C1373E962C76331 | ||
* | ||
* @return {number} | ||
*/ | ||
public async execute(): Promise<number> { | ||
|
||
// Arrange | ||
const accountInfo = { | ||
v: 3, | ||
type: QRCodeType.ExportAccount, | ||
network_id: NetworkType.MIJIN_TEST, | ||
chain_id: '9F1979BEBA29C47E59B40393ABB516801A353CFC0C18BC241FEDE41939C907E7', | ||
data: { | ||
ciphertext: '56d310848ee93d0794eb1f64a5195778ded2q7IxvtPbO+sA7jZZyhpu/khbaNdx1pzuoGoPJRw1A4aBsWPlex3y/gy5da8WjF0i4d+/D0B5ESy+zX5P+AoFAw3EFi3UVBdnav4rnqg=', | ||
salt: '42c8615bc6b2bc88cd239f08a5a17cc62bb0ebaece53f3e458a1cd67cd0888bc' | ||
} | ||
}; | ||
|
||
// create QR Code with JSON content and password | ||
const accountQR = AccountQR.fromJSON( | ||
JSON.stringify(accountInfo), | ||
new Password('password') | ||
); | ||
|
||
console.log("JSON: ", accountQR.toJSON()); | ||
console.log("BASE64: ", accountQR.toBase64()); | ||
console.log(""); | ||
console.log("ASCII: "); | ||
console.log(accountQR.toASCII()); | ||
console.log(""); | ||
return this.resolve(0); | ||
} | ||
} | ||
|
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,74 @@ | ||
/** | ||
* Copyright 2019 NEM | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
*limitations under the License. | ||
*/ | ||
import { | ||
NetworkType, | ||
Account, | ||
Password, | ||
} from 'nem2-sdk'; | ||
import {MnemonicPassPhrase} from 'nem2-hd-wallets'; | ||
|
||
// internal dependencies | ||
import { | ||
MnemonicQR, | ||
QRCodeType, | ||
} from '../index'; | ||
import {Example} from './Example'; | ||
|
||
export class ExampleExportMnemonicQR extends Example { | ||
|
||
/** | ||
* The `execute()` method should run the underlying | ||
* example business flow. | ||
* | ||
* This example uses an encryption password value of `password` | ||
* and following 24-words mnemonic pass phrase: | ||
* | ||
* stumble shoot spawn bitter forest waste attitude chest | ||
* square kite dawn photo twice message bargain trap | ||
* spin vote lamp wire also either else pupil | ||
* | ||
* @return {number} | ||
*/ | ||
public async execute(): Promise<number> { | ||
|
||
// MnemonicQR example data | ||
const mnemonicInfo = { | ||
v: 3, | ||
type: QRCodeType.ExportMnemonic, | ||
network_id: NetworkType.MIJIN_TEST, | ||
chain_id: "9F1979BEBA29C47E59B40393ABB516801A353CFC0C18BC241FEDE41939C907E7", | ||
data: { | ||
ciphertext: "964322228f401a2ec576ac256cbbdce29YfW+CykqESzGSzDYuKJxJUSpQ4woqMdD8Up7mjbow09I/UYV4e8HEgbhjlLjf30YLlQ+JKLBTf9kUGMnp3tZqYSq3lLZRDp8TVE6GzHiX4V59RTP7BOixwpDWDmfOP0B0i+Q1s0+OPfmyck4p7YZkVNi/HYvQF4kDV27sjRTZKs+uETKA0Ae0rl17d9EMV3eLUVcWEGE/ChgEfmnMlN1g==", | ||
salt: "b248953e9ebfa269cd7b940f9c03d2d4b192f90db61638375b5e78296bbe675a" | ||
} | ||
}; | ||
|
||
// create QR Code with JSON content and password | ||
const mnemonicQR = MnemonicQR.fromJSON( | ||
JSON.stringify(mnemonicInfo), | ||
new Password('password') | ||
); | ||
|
||
console.log("JSON: ", mnemonicQR.toJSON()); | ||
console.log("BASE64: ", mnemonicQR.toBase64()); | ||
console.log(""); | ||
console.log("ASCII: "); | ||
console.log(mnemonicQR.toASCII()); | ||
console.log(""); | ||
return this.resolve(0); | ||
} | ||
} | ||
|
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,80 @@ | ||
/** | ||
* Copyright 2019 NEM | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
*limitations under the License. | ||
*/ | ||
import { | ||
NetworkType, | ||
Account, | ||
TransferTransaction, | ||
Deadline, | ||
EmptyMessage, | ||
Mosaic, | ||
NamespaceId, | ||
UInt64, | ||
} from 'nem2-sdk'; | ||
import {MnemonicPassPhrase} from 'nem2-hd-wallets'; | ||
|
||
// internal dependencies | ||
import { | ||
TransactionQR, | ||
QRCodeType, | ||
} from '../index'; | ||
import {Example} from './Example'; | ||
|
||
export class ExampleRequestTransactionQR extends Example { | ||
|
||
/** | ||
* The `execute()` method should run the underlying | ||
* example business flow. | ||
* | ||
* This example uses an unsigned transfer transaction | ||
* with following details: | ||
* - Recipient: namespaceId "nemtech" | ||
* - Mosaics: 1 mosaic with namespaceId "cat.currency" and absolute amount 1 | ||
* - Message: Empty | ||
* | ||
* @return {number} | ||
*/ | ||
public async execute(): Promise<number> { | ||
|
||
const unsignedTransferInfo = { | ||
v: 3, | ||
type: 3, | ||
network_id: NetworkType.MIJIN_TEST, | ||
chain_id: '9F1979BEBA29C47E59B40393ABB516801A353CFC0C18BC241FEDE41939C907E7', | ||
data: { | ||
payload: 'A500000000000000000000000000000000000000000000000000000000000000' | ||
+ '0000000000000000000000000000000000000000000000000000000000000000' | ||
+ '0000000000000000000000000000000000000000000000000000000000000000' | ||
+ '000000000190544100000000000000008CDC693819000000912C7BC6007CB8AC' | ||
+ 'B8000000000000000000000000000000000100010044B262C46CEABB85010000' | ||
+ '0000000000' | ||
} | ||
}; | ||
|
||
// create QR Code with JSON content | ||
const transactionQR = TransactionQR.fromJSON( | ||
JSON.stringify(unsignedTransferInfo), | ||
); | ||
|
||
console.log("JSON: ", transactionQR.toJSON()); | ||
console.log("BASE64: ", transactionQR.toBase64()); | ||
console.log(""); | ||
console.log("ASCII: "); | ||
console.log(transactionQR.toASCII()); | ||
console.log(""); | ||
return this.resolve(0); | ||
} | ||
} | ||
|
Oops, something went wrong.