-
Notifications
You must be signed in to change notification settings - Fork 17
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 #1663 from p2p-org/feature/pwn-769
[ETH-769] Referral program
- Loading branch information
Showing
43 changed files
with
791 additions
and
12 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
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
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
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
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
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
6 changes: 6 additions & 0 deletions
6
p2p_wallet/Common/Services/ReferralProgram/Model/ReferralTimedSignature.swift
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,6 @@ | ||
import Foundation | ||
|
||
struct ReferralTimedSignature: Encodable { | ||
let timestamp: Int64 | ||
let signature: String | ||
} |
30 changes: 30 additions & 0 deletions
30
p2p_wallet/Common/Services/ReferralProgram/Model/RegisterUserRequest.swift
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,30 @@ | ||
import Foundation | ||
import SolanaSwift | ||
import TweetNacl | ||
|
||
struct RegisterUserRequest: Encodable { | ||
let user: String | ||
let timedSignature: ReferralTimedSignature | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case user, timedSignature = "timed_signature" | ||
} | ||
} | ||
|
||
struct RegisterUserSignature: BorshSerializable { | ||
let user: String | ||
let referrent: String? | ||
let timestamp: Int64 | ||
|
||
func serialize(to writer: inout Data) throws { | ||
try user.serialize(to: &writer) | ||
try Optional(referrent)?.serialize(to: &writer) | ||
try timestamp.serialize(to: &writer) | ||
} | ||
|
||
func sign(secretKey: Data) throws -> Data { | ||
var data = Data() | ||
try serialize(to: &data) | ||
return try NaclSign.signDetached(message: data, secretKey: secretKey) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
p2p_wallet/Common/Services/ReferralProgram/Model/SetReferentRequest.swift
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,31 @@ | ||
import Foundation | ||
import SolanaSwift | ||
import TweetNacl | ||
|
||
struct SetReferentRequest: Encodable { | ||
let user: String | ||
let referent: String | ||
let timedSignature: ReferralTimedSignature | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case user, referent, timedSignature = "timed_signature" | ||
} | ||
} | ||
|
||
struct SetReferentSignature: BorshSerializable { | ||
let user: String | ||
let referent: String | ||
let timestamp: Int64 | ||
|
||
func serialize(to writer: inout Data) throws { | ||
try user.serialize(to: &writer) | ||
try referent.serialize(to: &writer) | ||
try timestamp.serialize(to: &writer) | ||
} | ||
|
||
func sign(secretKey: Data) throws -> Data { | ||
var data = Data() | ||
try serialize(to: &data) | ||
return try NaclSign.signDetached(message: data, secretKey: secretKey) | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
p2p_wallet/Common/Services/ReferralProgram/ReferralProgramService.swift
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,114 @@ | ||
import Foundation | ||
import KeyAppBusiness | ||
import KeyAppNetworking | ||
import Resolver | ||
import SolanaSwift | ||
import TweetNacl | ||
|
||
protocol ReferralProgramService { | ||
var referrer: String { get } | ||
var shareLink: URL { get } | ||
|
||
func register() async | ||
func setReferent(from: String) async | ||
} | ||
|
||
enum ReferralProgramServiceError: Error { | ||
case failedSet | ||
} | ||
|
||
final class ReferralProgramServiceImpl { | ||
// MARK: - Dependencies | ||
|
||
@Injected private var nameStorage: NameStorageType | ||
@Injected private var userWallet: UserWalletManager | ||
|
||
private let jsonrpcClient = JSONRPCHTTPClient() | ||
|
||
// MARK: - Private properties | ||
|
||
private var baseURL: String { | ||
GlobalAppState.shared.referralProgramAPIEndoint | ||
} | ||
|
||
private var currentUserAddress: String { | ||
userWallet.wallet?.account.publicKey.base58EncodedString ?? "" | ||
} | ||
} | ||
|
||
// MARK: - ReferralProgramService | ||
|
||
extension ReferralProgramServiceImpl: ReferralProgramService { | ||
var referrer: String { | ||
nameStorage.getName() ?? currentUserAddress | ||
} | ||
|
||
var shareLink: URL { | ||
URL(string: "https://r.key.app/\(referrer)")! | ||
} | ||
|
||
func register() async { | ||
guard !Defaults.referrerRegistered else { return } | ||
do { | ||
guard let secret = userWallet.wallet?.account.secretKey else { throw ReferralProgramServiceError.failedSet } | ||
let timestamp = Int64(Date().timeIntervalSince1970) | ||
let signed = try RegisterUserSignature( | ||
user: currentUserAddress, referrent: nil, timestamp: timestamp | ||
) | ||
.sign(secretKey: secret) | ||
let _: String? = try await jsonrpcClient.request( | ||
baseURL: baseURL, | ||
body: .init( | ||
method: "register", | ||
params: RegisterUserRequest( | ||
user: currentUserAddress, | ||
timedSignature: ReferralTimedSignature( | ||
timestamp: timestamp, signature: signed.toHexString() | ||
) | ||
) | ||
) | ||
) | ||
Defaults.referrerRegistered = true | ||
} catch { | ||
debugPrint(error) | ||
DefaultLogManager.shared.log( | ||
event: "\(ReferralProgramService.self)_register", | ||
data: error.localizedDescription, | ||
logLevel: LogLevel.error | ||
) | ||
} | ||
} | ||
|
||
func setReferent(from: String) async { | ||
guard from != currentUserAddress else { return } | ||
do { | ||
guard let secret = userWallet.wallet?.account.secretKey else { throw ReferralProgramServiceError.failedSet } | ||
let timestamp = Int64(Date().timeIntervalSince1970) | ||
let signed = try SetReferentSignature( | ||
user: currentUserAddress, referent: from, timestamp: timestamp | ||
) | ||
.sign(secretKey: secret) | ||
let _: String? = try await jsonrpcClient.request( | ||
baseURL: baseURL, | ||
body: .init( | ||
method: "set_referent", | ||
params: SetReferentRequest( | ||
user: currentUserAddress, | ||
referent: from, | ||
timedSignature: ReferralTimedSignature( | ||
timestamp: timestamp, | ||
signature: signed.toHexString() | ||
) | ||
) | ||
) | ||
) | ||
} catch { | ||
debugPrint(error) | ||
DefaultLogManager.shared.log( | ||
event: "\(ReferralProgramService.self)_setReferent", | ||
data: error.localizedDescription, | ||
logLevel: LogLevel.error | ||
) | ||
} | ||
} | ||
} |
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
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
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
22 changes: 22 additions & 0 deletions
22
p2p_wallet/Resources/Assets.xcassets/referral-icon.imageset/Contents.json
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,22 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"filename" : "image [email protected]", | ||
"idiom" : "universal", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"filename" : "image [email protected]", | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+28.7 KB
p2p_wallet/Resources/Assets.xcassets/referral-icon.imageset/image [email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+51 KB
p2p_wallet/Resources/Assets.xcassets/referral-icon.imageset/image [email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions
22
p2p_wallet/Resources/Assets.xcassets/share-3.imageset/Contents.json
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,22 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"filename" : "[email protected]", | ||
"idiom" : "universal", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"filename" : "[email protected]", | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+683 Bytes
p2p_wallet/Resources/Assets.xcassets/share-3.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+809 Bytes
p2p_wallet/Resources/Assets.xcassets/share-3.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.