@@ -20,10 +20,9 @@ const Table = require('./table');
2020const Contract = require ( './smartContract' ) ;
2121const util = require ( '../lib/util' ) ;
2222const opType = require ( '../lib/config' ) . opType ;
23- const getFee = util . getFee ;
24- const getSequence = util . getSequence ;
2523const convertStringToHex = util . convertStringToHex ;
26- const getTableSequence = util . getTableSequence ;
24+ const getCryptAlgTypeFromAccout = util . getCryptAlgTypeFromAccout ;
25+
2726const getUserToken = util . getUserToken ;
2827const getTxJson = util . getTxJson ;
2928const generateToken = util . generateToken ;
@@ -47,10 +46,10 @@ class ChainsqlAPI extends Submit {
4746 this . cache = [ ] ;
4847 this . strictMode = false ;
4948 this . needVerify = 1 ;
50- if ( algType === "gmAlg" || algType === "normal" ) {
49+ if ( algType === "gmAlg" || algType === "normal" || algType === "softGMAlg" ) {
5150 keypairs . setCryptAlgType ( algType ) ;
5251 } else {
53- throw new Error ( "Wrong algType for ChainsqlAPI object, must be 'gmAlg' or 'normal'" ) ;
52+ throw new Error ( "Wrong algType for ChainsqlAPI object, must be 'gmAlg','softGMAlg' or 'normal'" ) ;
5453 }
5554 }
5655
@@ -112,10 +111,14 @@ ChainsqlAPI.prototype.disconnect = function (cb) {
112111 }
113112}
114113ChainsqlAPI . prototype . as = function ( account ) {
114+
115115 if ( ! account . secret || ! account . address ) {
116116 throw chainsqlError ( "c.as parameter invalid,must contain 'secret' and 'address'" ) ;
117117 }
118- this . connect . as ( account ) ;
118+
119+ // 根据账户信息判断底层的算法类型
120+ keypairs . setCryptAlgType ( getCryptAlgTypeFromAccout ( account ) ) ;
121+ this . connect . as ( account ) ;
119122}
120123ChainsqlAPI . prototype . use = function ( address ) {
121124 this . connect . use ( address ) ;
@@ -151,27 +154,32 @@ ChainsqlAPI.prototype.contract = function(jsonInterface, address, options) {
151154}
152155
153156ChainsqlAPI . prototype . generateAddress = function ( ) {
154- var account ;
157+
158+ var account = { secret :"" , address :"" } ;
155159 var keypair ;
156160 let ripple = new RippleAPI ( ) ;
157161 if ( arguments . length == 0 ) {
158162 account = ripple . generateAddress ( ) ;
159163 keypair = keypairs . deriveKeypair ( account . secret ) ;
160164 } else {
161165 if ( typeof ( arguments [ 0 ] ) === "object" ) {
162- let secretNew = keypairs . generateSeed ( arguments [ 0 ] ) ;
163- keypair = keypairs . deriveKeypair ( secretNew ) ;
164- account = {
165- secret : secretNew ,
166- address : keypairs . deriveAddress ( keypair . publicKey )
167- }
168- } else {
169- keypair = keypairs . deriveKeypair ( arguments [ 0 ] ) ;
170- account = {
171- secret : arguments [ 0 ] ,
172- address : keypairs . deriveAddress ( keypair . publicKey )
166+ let seed = keypairs . generateSeed ( arguments [ 0 ] ) ;
167+ keypair = keypairs . deriveKeypair ( seed ) ;
168+
169+ if ( typeof ( seed ) !== "object" ) {
170+ // ed25519
171+ account . secret = seed ;
172+ } else {
173+ // softGMAlg
174+ account . secret = util . encodeChainsqlAccountSecret ( keypair . privateKey )
173175 }
176+
177+ } else {
178+ keypair = keypairs . deriveKeypair ( arguments [ 0 ] )
179+ account . secret = arguments [ 0 ]
174180 }
181+
182+ account . address = keypairs . deriveAddress ( keypair . publicKey ) ;
175183 }
176184 var opt = {
177185 version : 35
@@ -305,19 +313,22 @@ ChainsqlAPI.prototype.createTable = function (name, raw, inputOpt) {
305313 } ;
306314
307315 if ( confidential ) {
308- var token = generateToken ( that . connect . secret ) ;
316+ var token = generateToken ( that . connect . secret ) ;
309317 var symKey = decodeToken ( that , token ) ;
318+ var regSoftGMSeed = / ^ [ a - z A - Z 1 - 9 ] { 51 , 51 } /
319+
310320 if ( that . connect . secret === "gmAlg" ) {
311321 payment . raw = crypto . symEncrypt ( symKey , payment . raw , "gmAlg" ) . toUpperCase ( ) ;
312- } else {
322+ } else if ( regSoftGMSeed . test ( that . connect . secret ) ) {
323+ payment . raw = crypto . symEncrypt ( symKey , payment . raw , "softGMAlg" ) . toUpperCase ( ) ;
324+ }
325+ else {
313326 payment . raw = crypto . symEncrypt ( symKey , payment . raw ) . toUpperCase ( ) ;
314- }
315-
327+ }
316328 payment . token = token . toUpperCase ( ) ;
317329 } else {
318330 payment . raw = convertStringToHex ( payment . raw ) ;
319331 }
320-
321332 if ( payment . operationRule ) {
322333 payment . operationRule = convertStringToHex ( payment . operationRule ) ;
323334 }
@@ -774,6 +785,7 @@ function handleGrantPayment(ChainSQL) {
774785 reject ( chainsqlError ( 'your publicKey is not validate' ) ) ;
775786 }
776787 ChainSQL . payment . token = token ;
788+ console . log ( "token : " , token )
777789 }
778790 delete ChainSQL . payment . name ;
779791 delete ChainSQL . payment . publicKey ;
@@ -802,6 +814,47 @@ ChainsqlAPI.prototype.eciesDecrypt = function (cipher, secret) {
802814 return crypto . eciesDecrypt ( cipher , keypair . privateKey ) ;
803815}
804816
817+
818+
819+ /**
820+ * 对称加密
821+ * @param {* } plainText
822+ * @param {* } publicKey
823+ */
824+ ChainsqlAPI . prototype . symEncrypt = function ( symKey , plaintext , algType = 'aes' ) {
825+ return crypto . symEncrypt ( symKey , plaintext , algType ) ;
826+ }
827+
828+ /**
829+ * 对称解密
830+ * @param {* } cipher
831+ * @param {* } privateKey
832+ */
833+ ChainsqlAPI . prototype . symDecrypt = function ( symKey , encryptedHex , algType = 'aes' ) {
834+
835+ return crypto . symDecrypt ( symKey , encryptedHex , algType ) ;
836+ }
837+
838+ /**
839+ * 非对称加密
840+ * @param {* } plainText
841+ * @param {* } publicKey
842+ */
843+ ChainsqlAPI . prototype . asymEncrypt = function ( plainText , publicKey ) {
844+ return keypairs . asymEncrypt ( plainText , publicKey ) ;
845+ }
846+
847+ /**
848+ * 非对称解密
849+ * @param {* } cipher
850+ * @param {* } privateKey
851+ */
852+ ChainsqlAPI . prototype . asymDecrypt = function ( cipher , privateKey ) {
853+
854+ return keypairs . asymDecrypt ( cipher , privateKey ) ;
855+ }
856+
857+
805858ChainsqlAPI . prototype . getAccountTables = function ( address , bGetDetailInfo = false ) {
806859 var connection = this . api ? this . api . connection : this . connect . api . connection ;
807860 return new Promise ( function ( resolve , reject ) {
@@ -927,7 +980,7 @@ ChainsqlAPI.prototype.getLedgerTxs = function(ledgerIndex,includeSuccess,include
927980
928981ChainsqlAPI . prototype . signFromString = function ( messageHex , secret ) {
929982
930- var keypair = keypairs . deriveKeypair ( secret ) ;
983+ var keypair = keypairs . deriveKeypair ( secret ) ;
931984 var signatue = keypairs . sign ( messageHex , keypair . privateKey ) ;
932985 return signatue ;
933986} ;
0 commit comments